标签:err .so 数据类型 length ace test blog 字典 ipython
In [125]: a=[] #新增方法1 In [126]: a.append("hello") In [127]: a Out[127]: [‘hello‘] #新增方法2 In [128]: a.insert(0,"huahua") In [129]: a Out[129]: [‘huahua‘, ‘hello‘]
In [131]: a Out[131]: [‘huahua‘, ‘hello‘] #删除方法1 In [132]: a.pop(0) Out[132]: ‘huahua‘ In [133]: a Out[133]: [‘hello‘] #删除方法2 In [136]: a.remove("hello") In [137]: a Out[137]: [] #删除方法3 In [138]: a.clear() In [139]: a Out[139]: []
In [145]: a Out[145]: [‘huahua‘, ‘hello‘] #查看huahua这个字符串的角标 In [146]: a.index("huahua") Out[146]: 0 #给a排序 In [147]: a.sort() In [148]: a Out[148]: [‘hello‘, ‘huahua‘]
In [77]: d={} #新增方法1 In [78]: d["l1"]=1 In [79]: d Out[79]: {‘l1‘: 1} #新增方法2 In [81]: d.update({"13":333}) In [82]: d Out[82]: {‘13‘: 333, ‘l1‘: 1}
In [85]: d Out[85]: {‘13‘: 333, ‘l1‘: 1} #删除方法1 In [86]: del d["l1"] In [92]: d Out[92]: {‘1‘: 2, ‘11‘: 22} #删除方法3 In [98]: d.pop("1") Out[98]: 2 In [99]: d Out[99]: {‘11‘: 22}
In [100]: d={"l1":1,"l2":2} In [101]: d.values() Out[101]: dict_values([1, 2]) In [102]: d.keys() Out[102]: dict_keys([‘l1‘, ‘l2‘]) In [103]: d.items() Out[103]: dict_items([(‘l1‘, 1), (‘l2‘, 2)]) In [104]: for k,v in d.items(): ...: print(k,v) ...: l1 1 l2 2
In [166]: a={1,2,3,4,5} #新增方法1 In [167]: a.add(6) In [168]: a Out[168]: {1, 2, 3, 4, 5, 6}
In [168]: a Out[168]: {1, 2, 3, 4, 5, 6} #删除方法1 In [169]: a.pop() Out[169]: 1 In [170]: a Out[170]: {2, 3, 4, 5, 6} #删除方法2 In [172]: a.remove(2) In [173]: a Out[173]: {3, 4, 5, 6} #删除方法3 In [175]: a.clear() In [176]: a Out[176]: set()
In [180]: a Out[180]: {1} #将传入的元素拆分,做为个体传入到集合中 #不能传入int值 In [182]: a.update(111) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-182-7c012df56cc5> in <module>() ----> 1 a.update(111) TypeError: ‘int‘ object is not iterable #将传入的元素拆分,做为个体传入到集合中 In [183]: a.update("111") In [184]: a Out[184]: {1, ‘1‘}
In [200]: test=("1",2,3) #查询3在该元组的角标是多少 In [201]: test.index(3) Out[201]: 2 #查询2在该元组一共有多少个 In [202]: test.count(2) Out[202]: 1
1.将str转换成各个类型
In [19]: test="123" #查看test的类型 In [20]: type(test) Out[20]: str #将字符串转换成list In [21]: list(test) Out[21]: [‘1‘, ‘2‘, ‘3‘] #将字符串转换成set In [22]: set(test) Out[22]: {‘1‘, ‘2‘, ‘3‘} #将str转换成tuple In [23]: tuple(test) Out[23]: (‘1‘, ‘2‘, ‘3‘) #无法将字符串转换成dict In [24]: dict(test) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-24-7b6401465808> in <module>() ----> 1 dict(test) ValueError: dictionary update sequence element #0 has length 1; 2 is required
2.列表与字典的转换
In [38]: lol=[["a","b"],["c","d"]] #lol的类型 In [39]: type(lol) Out[39]: list #双列表转换成dict In [40]: dict(lol) Out[40]: {‘a‘: ‘b‘, ‘c‘: ‘d‘} #列表里夹杂元组 In [41]: asd=[("aa","bb")] #asd的类型 In [42]: type(asd) Out[42]: list #将list转换成字典 In [43]: dict(asd) Out[43]: {‘aa‘: ‘bb‘} #字典 In [44]: qwe={"aa":"bb"} #类型 In [45]: type(qwe) Out[45]: dict #将dict转换成list。注意:只取到key In [47]: list(qwe) Out[47]: [‘aa‘]
3.使用zip将2个变量组合成一个集合、字典、列表、元组
In [48]: num=[1,2,3] In [49]: englist=["one","two","three"] #将2个组合成一个list In [50]: list(zip(num,englist)) Out[50]: [(1, ‘one‘), (2, ‘two‘), (3, ‘three‘)] #将2个组合成一个dict In [51]: dict(zip(num,englist)) Out[51]: {1: ‘one‘, 2: ‘two‘, 3: ‘three‘} #将2个组合成一个set In [52]: set(zip(num,englist)) Out[52]: {(1, ‘one‘), (2, ‘two‘), (3, ‘three‘)} #将2个组合成一个ltuple In [53]: tuple(zip(num,englist)) Out[53]: ((1, ‘one‘), (2, ‘two‘), (3, ‘three‘))
4.将列表或元组转换成字典。注意:里面的每一个字符串只能由2个字符组成
#一个key与value成对的列表 In [68]: los=[‘as‘,‘zc‘] #转换为字典 In [69]: dict(los) Out[69]: {‘a‘: ‘s‘, ‘z‘: ‘c‘} #一个元组 In [70]: los=("az","ww") #转换为字典 In [71]: dict(los) Out[71]: {‘a‘: ‘z‘, ‘w‘: ‘w‘}
标签:err .so 数据类型 length ace test blog 字典 ipython
原文地址:http://www.cnblogs.com/8013-cmf/p/7100193.html