一、元組
1.元組的表達
(1,2,3,4) ('olive',123) ("python",)
建立元組:
a=tuple((1,2,3,)) b=("python",)
2.元組功能屬性
class tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. """ def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ Return key in self. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, *args, **kwargs): # real signature unknown """ Return self[key]. """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __hash__(self, *args, **kwargs): # real signature unknown """ Return hash(self). """ pass def __init__(self, seq=()): # known special case of tuple.__init__ """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<p style="text-align: left;"><strong>3.元組的部分功能屬性介紹</strong></p><p style="text-align: left;">元組和清單有很大相似性,但是元組的元素是不可修改的,所以很多列表有的功能元組都沒有。 </p><p style="text-align: left;"><span style="background-color: #00ffff">1)count(self, 值):</span></p><p style="text-align: left;">統計元組中包含value元素的數量,並傳回一個int值。 </p><pre class="brush:php;toolbar:false">a=(1,2,3,4,1,2,3,1,2,) b=a.count(1) print(a,type(a)) print(b,type(b)) #运行结果 (1, 2, 3, 4, 1, 2, 3, 1, 2) <class> 3 <class> demo</class></class>
2)index(self, value, start=None, stop=None):
索引,查找元組中value元素第一個出現的位置,start與stop參數是查找起始與結束位置,預設為None,傳回int數值,如果查找中不包含這個元素,則傳回ValueError: 'f' is not in tuple報錯。
a=(1,2,3,4,1,2,3,1,2,) b=a.index(3) print(a,len(a)) print(b,type(b)) #运行结果 (1, 2, 3, 4, 1, 2, 3, 1, 2) 9 2 <class> demo</class>
3)add(self, *args, **kwargs):
為元組新增一個新的元素,新增的新元素需要以元組的形式添加,產生一個新的元組。
a=(1,2,3,4) b=a.__add__((5,1)) #括号理给出的必须是元组 print(a,type(a)) print(b,type(b)) #运行结果 (1, 2, 3, 4) <class> (1, 2, 3, 4, 5, 1) <class> demo</class></class>
4)contains(self, *args, **kwargs):
判斷元組中是否包含某個元素,返回布林值。
a=(1,2,3,4,1,2,3,1,2,) b=a.__contains__(2) c=a.__contains__(5) print(a) print(b) print(c) #运行结果 (1, 2, 3, 4, 1, 2, 3, 1, 2) True False demo
二、字典
1.字典的表達
{"name":"olive","age":18}
建立字典:
a={"name":"olive","age":18} b=dict({"name":"lusi","age":18})
2.字典功能屬性
class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ def clear(self): # real signature unknown; restored from __doc__ """ D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(*args, **kwargs): # real signature unknown """ Returns a new dict with keys from iterable and values equal to value. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def items(self): # real signature unknown; restored from __doc__ """ D.items() -> a set-like object providing a view on D's items """ pass def keys(self): # real signature unknown; restored from __doc__ """ D.keys() -> a set-like object providing a view on D's keys """ pass def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised """ pass def popitem(self): # real signature unknown; restored from __doc__ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass def update(self, E=None, **F): # known special case of dict.update """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ pass def values(self): # real signature unknown; restored from __doc__ """ D.values() -> an object providing a view on D's values """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ True if D has a key k, else False. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown """ Delete self[key]. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) x[y] """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__ """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self size of D in memory, in bytes """ pass __hash__ = None dict
3.字典的部分功能屬性介紹
1)clear(self):
清除字典中的所有元素。
a={"name":"olive","age":18} b=a.clear() print(a) print(b) #运行结果 {} None
2)copy(self):
複製一份元組,相當於一次淺拷貝。
a={"name": "olive","age":18} b=a.copy() print(a,id(a),id("name")) print(b,id(b),id("name")) #赋值 c={"name": "lusi","age":18} d=c print(c,id("name")) print(d,id("name")) #浅拷贝 e={"name": "shy","age":18} f=copy.copy(e) print(e,id(e),id("name")) print(f,id(f),id("name")) #运行结果 {'name': 'olive', 'age': 18} 2915224 2019840 {'name': 'olive', 'age': 18} 2915304 2019840 {'name': 'lusi', 'age': 18} 2019840 {'name': 'lusi', 'age': 18} 2019840 {'name': 'shy', 'age': 18} 5584616 2019840 {'name': 'shy', 'age': 18} 5586056 2019840
3)fromkeys(*args, **kwargs):【fromkeys(seq,value=None)】
建立一個新的字典,以seq為字典的keys(鍵),value為字典的值,預設為None。適合建立一個一樣值的字典。
a={"hunan": "changsha","guangdong":"guangzhou","jiangsu":"nanjing",'hubei':"wuhan"} b=dict.fromkeys(a,"good") c=dict.fromkeys(["a","b","c"],"abc") d=dict.fromkeys("abcc") print(a) print(b) print(c) print(d) #运行结果 {'guangdong': 'guangzhou', 'hubei': 'wuhan', 'hunan': 'changsha', 'jiangsu': 'nanjing'} {'hubei': 'good', 'guangdong': 'good', 'hunan': 'good', 'jiangsu': 'good'} {'c': 'abc', 'b': 'abc', 'a': 'abc'} {'c': None, 'b': None, 'a': None} #seq给出的字符串c是重复的,但是创建的键只取一个。
4)get(self, k, d=None):
取得字典中鍵為k的值,如果字典中不包含k,則給出d值,d預設為None。
a={"a":1,"b":2,"c":3,"d":4} b=a.get("a") c=a.get("e") d=a.get("e",5) print(a) print(b) print(c) print(d) #运行结果 {'b': 2, 'a': 1, 'c': 3, 'd': 4} 1 None 5
5)items(self):
遍歷字典的一個方法,把字典中每對key和value組成一個元組,並把這些元組放在一個類似列表的dict_items中返回。
a={"a":1,"b":2,"c":3,"d":4} b=a.items() print(a) print(b,type(b)) #运行结果 {'d': 4, 'c': 3, 'a': 1, 'b': 2} dict_items([('d', 4), ('c', 3), ('a', 1), ('b', 2)]) <class></class>
6)keys(self):
#遍歷字典鍵keys的一個方法,傳回一個類似列表的dict_keys,與items方法用法相同。
a={"a":1,"b":2,"c":3,"d":4} b=a.keys() print(a) print(b,type(b)) #运行结果 {'b': 2, 'a': 1, 'c': 3, 'd': 4} dict_keys(['b', 'a', 'c', 'd']) <class></class>
7)values(self):
#遍歷字典值value的一個方法,傳回一個類似列表的dict_values,與items方法用法相同。
a={"a":1,"b":2,"c":3,"d":4} b=a.values() print(a) print(b,type(b)) #运行结果 {'c': 3, 'd': 4, 'b': 2, 'a': 1} dict_values([3, 4, 2, 1]) <class></class>
8)pop(self, k, d=None):
和get方法用法相似,只不過,get是取得字典中鍵為k的值,而pop則是取出字典中鍵為k的值。當字典中不含鍵k時,d不是預設值時,取到的值就為d值,如果d為預設值None時,則KeyError報錯。
a={"a":1,"b":2,"c":3,"d":4} b=a.pop("a") c=a.pop("e","five") print(a) print(b,type(b)) print(c,type(c)) #运行结果 {'c': 3, 'd': 4, 'b': 2} 1 <class> five <class></class></class>
9)#popitem(self):
从字典中随机取出一组键值,返回一个新的元组。如果字典中无键值可取,则KeyError报错。
a={"a":1,"b":2,"c":3,"d":4} b=a.popitem() print(a) print(b,type(b)) #运行结果 {'d': 4, 'b': 2, 'a': 1} ('c', 3) <class></class>
10)setdefault(self, k, d=None):
从字典中获取键为k的值,当字典中包含键k值时,功能和get基本一致,当字典中不包含键k值时,在原字典上添加上键为k的初始键值对,并返回值d。
a={"a":1,"b":2,"c":3,"d":4} b=a.setdefault("a") c=a.setdefault("e") d=a.setdefault("f",6) print(a) print(b) print(c) print(d) #运行结果 {'f': 6, 'c': 3, 'a': 1, 'e': None, 'b': 2, 'd': 4} 1 None 6
11)update(self, E=None, **F):
给字典新增元素,没有返回值。用法:dict.update(dict2)。
a={"a":1,"b":2,"c":3,"d":4} b=a.update({"e":5}) print(a) print(b) #运行结果 {'c': 3, 'b': 2, 'd': 4, 'a': 1, 'e': 5} None
12)contains(self, *args, **kwargs):
判断列表中是否包含某个键值对,返回布尔值。用法:dict.contains(keys)。
a={"a":1,"b":2,"c":3,"d":4} b=a.__contains__("a") print(a) print(b) #运行结果 {'a': 1, 'd': 4, 'c': 3, 'b': 2} True
13)delitem(self, *args, **kwargs):
删除字典中的某个键值对,没有返回值。用法:dict.delitem(keys)。
a={"a":1,"b":2,"c":3,"d":4} b=a.__delitem__("a") print(a) print(b) #运行结果 {'c': 3, 'b': 2, 'd': 4} None
以上是python元組與字典的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。