python 字串和容器總結

來源:互聯網
上載者:User

標籤:list   計算   copy   tuple   ever   iter   ems   資料類型   填充   

 

  字串(不可變類型) 列表(可變類型) 元組(不可變類型) 字典(可變類型) 集合(可變類型)
特性 和列表類似,有下標索引,是可迭代對象 一種有序的集合  唯讀列表 python中唯一的索引值關係映射集合。鍵唯一且為不可變類型 set內的元素一定是不可變資料類型,且內建去重
建立 s=str();
s="abc";
li=[1,];
li=list((1,2,3));
tu=(1,);
tu=tuple([1,2,3]);
dic={1:"a",2:"b"};
dic=dict();
s={1,2,3};
s=set([1,2,3]);
查詢 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,可修改)
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)

增加   li.insert(index,"elemente");
li.append("elemente");
li.extend([1,2,3]);
  dic["key"]="value";
dic.setdefault(key,value);
dic.update(dic2)
s.add("element");
刪除 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() #隨機刪除
修改 li["element"]=new_element;   dic["key"]="value"  
其他 strip #
split 
join #合并容量裡的元素,返回字串
%s   %d   %f
+
format
format_map
replace
count
capitalize#首字元大寫
lower
upper
center、ljust、rjust#對齊
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有,B沒有的
difference_update
symmetric_difference  ^    #對稱差集: A和B互相不在的都列印


 

 

總結:

1. 字串和容器都可以計算長度len(), 檢查某個元素是否屬於(in)

2. 只有列表和字典有copy()。

3. 元組和字串不可變,其他3個都可變。

4.容器和字串的對象,都沒用__dict__屬性。

python 字串和容器總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.