您的位置:首页 > 编程语言 > Python开发

02 Python元组 字典 数据类型 if while for 迭代

2015-12-13 14:59 781 查看
元组
字典类型
数据类型与运算
集合的交集
if while for自动迭代
将两个列表组合成字典
迭代器:列表解析 比for 更快的方法 对原数值进行选择利用
偏移 元素

元组
In [1]: t1= (1,1,2,3) 用括号表示元组In [2]: t1.count(1) 统计Out[2]: 2In [3]: t1.index(3) 索引Out[3]: 3In [4]: print t1[2:] 切片(2, 3)In [5]: t4='x','y','zy' 也可以省略括号In [6]: print t4('x', 'y', 'zy')In [15]: t4=(1,5,7) 元组相加In [16]: t3=(2,5,4,9)In [17]: t4+t3 元组相加Out[17]: (1, 5, 7, 2, 5, 4,9) 生成的是一个新的元组In [19]: t4*2 指定重复次数Out[19]: (1, 5, 7, 1, 5, 7)In [20]: 5 in t4 判断成员是否存在Out[20]: True






字典类型(通过键实现存取,无序集合)
In [1]:d1={'a':32,'b':[1,2,3,4,5]}In [2]: d1['a']Out[2]: 32In [3]: d1['b']Out[3]: [1, 2, 3, 4, 5]In [6]: d1['b'][:]In [7]: d1['b'][3:]Out[7]: [4, 5]In [8]: len(d1) 获取字典的长度Out[8]: 2In [9]: d1['a']=111 修改值后 无序存放In [10]: print d1{'a': 111, 'b': [1, 2, 3, 4,5]}In [14]: d2=d1.copy()In [15]: print d2{'a': 111, 'b': [1, 2, 3, 4,5]}In [16]: d2.get('a') 返回值 不存在不会抛出异常Out[16]: 111In [17]: d1.has_key('a')Out[17]: TrueIn [18]: d1.items() 把字典转化为列表Out[18]: [('a', 111), ('b',[1, 2, 3, 4, 5])]In [19]: q1,q2=d1.items() 变量解包In [20]: print q1('a', 111)In [21]: print q2('b', [1, 2, 3, 4, 5])In [22]: m1,m2={'a':32,'f':54} 保存的是键值In [23]: print m1aIn [24]: print m2f In [28]: print d1{'a': 111, 'b': [1, 2, 3, 4,5]}In [25]: d1.keys() 返回键值Out[25]: ['a', 'b']In [26]: d1.items() 拆解为列表Out[26]: [('a', 111), ('b',[1, 2, 3, 4, 5])]In [27]: d1.values() 返回数值Out[27]: [111, [1, 2, 3, 4,5]]In [29]: d1.popitem() 随机弹出键值映射Out[29]: ('a', 111)In [30]: d2 = {'a':32,'b':43}键名重复时,会覆盖原来的键值,相当于合并覆盖In [31]: d3 ={'a':37,'c':63,'f':7546}In [32]: d2.update(d3)In [33]: print d2{'a': 37, 'c': 63, 'b': 43,'f': 7546} In [39]: i1=d2.iteritems() In [40]: i1.next()Out[40]: ('a', 37)In [41]: i1.next()Out[41]: ('c', 63)In [42]: i1.next()Out[42]: ('b', 43)In [43]: i2=d2.iterkeys()In [44]: i2.next() 迭代器Out[44]: 'a'In [45]: i2.next()Out[45]: 'c'In [46]: zip('123','qwe') 展开功能,不被匹配的就会被舍弃Out[46]: [('1', 'q'), ('2','w'), ('3', 'e')]In [47]:zip('123','qwe','222')Out[47]: [('1', 'q', '2'),('2', 'w', '2'), ('3', 'e', '2')]In [52]:dict(zip('123','qwe')) 用来创建字典Out[52]: {'1': 'q', '2': 'w','3': 'e'}----------------------------------------------------------------
数据类型与运算
n[55]: l1=[8,6,4,6,9,0,45,2] In [56]: l1.sort()In [58]: print l1[0, 2, 4, 6, 6, 8, 9, 45]In [59]: l1.reverse()In [60]: print l1[45, 9, 8, 6, 6, 4, 2, 0]
集合




