python内蔵データ操作---リストリスト、メタグループtuple、辞書dict、コレクション
23702 ワード
pythonには、コンテナ(container)と呼ばれるデータを格納するデータ構造がある.ここで、リスト、メタグループ、文字列はシーケンス(秩序性)に属し、辞書はマッピング(キーによるマッピング値)に属し、セット(set)は無秩序で自動的に重み付けされるデータ構造である.本編ではリスト、メタグループ、辞書、集合をまとめましたが、文字列の方法は前編を参照してください
一、リスト
pythonは弱い言語として、データの作成方法が簡単です.リストの作成方法は次のとおりです.
リストクラスの組み込み方法:
二、元グループ
メタグループの作成方法は次のとおりです.
メタグループの作成後に修正できない特性のため、その内蔵方法は少ない(削除できない、調べるしかない):
三、辞書
辞書(dict)の作成は次のとおりです.
辞書の組み込み方法:
四、集合
コレクション(set)の作成方法は次のとおりです.
コレクションの組み込み方法:
内蔵データ型の方法はいずれも下位層がC音声で実現されるため,pythonで書かれた同じ方法よりも実行効率が高い.プログラムを書くときは内蔵方法で、できるだけ自分で書かないで、実行時間を減らすことができます.
詳細の追加
To be continued...
転載先:https://www.cnblogs.com/by3333/p/7718080.html
一、リスト
pythonは弱い言語として、データの作成方法が簡単です.リストの作成方法は次のとおりです.
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
リストクラスの組み込み方法:
def append(self, p_object):
""" L.append(object) -> None -- append object to end """
pass
p_object , , 。
def clear(self):
""" L.clear() -> None -- remove all items from L """
pass
, , 。
def copy(self):
""" L.copy() -> list -- a shallow copy of L """
return []
。
def count(self, value):
""" L.count(value) -> integer -- return number of occurrences of value """
return 0
value 。
def extend(self, iterable):
""" L.extend(iterable) -> None -- extend list by appending elements from the iterable """
pass
iterable 。 iterable , ; iterable , 。 。
def index(self, value, start=None, stop=None):
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
[start:end] value , 。 start、end None , , value , ValueError 。
def insert(self, index, p_object):
""" L.insert(index, object) -- insert object before index """
pass
index p_object, L[index] == p_object。
def pop(self, index=None):
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass
index , 。 index , IndexError 。
def remove(self, value):
"""
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
value , value , VauleError 。
def reverse(self):
""" L.reverse() -- reverse *IN PLACE* """
pass
,
def sort(self, key=None, reverse=False):
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
pass
, 。
二、元グループ
メタグループの作成方法は次のとおりです.
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):
""" T.count(value) -> integer -- return number of occurrences of value """
return 0
value 。
def index(self, value, start=None, stop=None):
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
T[start:end] ,value , value , VauleError 。
三、辞書
辞書(dict)の作成は次のとおりです.
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):
""" D.clear() -> None. Remove all items from D. """
pass
, 。
def copy(self):
""" D.copy() -> a shallow copy of D """
pass
,
@staticmethod
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
key k , , d。 d None。
def items(self):
""" D.items() -> a set-like object providing a view on D's items """
pass
dict_items 、 , :[(key1, value1),(key2, value2),.....], 。 , 。 :
for key, value in dict.items():
print(key, value)
def keys(self):
""" D.keys() -> a set-like object providing a view on D's keys """
pass
dict_keys 、 , :[key1,key2,key3,....], 。 key :
if key in dict.keys():
print(key)
def pop(self, k, d=None):
"""
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
k , k 。 k , d , d;d None , KeyError 。
def popitem(self):
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
"""
pass
, :(key, value)。
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
k , k , k、 d (d None), d 。
def update(self, E=None, **F):
"""
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
E F 。 E keys() , :
for k in E:
D[k] = E[k]
keys() , :
for k, v in E:
D[k] = v
, :
for k in F:
D[k] = F[k]
def values(self):
""" D.values() -> an object providing a view on D's values """
pass
dict_values , [value1,value2, value3,...], 。
四、集合
コレクション(set)の作成方法は次のとおりです.
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
コレクションの組み込み方法:
def add(self, *args, **kwargs):
"""
Add an element to a set.
This has no effect if the element is already present.
"""
pass
, , 。
def clear(self, *args, **kwargs):
""" Remove all elements from this set. """
pass
, 。
def copy(self, *args, **kwargs):
""" Return a shallow copy of a set. """
pass
。
def difference(self, *args, **kwargs):
"""
Return the difference of two or more sets as a new set.
(i.e. all elements that are in this set but not the others.)
"""
pass
( , ) ( )。
def difference_update(self, *args, **kwargs):
""" Remove all elements of another set from this set. """
pass
( ), 。
def discard(self, *args, **kwargs):
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
pass
。 , 。
def intersection(self, *args, **kwargs):
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
pass
( )。
def intersection_update(self, *args, **kwargs):
""" Update a set with the intersection of itself and another. """
pass
。
def isdisjoint(self, *args, **kwargs):
""" Return True if two sets have a null intersection. """
pass
, True, False。
def issubset(self, *args, **kwargs):
""" Report whether another set contains this set. """
pass
。
def issuperset(self, *args, **kwargs):
""" Report whether this set contains another set. """
pass
。
def pop(self, *args, **kwargs):
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass
, 。 , KeyError 。
def remove(self, *args, **kwargs):
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
"""
pass
, , KeyError 。
def symmetric_difference(self, *args, **kwargs):
"""
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
"""
pass
( )。
def symmetric_difference_update(self, *args, **kwargs):
""" Update a set with the symmetric difference of itself and another. """
pass
。
def union(self, *args, **kwargs):
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
pass
( )。
def update(self, *args, **kwargs):
""" Update a set with the union of itself and others. """
pass
。
内蔵データ型の方法はいずれも下位層がC音声で実現されるため,pythonで書かれた同じ方法よりも実行効率が高い.プログラムを書くときは内蔵方法で、できるだけ自分で書かないで、実行時間を減らすことができます.
詳細の追加
To be continued...
転載先:https://www.cnblogs.com/by3333/p/7718080.html