>>> a = [ 0, "1", "Two", 3.0, [4], None, (6,7), {8}, {"b":"9"} ]
>>>
>>> type(a[0])
<type 'int'>
>>> type(a[1])
<type 'str'>
>>> type(a[2])
<type 'str'>
>>> type(a[3])
<type 'float'>
>>> type(a[4])
<type 'list'>
>>> type(a[5])
<type 'NoneType'>
>>> type(a[6])
<type 'tuple'>
>>> type(a[7])
<type 'set'>
>>> type(a[8])
<type 'dict'> # 键(key) 必须是不可变的,如字符串,数字或元组
--------------------------------------------
list = [] # Can add/append/pop/remove/insert 的方法 methods
tuple = () # No append or pop
dict = {“KEY”:“VALUE”} # key/value pair 键值对
set = set() # No Repeat value, 无重复 表示为 大括号{}
-----------------------
>>>list = [11, 12, 13, 12, 11]
>>>set(list) # Remove duplicate values from list
{11,12,14}
--------------------------------------------
a = "test"
[x for x in a] # List comprehensions
['t', 'e', 's', 't']
[(x+"1") for x in a] # List comprehensions
['t1', 'e1', 's1', 't1']
-----------------------
a.split("String") 得到列表
"string".join(列表)得到 新String
>>> a
'1.2.3.4'
>>> a.split(".")
['1', '2', '3', '4']
>>> b=a.split(".")
>>> "-".join(b)
'1-2-3-4'
--------------------------------------------
定义:
a={} # 空字典
b={}
a={1:'One'} # 字典和一个key/value Pair
a[2]='Two'
b[3]=‘Three'
修改:
a[2]='二'
b[3]=’三‘
删除:
del a[1] # 删除 键 as 1
b.clear() # 清空empty字典 b
del dict # 删除remove字典
Function & 方法:
str(a)
len(a)
-----------------------
a.keys() # 显示所有字典 a 的键Keys
a.values() # 显示所有字典 a 的值Vlaues
-----------------------
a.get(1) # 如果制定key不存在,则显示None
None
a.get(2)
Two
a.get(1,ONE) # 如果制定key不存在,返回 ONE
ONE
a.setdefault(1,one) # 与.get类似,如果可以不存在,赋值 one
-----------------------
x in a # 键 x 是否在 dictionary a 里面, 返回 True/False
x not in a
-----------------------
seq=['a','b','c']
a={}
a = a.fromkeys(seq) # 创建 dictionary with key a,b,c
{'a': None, 'b': None, 'c': None}
a = a.fromkeys(seq, 10) # 创建 dictionary with key a,b,c 并全部赋值 10
{'a': 10, 'b': 10, 'c': 10}
-----------------------
d={1:"a",2:"b",3:"c"}
result=[]
for k,v in d.items(): # 显示出所有(键值对)组
result.append(k) # 拆分他们为新的list
result.append(v)
print(d.items())
print(result)
dict_items([(1, 'a'), (2, 'b'), (3, 'c')])
[1, 'a', 2, 'b', 3, 'c']
----------------------
for i in d.keys(): # 搜集dictionary中所有key并拆分他们为新的list,同理运用与value
print (i)
key+=str(i)
print (key)
['1', '2', '3']
a.pop()
b.remove()
a.append()
b.extend()
"AstrBstrCstr".split("str") 得到列表 ["a",“b”,“c”]
"123".join(列表["a",“b”,“c”]) 得到 新String "a123b123c"
chr(97), ord('a'), hex('a')
print(b)
map(lambda x: x*2, [3,4,5])
filter(lambda x: x%2==0, [2,3,4])
return 永久存储
print 临时显示