{}
Dict dict(key1=value1,.....)函数
Dict dict.fomKeys([key1,key2,....],default_value=None)
dictionary1={“name”:"Jack","sex":"male","age":25} type(dictionary1) --> <class 'dict'> dictionary1 --> {'name': 'Jack', 'sex': 'male', 'age': 25} dictionary2=dict.fromkeys(["name","sex","age"],"unknown") type(dictionary2) --> <class 'dict'> dictionary2 --> {'name': 'unkonown', 'sex': 'unkonown', 'age':'unknown'}value Dict[key]
value Dict.get(key[,default_value])
dictionary={“name”:"Jack","sex":"male","age":25} dictionary["name"] --> 'Jack' dictinnary["grade"] --> NameError dictionary.get("name") --> 'Jack' dictionary.get("grade") --> dictionary.get("grade","unkonwn") --> 'unknown'for key in Dict:\n ......
dictionary={"name":"Jack","age":"25"} for key in dictionary: print(key,"---",dictionary[key]) Result: name --- Jack age --- 25for key,value in Dict.items():\n ......
dictionary={"name":"Jack","age":"25"} for key,value in dictionary.items(): print(key,"---",value) Result: name --- Jack age --- 25Dict[key]=new_value
void Dict.update({key1:new_value1,key2:value2,.....})
增操作与改操作相同,有则更新,无则增加(上面两种方法都可以)
value Dict.pop(key) --> 删除key对应的一项
Tuple Dict.popitem() --> 删除最后一项
void Dict.clear() --> 清空字典
dict={"name":"Jack","sex":"male","age":"25"} dict --> {'name': 'Jack', 'sex': 'male', 'age': '25'} dict["job"]="police" #1 dict --> {'name': 'Jack', 'sex': 'male', 'age': '25', 'job': 'police'} dict["age"]=25 #3 dict --> {'name': 'Jack', 'sex': 'male', 'age': 25, 'job': 'police'} dict.update({"dpt":"indentification","sal":5000}) #2 dict --> {'name': 'Jack', 'sex': 'male', 'age': 25, 'job': 'police', 'dpt': 'indentification', 'sal': 5000} dict.pop("dpt") --> 'indentification' #4 dict --> {'name': 'Jack', 'sex': 'male', 'age': 25, 'job': 'police', 'sal': 5000} dict.popitem() --> ('sal', 5000) #5 dict --> {'name': 'Jack', 'sex': 'male', 'age': 25, 'job': 'police'} dict.clear() #6 dict --> {}value Dict.setdefault(key,value) --> 设置默认值,如果key存在,可以忽略
Dict_keys Dict.keys()
Dict_values Dict.values()
Dict_items Dict.items()
2、3、4都是获取字典视图,字典变化,视图变化
老版本字典格式化字符串“%(key1)s,%(key2)s" %Dict
python3之后字典格式化字符串 “{key1},{key2}”.format_map(Dict)
dict={"name":"Jack","sex":"male","age":"25"} dict["dept"] --> KeyError dict.setdefault("deft","None") --> 'None' dict["dept"] --> KeyError type(dict.keys) --> <class 'builtin_function_or_method'> type(dict.values) --> <class 'builtin_function_or_method'> type(dict.items) --> <class 'builtin_function_or_method'> print("%(name)s--%(age)s"%dict) --> Jack--25 print("{name}--{age}".format_map(dict)) --> Jack--25Tuple3=Tuple1+Tuple2
Tuple2=Tuple*count
元组运算符适用于列表,但是前后类型应该相同
[1,2,3]+[4,5,6] --> [1, 2, 3, 4, 5, 6] [1,2,3]*3 --> [1, 2, 3, 1, 2, 3, 1, 2, 3] (1,2,3)+(4,5,6) --> (1, 2, 3, 4, 5, 6) (1,2,3)*2 --> (1, 2, 3, 1, 2, 3) (1,)*2 --> (1, 1) (1)*2 --> 2 [1]*2 --> [1, 1]Range range(begin_num,edn_num[,step])
range(1,4) --> range(1,4) range(1,10,2) --> range(1,10,2)List list(sequence) --> 转换为列表
Tuple tuple(sequence) --> 转换为元组
String element.join(String_Tuple/List) String str() --> 转换为字符串
List String.split(element) --> 传入分割元素,分割String,返回List
List String.splitlies() --> 按行分割String
for循环 --> 辅助使用,将数字转换为String
#转换为List #1,4,5 list((1,2,3,4)) --> [1, 2, 3, 4] list("abcd") --> ['a', 'b', 'c', 'd'] list(range(0,11,2)) --> [0, 2, 4, 6, 8, 10] "1,2,3,4".split(",") --> ['1', '2', '3', '4'] "1\n2\n3\n4".splitlines() --> ['1', '2', '3', '4'] #转换为Tuple #2 tuple("hello") --> ('h', 'e', 'l', 'l', 'o') tuple([1,2,3]) --> (1, 2, 3) tuple(range(1,5)) --> (1, 2, 3, 4) #转换为String #使用到join()函数 #3 ",".join("hello") --> 'h,e,l,l,o' ",".join(("a","b","c")) --> 'a,b,c' ",".join(["a","b","c"]) --> 'a,b,c' "".join(("a","b","c")) --> 'abc' "".join(["a","b","c"]) --> 'abc' #使用str()函数 #3 str(["a","b","c"] --> "['a', 'b', 'c']" str(range(1,5)) --> 'range(1, 5)' str(("a","b")) --> "('a', 'b')" #range()-->String #6 string = "" for i in range(1,5): string+=str(i) string --> '1234'{}
Set set(anthor_type(Tuple/String/List))
空集合的创建不能用{},应该用set()
dict={} type(dict) --> <class 'dict'> set=set() type(set) --> <class 'set'> set={1,2,3} set((1,2,3)) --> {1,2,3} set([1,2,3]) --> {1,2,3} set("hello") --> {'l', 'o', 'e', 'h'} set(range(1,6)) --> {1, 2, 3, 4, 5}Set Set1.intersection(Set2)
Set set1&set2 --> 交集
void Set1.intersection_update(Set2)
Set Set1.union(Set2)
Set set1|set2 --> 并集
void Set1.union(Set2) --> 不存在
Set Set1.difference(Set2) 差集(Set1中Set2不存在的元素)
Set set1-set2 --> 差集
Set Set1.symmetric_difference(Set2) --> 双向差集,两个集合中都不存在的元素
void Set1.difference_update(Set2)
void Set1.symmetric_difference_update(Set2)
set1,set2 --> 两个Set合成为一个Tuple
set1={1,2,3,4,5} set2={4,5,6,7,8} #交集 set1.intersection(set2) --> {4, 5} #1 set1&set2 --> {4, 5} #2 set1.intersection_update(set2) #3 set1 --> {4, 5} #并集 set1={1,2,3,4,5} set1.union(set2) --> {1, 2, 3, 4, 5, 6, 7, 8} #4 set1|set2 --> {1, 2, 3, 4, 5, 6, 7, 8} #5 #差集 set1.difference(set2) --> {1, 2, 3} #7 set1-set2 --> {1, 2, 3} #8 set1.symmetric_difference(set2) --> {1, 2, 3, 6, 7, 8} #9 set1.difference_update(set2) #10 set1 --> {1,2,3} set1={1,2,3,4,5} set1.symmetric_difference_update(set2) #11 set1 --> {1, 2, 3, 6, 7, 8} #合并为tuple set1={1,2,3,4,5} set2={4,5,6,7,8} st1,set2 --> ({1, 2, 3, 4, 5}, {4, 5, 6, 7, 8}) #12Bool Set1==Set2
Bool Set1.issubset(Set2) --> Set1是不是Set2的子集
Bool Set1.issuperset(Set2) --> Set1是否为Set2的父集
Bool Set1.isdisjoint(Set2) --> Set1和Set2中是否有重复元素
# 2 set1={1,2} set2={1,2,3} set1.issubset(set2) --> True set1={1,2,5} set1.issubset(set2) --> False # 3 set1={1,2,3,4} set2={2,3} set1.issuperset(set2) --> True set2={7,8} set1.issuperset(set2) --> False # 4 set1={1,2,3,4,5} set2={6,7,8,9} set1.isdisjoint(set2) --> True set1={6,7} set1.isdisjoint(set2) --> Falsevoid Set1.add(element)
void Set1.update(Tuple/List/Dict)
不可直接改
void Set.remove(element) --> 不存在报错
void Set.discard(element) --> 不存在不报错
set1={1,2,3,4,5} set1.add(0) #1 set1 --> {0, 1, 2, 3, 4, 5} set1={1,2,3,4,5} set1.update([6,7,8]) #2 set1 --> {1, 2, 3, 4, 5, 6, 7, 8} set1={1,2,3,4,5} set1.update((6,7,8)) #2 set1 --> {1, 2, 3, 4, 5, 6, 7, 8} set1={1,2,3,4,5} set1.update({6,7,8}) #2 set1 --> {1, 2, 3, 4, 5, 6, 7, 8} set1={1,2,3,4,5} set1.update(range(10,16)) #2 set1 --> {1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15} set1={1,2,3,4,5} set1.remove(1) #4 set1 --> {2, 3, 4, 5} set1.remove(0) -->KeyError #4 set1 --> {1,2,3,4,5} set1.discard(1) #5 set1 --> {2, 3, 4, 5} set1.discard(0) #5列表生成式 List=[i*10 for i in range(10,20)... if i%2 == 0...]
list=[i*j for i in range(1,5) for j in range(1,5) if i%2==0] list Result: [2, 4, 6, 8, 4, 8, 12, 16]字典生成式 Dict={i*10 for i in range(10,20)... if i%2 == 0...}
fruits=["apple","banana","orange"] dict={i:fruits[i] for i in range(0,len(fruits))} dict Result: {0: 'apple', 1: 'banana', 2: 'orange'}集合生成式 Set={i*10 for i in range(10,20)... if i%2 == 0...}
set={i*j for i in range(1,5) for j in range(1,5) if i%2==0} set Result: {2, 4, 6, 8, 12, 16}