标签:values tuple hello def obj val ems item tde
>>> a = [‘a‘, ‘b‘, 123, ‘hello‘] >>> print a [‘a‘, ‘b‘, 123, ‘hello‘] >>> a.append(‘name‘) >>> print(a) [‘a‘, ‘b‘, 123, ‘hello‘, ‘name‘] >>> print(a.index(123)) 2 >>> a.insert(1, ‘good‘) >>> print(a) [‘a‘, ‘good‘, ‘b‘, 123, ‘hello‘, ‘name‘] >>> a.pop() ‘name‘ >>> print(a) [‘a‘, ‘good‘, ‘b‘, 123, ‘hello‘] >>> a.remove(123) >>> print(a) [‘a‘, ‘good‘, ‘b‘, ‘hello‘] >>> a.sort() >>> print(a) [‘a‘, ‘b‘, ‘good‘, ‘hello‘] >>> a.reverse() >>> print(a) [‘hello‘, ‘good‘, ‘b‘, ‘a‘] >>> print(a) [‘hello‘, ‘good‘, ‘b‘, ‘a‘]
>>> print(a) [‘hello‘, ‘good‘, ‘b‘, ‘a‘] >>> print(a[2:]) [‘b‘, ‘a‘] >>> print(a[1:3]) [‘good‘, ‘b‘] >>> print(a[-1:]) [‘a‘] >>> print(a[::2]) [‘hello‘, ‘b‘]
t = (‘a‘, ‘b‘, 123, 345) >>> a=‘abcd‘ >>> tuple(a) (‘a‘, ‘b‘, ‘c‘, ‘d‘) >>> a = (‘hello‘,) >>> print(a) (‘hello‘,)
>>> t1 = (1, 2, 1 ,4 ,‘a‘, ‘a‘ ,‘hello‘) >>> t1.count(1) 2 >>> t1.count(‘a‘) 2 >>> t1.index(‘hello‘) 6 >>> t1.index(1) 0
>>> print(dic3.get(‘name‘)) hee
>>> print(dic3.setdefault(‘addr‘, ‘beijing‘)) beijing
>>> print(dic3.keys()) [‘age‘, ‘name‘, ‘addr‘]
>>> print(dic3.values()) [20, ‘hee‘, ‘beijing‘]
>>> for k,v in dic3.iteritems(): ... print(k,v) ... (‘k‘, None) (‘age‘, 20) (‘name‘, ‘hee‘) (‘addr‘, ‘beijing‘) >>> for k,v in dic3.items(): ... print(k, v) ... (‘k‘, None) (‘age‘, 20) (‘name‘, ‘hee‘) (‘addr‘, ‘beijing‘)
>>> print(dic3) {‘k‘: None, ‘age‘: 20, ‘name‘: ‘hee‘, ‘addr‘: ‘beijing‘} >>> dic3.pop(‘addr‘) ‘beijing‘ >>> print(dic3) {‘k‘: None, ‘age‘: 20, ‘name‘: ‘hee‘}
dict.fromkeys(seq[, value])) >>> l = [‘a‘, ‘b‘, ‘c‘, ‘d‘] >>> dic4 = dict.fromkeys(l, 123) >>> print(dic4) {‘a‘: 123, ‘c‘: 123, ‘b‘: 123, ‘d‘: 123}
>>> l1 = [‘a‘, ‘b‘, ‘c‘, ‘d‘] >>> l2 = [1, 2, 3, 4] >>> dic5 = zip(l1, l2) >>> print(dic5) [(‘a‘, 1), (‘b‘, 2), (‘c‘, 3), (‘d‘, 4)] >>> print(dict(dic5)) {‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘d‘: 4}
>>>a = [5,7,6,3,4,1,2] >>> b = sorted(a) # 保留原列表 >>> a [5, 7, 6, 3, 4, 1, 2] >>> b [1, 2, 3, 4, 5, 6, 7] >>> L=[(‘b‘,2),(‘a‘,1),(‘c‘,3),(‘d‘,4)] >>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1])) # 利用cmp函数 [(‘a‘, 1), (‘b‘, 2), (‘c‘, 3), (‘d‘, 4)] >>> sorted(L, key=lambda x:x[1]) # 利用key [(‘a‘, 1), (‘b‘, 2), (‘c‘, 3), (‘d‘, 4)] >>> students = [(‘john‘, ‘A‘, 15), (‘jane‘, ‘B‘, 12), (‘dave‘, ‘B‘, 10)] >>> sorted(students, key=lambda s: s[2]) # 按年龄排序 [(‘dave‘, ‘B‘, 10), (‘jane‘, ‘B‘, 12), (‘john‘, ‘A‘, 15)] >>> sorted(students, key=lambda s: s[2], reverse=True) # 按降序 [(‘john‘, ‘A‘, 15), (‘jane‘, ‘B‘, 12), (‘dave‘, ‘B‘, 10)] >>
标签:values tuple hello def obj val ems item tde
原文地址:http://www.cnblogs.com/yshan13/p/7720018.html