#序列的sum函数,列表后面的数不是列表中的数,而是自定义的一个加数。
a = [3,4,77,88,54]
print (sum(a))          #226
print (sum(a,10000))    #10226

#eval函数就是执行一个字符串表达式,并返回表达式的值,实现list、dict、tuple与str之间的转化。
a = '[3,4,77,88,54]'
print('字符串a使用eval前的类型:',type(a))        #字符串a使用eval前的类型: <class 'str'>

print('字符串a使用eval后的类型:',type(eval(a)))  #字符串a使用eval后的类型: <class 'list'>

b = "{'q':3,'w':4,'t':8}"
print('字符串a使用eval前的类型:',type(b))        #字符串a使用eval前的类型: <class 'str'>

print('字符串a使用eval后的类型:',type(eval(b)))  #字符串a使用eval后的类型: <class 'dict'>




#将一个字符串用字典输出显示
c = "xyzacg"
strc = str(c)
dict1 = {}
for i,item in enumerate(c):
    dict1[i] = item
print (dict1)                      #{0: 'x', 1: 'y', 2: 'z', 3: 'a', 4: 'c', 5: 'g'}

#输出字符串c中第n~m个字符,c[n-1,m],因为结束的时候不算第m个。
print('c的第3~7个字符为:',c[2:7])  #c的第3~7个字符为: zabce


#将两个字符串打包成字典
d = ("aa","bb","cc")
e = [12,13,14]
f =dict(zip(d,e))
print ("元组和列表打包为字典:",f)   #元组和列表打包为字典: {'aa': 12, 'bb': 13, 'cc': 14}

d = "xyzufs"
e = "123hvn"
f2 =dict(zip(d,e))
print ("两个字符串打包为字典:",f2)  #两个字符串打包为字典: {'x': '1', 'y': '2', 'z': '3', 'u': 'h', 'f': 'v', 's': 'n'}

结果:

226
10226
字符串a使用eval前的类型: <class 'str'>
字符串a使用eval后的类型: <class 'list'>
字符串a使用eval前的类型: <class 'str'>
字符串a使用eval后的类型: <class 'dict'>
{0: 'x', 1: 'y', 2: 'z', 3: 'a', 4: 'c', 5: 'g'}
c的第3~7个字符为: zacg
元组和列表打包为字典: {'aa': 12, 'bb': 13, 'cc': 14}
两个字符串打包为字典: {'x': '1', 'y': '2', 'z': '3', 'u': 'h', 'f': 'v', 's': 'n'}

 

posted on 2019-11-21 22:03  jxba  阅读(265)  评论(0编辑  收藏  举报