集合的交集
In [1]: s1=set([1,2,3,4])In [2]: s2=set([5,6,3,9,94])In [3]: s1&s2 都一样In [5]: s1.intersection(s2)Out[3]: {3}In [4]: s1|s2Out[4]: {1, 2, 3, 4, 5, 6, 9,94}In [7]:s1.symmetric_difference(s2) 显示不同 Out[7]: {1, 2, 4, 5, 6, 9,94}In [8]: s2.pop() 也支持弹出Out[8]: 9In [9]: s2.update(s1) 合并集合In [10]: print s2set([1, 2, 3, 4, 5, 6, 94])In [11]: s1.add(78) 加入数值或字符串In [12]: print s1set([1, 2, 3, 4, 78])In [13]: s1.add('haha')In [14]: print s1set([1, 2, 3, 4, 'haha', 78])import sys 看计数器引用的次数In [17]:sys.getrefcount(haha)自己也会用一次Out[17]: 3 In [23]: a1=3.0In [23]: a2=4.0In [24]: a2 // a1Out[24]: 1.0In [25]: a2 / a1Out[25]: 1.3333333333333333



























In[26]: s1 =('1','3','k')

In[27]: x,v,f=s1 括号可以省略,就是元组

In[28]: print x,v,f
1 3k







In[1]: l1=[1,2,3,4,5]

In[2]: if 2 in l1:
...: print "haha 2 in l1"
...:
haha2 in l1
In[4]: 2 in l1
Out[4]:True
In[5]: x=3

In[6]: y= 5
if判断语句
In [7]: if x>y : ...: print "max = x" ...: else: ...: print "max =y " ...: max =y In [18]: if name=="lcl": print "name = %s" %nameelifname=="hahaha": print "%s" %nameelse: ....: name='lcl' ....: name = lcl In [20]: A=1In [21]: B=3In [22]: MAX = A if A>Belse BIn [23]: print MAX3
whlie 循环
In [24]: u="www.baidu.com" In [25]: while u: ....: print u ....: u=u[1:] ....: www.baidu.comww.baidu.comw.baidu.com.baidu.combaidu.comaidu.comidu.comdu.comu.com.comcomomm In [45]: u="www.baidu.com" In [46]: while u: print u u=u[:-1] ....: www.baidu.comwww.baidu.cowww.baidu.cwww.baidu.www.baiduwww.baidwww.baiwww.bawww.bwww.wwwwwwx=0;y=10In [35]: x=0 In [36]: while x<y: print x x+=1 ....: 0123456789 In [38]: while x<y: print x,加逗号 可以在一行中显示 x+=1 ....: 0 1 2 3 4 5 6 7 8 9 In [55]: u="www.baidu.com" In [56]: while u: print u u=u[:-1] ....: else: ....: print "hahah" 执行完后做一次 ....: www.baidu.comwww.baidu.cowww.baidu.cwww.baidu.www.baiduwww.baidwww.baiwww.bawww.bwww.wwwwwwhahah


In[83]: l1=[1,2,5,63,89,0]

In[84]: while l1:
....: print l1[0]
....: l1.pop(0)
....:
1
2
5
63
89
0

逆序来显示
In [85]: l1=[1,2,5,63,89,0] In [86]: while l1: ....: print l1[-1] ....: l1.pop() 默认弹出最后一个 ....: 08963521





In[96]: x=1

In[97]: l1=[]

In[98]: while x<=100:
l1.append(x)
x+=2
....:
In[102]: print l1
[1,3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83,85, 87, 89, 91, 93, 95, 97, 99]

将两个列表组合成字典
In [103]: d1={}In [104]: count = 0In [105]: l1= [0,1,2,3,4,5,6]In [106]:l2=['sun','mon','tue','wen','thu','fri','sat']In [108]: while count<len(l1): .....: d1[l1[count]]= l2[count] .....: count+=1 .....: In [109]: print d1{0: 'sun', 1: 'mon', 2:'tue', 3: 'wen', 4: 'thu', 5: 'fri', 6: 'sat'}

for循环 (比while要快)

In [111]: for x in l2: 自动迭代 .....: print x .....: sunmontuewenthufrisat生成1到33个数的数列 range(1,34) sum =0In [119]: for i in range(1,101): 先在内存生成序列 sum+=iprint sum----------------------------------------In [125]: sum =0In [126]: for i in xrange(1,101): 用一个数就生成一个数,节约空间,合理利用内存 sum+=i .....: In [127]: print sum5050











