[8, 6, -18, 16, 12, -36, 24, 18, -54]
>>>[vec[i]+vec2[i] for i in range(len(vec))]
[6, 7, -3]
>>>[str(round(355/113.0,i)) for i in range(1,6)]
#str()是转换类型为可以打印的字符
#round(x,n)表示对x保留n位小数(四舍五入)
['3.1', '3.14', '3.142', '3.1416', '3.14159']
3.4. 元组
一旦初始化便不能更改的数据结构,速度比list快
>>>t=1234,5567,'hello' #t=(1234,5567,'hello')的简写
>>>x,y,z=t #拆分操作可以应用于所有sequence
>>>x
1234
>>>u=t,(1,2,3)
>>>u
((1234,5567,'hello'),(1,2,3))
>>>empty=() #空元组
>>>singleton='hi', #单个元素的元组,注意逗号
通过元组可以很简单的进行数据交换. 比如:
a=1
b=2
a,b=b,a
3.5. set
set(集合):无序不重复的元素集
>>>basket = ['apple','orange','apple','pear','apple','banana']
>>>fruit=set(basket)
>>>fruit
set(['orange', 'pear', 'apple', 'banana'])
>>>'orange' in fruit
True
>>>a=set('abracadabew')
>>>a
set(['a', 'c', 'b', 'e', 'd', 'r', 'w'])
>>>b=set('wajgwaoihwb')
>>>b
PyAbsolutelyZipManual - Woodpecker.org.cn Wiki for CPUG
file:///C:/Users/Administrator/AppData/Roaming/Mozilla/F...
第6页 共26页 2009/9/5 10:16