1.튜플
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<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass def __mul__(self, *args, **kwargs): # real signature unknown """ Return self*value.n """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __rmul__(self, *args, **kwargs): # real signature unknown """ Return self*value. """ pass tuple
3. 튜플의 일부 기능적 속성 소개
튜플과 리스트는 매우 유사하지만 튜플의 요소는 수정할 수 없으므로 목록에 있는 많은 기능을 튜플에서 사용할 수 없습니다.
1) count(self, value):
튜플에 있는 값 요소의 수를 계산하고 int 값을 반환합니다.
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 'tuple'> 3 <class 'int'> demo
2)index(self, value, start=None, stop=None):
index에서 값 요소를 찾습니다. 튜플 처음 나타나는 위치, 시작 및 중지 매개변수는 검색의 시작 및 끝 위치입니다. 기본값은 None이고 이 요소가 검색에 포함되지 않으면 ValueError: 'f'가 반환되지 않습니다. 튜플이 반환됩니다.
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 'int'> demo
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 'tuple'> (1, 2, 3, 4, 5, 1) <class 'tuple'> demo
4) contain(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
2. 사전
1. 사전 표현
{"name":"olive","age":18}
사전 만들기:
a={"name":"olive","age":18} b=dict({"name":"lusi","age":18})🎜>
2. 사전의 기능적 속성rree
3. 사전의 일부 기능적 속성 소개
1) 클리어(self):
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<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __setitem__(self, *args, **kwargs): # real signature unknown """ Set self[key] to value. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass __hash__ = None dict
2) copy(self):
얕은 복사본에 해당하는 튜플을 복사합니다.a={"name":"olive","age":18} b=a.clear() print(a) print(b) #运行结果 {} None
3) fromkeys(*args, **kwargs):[fromkeys(seq,value=None)]
seq를 키로 사용하여 새 사전을 만듭니다. 사전의 값은 사전의 값이고 기본값은 없음입니다. 동일한 값의 사전을 만드는 데 적합합니다.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
4) get(self, k, d=None):
사전에서 키 k를 사용하여 값을 가져옵니다. , k가 사전에 포함되어 있지 않으면 d의 값이 주어지고 d의 기본값은 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是重复的,但是创建的键只取一个。
5) items(self):
사전을 순회하여 각 키와 값 쌍을 결합하는 방법 사전 A 튜플을 사용하고 이러한 튜플을 목록과 같은 dict_items로 반환합니다.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
6)keys(self):
사전 키를 순회하고 다음과 동일하게 사용되는 목록과 같은 dict_keys를 반환하는 메서드입니다. 아이템 방식.
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 'dict_items'>
7) value(self):
사전 값 값을 순회하고 목록과 같은 dict_values를 반환하는 메서드 , 사용법은 아이템 방법과 동일합니다.
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 'dict_keys'>
8) pop(self, k, d=None):
및 get 메소드 get이 사전에서 키 k로 값을 가져오는 반면, pop은 사전에서 키 k로 값을 가져오는 것을 제외하면 사용법은 비슷합니다. 키 k가 사전에 포함되지 않고 d가 기본값이 아닌 경우 얻은 값은 d 값입니다. d가 기본값 None이면 KeyError가 보고됩니다. 아아앙
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 'tuple'>
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!