一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python字符串/元祖/列表/字典互转介绍

时间:2016-08-17 编辑:简简单单 来源:一聚教程网

#-*- coding:UTF-8 -*-

#author:RXS002

#1.字典

dict = {'name':'Zara','age':7,'class':'First'}

#字典转换为字符串,返回: {'age':7,'name':'Zara','class':'First'}

print (type(str(dict)),str(dict))

#字典可以转为元祖,返回:('age','name','class')

print (tuple(dict)

#字典可以转为元祖,返回(7,'Zara','First')

print tuple(dict.values())

#字典转为列表,返回:['age','name','class']

print list(dict)

#字典转为列表

print (dict.values)

#2.元祖

tup = (1,2,3,4,5)

#元祖转为字符串,返回:(1,2,3,4,5)

print (tup.__str__())

#元祖转为列表,返回:[1,2,3,4,5]

print (list(tup))

#元祖不可以转为字典

#3.列表
nums = [1,3,5,7,8,13,20];
#列表转为字符串,返回:[1,3,5,7,8,13,20]
print(str(nums))
#列表转为元祖,返回:(1,3,5,7,8,13,20)
print(tuple(nums))
#列表不能转为字典

#4.字符串
#字符串转为元祖,返回:(1,2,3)
print(tuple(eval('1,2,3')))
#字符串转为列表,返回:[1,2,3]
print (list(eval('1,2,3')))
#字符串转为字典,返回:
print (type(eval("{'name':'srx','age':'41'}")))

补充:

python的基础数据结构有:列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set)

1)列表(list)

#1)创建
list = ['1',(1,2),'1', '2']
#2) 得到list 长度
>>> print len(list)
4
#3) 删除
>>> del list[0]
>>> print list
[(1, 2), '1', '2']
>>> del list[0:2]
>>> print list
['2']

#4) 添加
>>> list.append('3')
>>> print list
['2', '3']

#5) 插入
>>> list[0:0] = ['sample value']
>>> print list
['sample value', '2', '3']
>>> list[0:0] = ['sample value', 'sample value 1']
>>> print list
['sample value', 'sample value 1', 'sample value', '2', '3']
>>> list[1:2] = ['sample value 2', 'sample value 3']
>>> print list
['sample value', 'sample value 2', 'sample value 3', 'sample value', '2', '3']

#6) 取值,遍历
取值单个值
>>> print list[0]
sample value
>>> print list[1]
sample value 2
>>> print list[2]
sample value 3

取片段
>>> print list[2:4]
['sample value 3', 'sample value']

遍历
>>> for line in list:
...     print line
...
sample value
sample value 2
sample value 3
sample value
2
3

list的方法
L.append(var)   #追加元素
L.insert(index,var)
L.pop(var)      #返回最后一个元素,并从list中删除之
L.remove(var)   #删除第一次出现的该元素
L.count(var)    #该元素在列表中出现的个数
L.index(var)    #该元素的位置,无则抛异常
L.extend(list)  #追加list,即合并list到L上
L.sort()        #排序
L.reverse()     #倒序

2)元祖(tuple)

#元组和列表十分类似,只不过元组和字符串一样是
#不可变的 即你不能修改元组
tuple = ('a', 'b', 'c', 'd', 'e')
>>> print tuple[0]
a
>>> print tuple[0:2]
('a', 'b')
3)字符串(string)

string = "Hello My friend"
>>> print string[0]
H
>>> print string[0:5]
Hello

字符串包含判断操作符:in,not in
>>> print 'He' in string
True
>>> print 'sHe' in string
False

*后面跟数字表示字符串重复的次数,比如
print 'hello'*5
>>> hellohellohellohellohello

string模块,还提供了很多方法,如
S.find(substring, [start [,end]]) #可指范围查找子串,返回索引值,否则返回-1
S.rfind(substring,[start [,end]]) #反向查找
S.index(substring,[start [,end]]) #同find,只是找不到产生ValueError异常
S.rindex(substring,[start [,end]])#同上反向查找
S.count(substring,[start [,end]]) #返回找到子串的个数

S.lowercase()
S.capitalize()      #首字母大写
S.lower()           #转小写
S.upper()           #转大写
S.swapcase()        #大小写互换

S.split(str, ' ')   #将string转list,以空格切分
S.join(list, ' ')   #将list转string,以空格连接

处理字符串的内置函数
len(str)                #串长度
cmp("my friend", str)   #字符串比较。第一个大,返回1
max('abcxyz')           #寻找字符串中最大的字符
min('abcxyz')           #寻找字符串中最小的字符

string的转换

float(str) #变成浮点数,float("1e-1")  结果为0.1
int(str)        #变成整型,  int("12")  结果为12
int(str,base)   #变成base进制整型数,int("11",2) 结果为2
long(str)       #变成长整型,
long(str,base)  #变成base进制长整型,

字符串的格式化(注意其转义字符,大多如C语言的,略)
str_format % (参数列表) ?#参数列表是以tuple的形式定义的,即不可运行中改变
>>>print ""%s's height is %dcm" % ("My brother", 180)
#结果显示为 My brother's height is 180cm
4)字典(dict)

key-value的数据结构,跟c++中的stl:map类似。

#创建字典:
#1)基本
d = {} #空字典
d = {'name':'tom', 'age':22}
#等价
d = {}
d['name'] = 'tom'
d['age'] = 22
2)dict
d = dict() #空
d = dict(name='tom', age=22)

d = dict([('name','tom'), ('age',22)])
#等价
keys = ['name','age']
values = ['tom', 22]
d = dict(zip(keys,values))

#3) fromkeys
>>> dict.fromkeys(['name','age'],'default_value')
{'age': 'default_value', 'name': 'default_value'}

#判断key是否存在
if k in d:   #k not in
dosomething()

#读取
print d['name'] #存在得到结果,但是若键不存在,将引发异常KeyError。慎用,建议不使用
print d.get('name', 'jack') #存在得到,若键不存在,返回第二个参数default_value.若是没有设default_value返回None

#使用用例
if k in d:
print d[k]

try:
print d[k]
except KeyError:
dosomething()

print d.get(k, default)
#等价 d[k] if k in d else default

#遍历
for key in d:
print key, d[key]
#等价 for key in d.keys()

for key,value in d.items():
print key, value

#修改
d['name'] = 'tom'

d.update({'name':'tom'})  #这里支持一整组值
d.update( [ ('name','tom'), ('age',2) ] ) #每个元组两个元素,(key,value)
d.update('name'='tom', 'age'=4)

#删除
del d['key']
value = d.pop('key') #删除并返回值
d.clear() #清空

#排序
d = {'a':10, 'c':8, 'b':9, 'd':7}
#1)字典排序 按照key排序
keys = d.keys()
keys.sort()
for key in keys:
print d.get(key)
结果为:
10
9
8
7

#2) 按照value进行排序
sorted(d.items(), lambda x,y: cmp(x[1],y[1]))
结果为:
[('d', 7), ('c', 8), ('b', 9), ('a', 10)]

#3) 另一种排序方法
sorted(d)
>>> print d
{'a': 10, 'c': 8, 'b': 9, 'd': 7}

热门栏目