python 字符串和容器总结
生活随笔
收集整理的這篇文章主要介紹了
python 字符串和容器总结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
| ? | 字符串(不可變類型) | 列表(可變類型) | 元組(不可變類型) | 字典(可變類型) | 集合(可變類型) |
| 特性 | 和列表類似,有下標(biāo)索引,是可迭代對(duì)象 | 一種有序的集合? | 只讀的列表 | python中唯一的鍵值關(guān)系映射集合。鍵唯一且為不可變類型 | set內(nèi)的元素一定是不可變數(shù)據(jù)類型,且自帶去重 |
| 創(chuàng)建 | 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); #找不到返回默認(rèn)值(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有沒(méi)有交集 "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();#隨機(jī)刪除 | s.remove("element"); s.discard("element");#刪除不存在的元素也不會(huì)報(bào)錯(cuò) s.pop() #隨機(jī)刪除 | |
| 修改 | li["element"]=new_element; | ? | dic["key"]="value" | ? | |
| 其他 | strip # split? join #合并容量里的元素,返回字符串 %s ? %d ? %f + format format_map replace count capitalize#首字符大寫(xiě) lower upper center、ljust、rjust#對(duì)齊 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沒(méi)有的 difference_update symmetric_difference ?^ ? ?#對(duì)稱差集:?A和B互相不在的都打印 ? |
?
總結(jié):
1. 字符串和容器都可以計(jì)算長(zhǎng)度len(),?檢查某個(gè)元素是否屬于(in)
2. 只有列表和字典有copy()。
3. 元組和字符串不可變,其他3個(gè)都可變。
4.容器和字符串的對(duì)象,都沒(méi)用__dict__屬性。
轉(zhuǎn)載于:https://www.cnblogs.com/yangzhenwei123/p/6759216.html
總結(jié)
以上是生活随笔為你收集整理的python 字符串和容器总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jmeter分布式测试配置
- 下一篇: 第二十六讲:基础一开放封闭原则