In[128]: test=[]
In[130]: while True:
.....: x=raw_input("输入一个数")
.....: if x == 'q' or x == 'quit':
.....: break
.....: else:
.....: test.append(x)
In [131]: print test
['3','4', '5', '6', '7', 'f', 'd']




In [132]: d1={'q':122,'d':54324,'t':754,'f3':432}In [133]: for(k,v)ind1.items(): print k,vq 122f3 432d 54324t 754显示奇数项的值
l2=['sun','mon','tue','wen','thu','fri','sat']for i in range(1,len(l2),2):print l2[i]monwenfri
挑出
In [138]:l2=['sun','mon','tue','wen','thu','fri','sat']In [139]:l1=['haha','fd','fds','wen','thu','fri','sat']In [140]: l3 =[]In [142]: for i in l2: if i not in l1: l3.append(i)In [143]: print l3['sun', 'mon', 'tue']


删除别人已存在的
In [144]: l1=['sun','mon','tue','wen','thu','fri','sat']In [145]: l2=['thu','fri','sat']In [147]: for i in l2: if i in l1: l1.remove(i) In [148]: print l1['sun', 'mon', 'tue', 'wen']

--------------------------------------------
迭代器
In [149]:l1=['sun','mon','tue','wen','thu','fri','sat']
In [150]: i1=iter(l1)In [151]: i1.next()Out[151]: 'sun'In [152]: i1.next()Out[152]: 'mon' 然后抛出异常,不是出错








【列表解析】以第一个为基础,创建新的列表
In [153]: k1=[1,2,3,4,5]In [154]: k2=[]In [155]: for i in k1: .....: k2.append(i**2) In [157]: print k2[1, 4, 9, 16, 25]【比for 更快的方法】
In [158]: k3=[1,3,5,7]In [159]: k4=[i**3 for i in k3]In [160]: print k4[1, 27, 125, 343]【对原数值进行选择利用】

In [161]: k4=[i**3 for i ink3 if i >3] In [162]: print k4[125, 343]【1到10的平方,再除以2】
In [167]: for i in [i**2 fori in range(1,11)]: print i/2, .....: 0 2 4 8 12 18 24 32 40 50【1到10偶数的平方,再除以2】
In [168]: for i in [i**2 fori in range(2,11,2)]: print i/2, .....: 2 8 18 32 50

【获取log后缀的文件名】
In [174]: filelist1=os.listdir('/var/log') In [175]: filelist2=[i for iin filelist1 if i.endswith('.log') ]print filelist2['mysqld.log','anaconda.ifcfg.log', 'anaconda.storage.log', 'dracut.log','anaconda.program.log', 'yum.log', 'anaconda.yum.log', 'Xorg.9.log','boot.log', 'Xorg.0.log', 'anaconda.log', 'wpa_supplicant.log','pm-powersave.log']【类似于多项式相乘】
In [178]: l1 =[3,5,7,9,67]In [179]: l2=['a','s','d']In [180]: l3=[(i,j) for i inl1 for j in l2]In [182]: print l3 [(3, 'a'), (3, 's'), (3,'d'), (5, 'a'), (5, 's'), (5, 'd'), (7, 'a'), (7, 's'), (7, 'd'), (9, 'a'), (9,'s'), (9, 'd'), (67, 'a'), (67, 's'), (67, 'd')]【再复杂一点,5不参与运算】
In [1]: l1 =[3,5,7,9,67]In [2]: l2=['a','s','d']In [5]: l3=[(i,j) for i in l1for j in l2 if i!=5]In [6]: print l3[(3, 'a'), (3, 's'), (3,'d'), (7, 'a'), (7, 's'), (7, 'd'), (9, 'a'), (9, 's'), (9, 'd'), (67, 'a'),(67, 's'), (67, 'd')]【生成器表达式用小括号】
In[7]: [i**2 for i in range(1,11) ]Out[7]:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]In[8]: h1=(i**2 for i in range(1,11) )In[9]: h1.next()Out[9]:1In[10]: h1.next()Out[10]:4


【使用方法】In [11]:u="www.lcl.com"In [12]: u1=enumerate(u)In [13]: u1.next()Out[13]: (0, 'w')In [14]: u1.next()Out[14]: (1, 'w')In [15]: u1.next()Out[15]: (2, 'w')In [16]: u1.next()Out[16]: (3, '.')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息