|
String (immutable type) |
List (mutable type) |
Tuples (immutable types) |
Dictionary (mutable type) |
Collection (mutable type) |
Characteristics |
Similar to list, with subscript index, which is an iterative object |
An ordered set of |
Read-only List |
A collection of unique key-value relationship mappings in Python. Keys are unique and immutable types |
The elements within the set must be immutable data types, with their own deduplication |
Create |
S=str (); s= "ABC"; |
Li=[1,]; Li=list (()); |
tu= (1,); Tu=tuple ([+]); |
Dic={1: "A", 2: "B"}; Dic=dict (); |
s={1,2,3}; S=set ([+]); |
query |
s[1:7:2]; S.index (' A ', 1,6); S.find (' A ', 1,6); "A" in S S.count ("Elemente") Len (s) |
li[index]; Li[1:7:2]; li.index["Elemente"]; Li.count ("Elemente"); Len (LI); "Elemente" in Li |
tu[index]; Tu[1:7:2]; tu.index["Elemente"]; Tu.count ("Elemente"); Len (TU); "Elemente" in Tu |
dic["key"]; Dic.get (key); #找不到返回默认值 (None, modifiable) Dic.keys (); #Iterable Dic.values (); #Iterable "Key" in DIC; Len (DIC) |
s.issubset (S1); #等同s <s1 S.issuperset (S1); #等同s >s1 S.isdisjoint (S1); #s和s1有没有交集 "Elemente" in S Len (s) |
Increase |
|
Li.insert (Index, "Elemente"); Li.append ("Elemente"); Li.extend ([+]); |
|
dic["Key"]= "value"; Dic.setdefault (Key,value); Dic.update (DIC2) |
S.add ("element"); |
Delete |
Li.pop (index); Li.remove ("element"); Del Li[index]; |
|
Dic.pop ("key"); Del dic["Key"]; Dic.popitem (); #随机删除 |
S.remove ("element"); S.discard ("element"); #删除不存在的元素也不会报错 S.pop () #随机删除 |
Modify |
li["element"]=new_element; |
|
dic["key"]= "value" |
|
Other |
Strip # Split Join #合并容量里的元素, return string %s%d%f + Format Format_map Replace Count capitalize# First character uppercase Lower Upper Center, Ljust, rjust# align Zfill #用0填充 Ord ("A") Chr (65) |
A=a+b #合并 Li.sort () Li.reverse () Copy Extend |
|
Fromkeys #快速生成字典 Items Copy |
Intersection & #交集 Intersection_update Union | #并集 Update difference-#差集 A Yes, b not. Difference_update symmetric_difference ^ #对称差集: A and B are not printed on each other
|
Summarize:
1. String and container can calculate the length Len (), check whether an element belongs to (in)
2. Only the list and dictionary have copy ().
3. Tuples and strings are immutable, and the other 3 are mutable.
4. Container and String objects do not use the __dict__ property.
Python String and container summary