li = [1, 12, 9, "age", ["石振文", ["19", 10], "龐麥郎"], "alex", True] # 經過list類建立的對象,li
列表可包含int str list list_in_list bool
列表元素,能夠被修改 li[1] = 120 或切片修改 li[0:1] = [120,11]
嵌套取值 li[4][2][1] = 10
pop刪除某個值(1.指定索引;2. 默認最後一個),並獲取刪除的值 # li = [11, 22, 33, 22, 44] # v = li.pop() # print(li) # print(v) # li = [11, 22, 33, 22, 44] # v = li.pop(1) # print(li) # print(v) remove 刪除列表中的指定值,左邊優先 # li = [11, 22, 33, 22, 44] # li.remove(22) # print(li) # PS: pop remove del li[0] del li[7:9] clear
# li = [11, 22, 33, 22, 44] # li.append([9898,"不得了"]) # [11, 22, 33, 22, 44, [9898, '不得了']] # 擴展原列表,參數:可迭代對象 # li.extend([9898,"不得了"]) # for i in [9898,"不得了"]: # li.append(i) # [11, 22, 33, 22, 44, 9898, '不得了'] # # li.extend("不得了") # print(li)
# 將當前列表進行翻轉 # li = [11, 22, 33, 22, 44] # li.reverse() # print(li) # 列表的排序 # li = [11,44, 22, 33, 22] # li.sort() # li.sort(reverse=True) # print(li) ### 欠 # cmp # key # sorted
class list(object): """ list() -> new empty list list(iterable) -> new list initialized from iterable's items """ def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -- append object to end """ pass def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -- extend list by appending elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass def pop(self, index=None): # real signature unknown; restored from __doc__ """ 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 def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 """ pass def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __delslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__delslice__(i, j) <==> del x[i:j] Use of negative indices is not supported. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __iadd__(self, y): # real signature unknown; restored from __doc__ """ x.__iadd__(y) <==> x+=y """ pass def __imul__(self, y): # real signature unknown; restored from __doc__ """ x.__imul__(y) <==> x*=y """ pass def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __reversed__(self): # real signature unknown; restored from __doc__ """ L.__reversed__() -- return a reverse iterator over the list """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ """ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ L.__sizeof__() -- size of L in memory, in bytes """ pass __hash__ = None
字符串--列表互相轉換html
# 字符串轉換列表 li = list("asdfasdfasdf"), 內部使用for循環 # s = "pouaskdfauspdfiajsdkfj" # new_li = list(s) # print(new_li) # 列表轉換成字符串, # 須要本身寫for循環一個一個處理: 既有數字又有字符串 # li = [11,22,33,"123","alex"] # s = "" # for i in li: # s = s + str(i) # print(s) # 直接使用字符串join方法:列表中的元素只有字符串 # li = ["123","alex"] # v = "".join(li) # print(v)
# 元組,元素不可被修改,不能被增長或者刪除
# tuple
# tu = (11,22,33,44)
# tu.count(22),獲取指定元素在元組中出現的次數
####################################### 操做方法 #######################################
# 1. 書寫格式
# tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
# 通常寫元組的時候,推薦在最後加入 ,
# 元素不可被修改,不能被增長或者刪除
# 2. 索引
# v = tu[0]
# print(v)
# 3. 切片
# v = tu[0:2]
# print(v)
# 4. 能夠被for循環,可迭代對象
# for item in tu:
# print(item)
# 5. 轉換
# s = "asdfasdf0"
# li = ["asdf","asdfasdf"]
# tu = ("asdf","asdf")
#
# v = tuple(s)
# print(v)
# v = tuple(li)
# print(v)
# v = list(tu)
# print(v)
# v = "_".join(tu)
# print(v)
# li = ["asdf","asdfasdf"]
# li.extend((11,22,33,))
# print(li)
# 6.元組的一級元素不可修改/刪除/增長
# tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
# # 元組,有序。
# # v = tu[3][0][0]
# # print(v)
# # v=tu[3]
# # print(v)
# tu[3][0] = 567
## print(tu)
#以10做爲開始序號,依次輸出序號和對應值
tu = ('alex', 'eric', 'rain')
for idx, elem in enumerate(tu, 10):
print(idx, elem)
lass 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, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __hash__(self): # real signature unknown; restored from __doc__ """ x.__hash__() <==> hash(x) """ 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): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ T.__sizeof__() -- size of T in memory, in bytes """ pass tuple
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
經常使用操做:app
# 1 根據序列,建立字典,並指定統一的值
# v = dict.fromkeys(["k1",123,"999"],123)
# print(v)
# 2 根據Key獲取值,key不存在時,能夠指定默認值(None)
# v = dic['k11111']
# print(v)
# v = dic.get('k1',111111)
# print(v)
# 3 刪除並獲取值
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# v = dic.pop('k1',90)
# print(dic,v)
# k,v = dic.popitem() #隨機刪除鍵值對,返回對應值
# print(dic,k,v)
# 4 設置值,
# 已存在,不設置,獲取當前key對應的值
# 不存在,設置,獲取當前key對應的值
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# v = dic.setdefault('k1111','123')
# print(dic,v)
# 5 更新
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# dic.update({'k1': '111111','k3': 123}) 已存在的覆蓋,不存在的key更新上
# print(dic)
# dic.update(k1=123,k3=345,k5="asdf")
# print(dic)
# 最經常使用的有: 6 keys() 7 values() 8 items() get update
# 一、基本機構 # info = { # "k1": "v1", # 鍵值對 # "k2": "v2" # } #### 2 字典的value能夠是任何值 # info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info) #### 3 列表、字典不能做爲字典的key # info ={ # 1: 'asdf', # "k1": 'asdf', # True: "123", # # [11,22]: 123 # (11,22): 123, # # {'k1':'v1'}: 123 # # } # print(info) # 4 字典無序 # info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info) # 五、索引方式找到指定元素 # info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # # v = info['k1'] # # print(v) # # v = info[2] # # print(v) # v = info['k3'][5]['kk3'][0] # print(v) # 6 字典支持 del 刪除 # info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # del info['k1'] # # del info['k3'][5]['kk1'] # print(info) # 7 for循環 # dict # info = { # "k1": 18, # 2: True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # 'kk1': 'vv1', # 'kk2': 'vv2', # 'kk3': (11,22), # } # ], # "k4": (11,22,33,44) # } # for item in info: # print(item) # # for item in info.keys(): # print(item) # for item in info.values(): # print(item) # for item in info.keys(): # print(item,info[item]) # for k,v in info.items(): # print(k,v) # True 1 False 0 # info ={ # "k1": 'asdf', # True: "123", # # [11,22]: 123 # (11,22): 123, # # {'k1':' v1'}: 123 # # } # print(info)
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(S, v=None): # real signature unknown; restored from __doc__ """ dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ """ 根據key獲取值,d是默認值 """ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def has_key(self, k): # real signature unknown; restored from __doc__ """ 是否有key """ """ D.has_key(k) -> True if D has a key k, else False """ return False def items(self): # real signature unknown; restored from __doc__ """ 全部項的列表形式 """ """ D.items() -> list of D's (key, value) pairs, as 2-tuples """ return [] def iteritems(self): # real signature unknown; restored from __doc__ """ 項可迭代 """ """ D.iteritems() -> an iterator over the (key, value) items of D """ pass def iterkeys(self): # real signature unknown; restored from __doc__ """ key可迭代 """ """ D.iterkeys() -> an iterator over the keys of D """ pass def itervalues(self): # real signature unknown; restored from __doc__ """ value可迭代 """ """ D.itervalues() -> an iterator over the values of D """ pass def keys(self): # real signature unknown; restored from __doc__ """ 全部的key列表 """ """ D.keys() -> list of D's keys """ return [] 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__ """ 若是key不存在,則建立,若是存在,則返回已存在的值且不修改 """ """ 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 """ 更新 {'name':'alex', 'age': 18000} [('name','sbsbsb'),] """ """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, 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() -> list of D's values """ return [] def viewitems(self): # real signature unknown; restored from __doc__ """ 全部項,只是將內容保存至view對象中 """ """ D.viewitems() -> a set-like object providing a view on D's items """ pass def viewkeys(self): # real signature unknown; restored from __doc__ """ D.viewkeys() -> a set-like object providing a view on D's keys """ pass def viewvalues(self): # real signature unknown; restored from __doc__ """ D.viewvalues() -> an object providing a view on D's values """ pass def __cmp__(self, y): # real signature unknown; restored from __doc__ """ x.__cmp__(y) <==> cmp(x,y) """ pass def __contains__(self, k): # real signature unknown; restored from __doc__ """ D.__contains__(k) -> True if D has a key k, else False """ return False def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ 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): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass __hash__ = None dict
數字 字符串 列表 元組 字典 bool整理總結ide
####################### 整理 ################# # 1、數字 # int(..) # 2、字符串 # replace/find/join/strip/startswith/split/upper/lower/format # tempalte = "i am {name}, age : {age}" # # v = tempalte.format(name='alex',age=19) # v = tempalte.format(**{"name": 'alex','age': 19}) # print(v) # 3、列表 # append、extend、insert # 索引、切片、循環 # 4、元組 # 忽略 # 索引、切片、循環 以及元素不能被修改 # 5、字典 # get/update/keys/values/items # for,索引 # dic = { # "k1": 'v1' # } # v = "k1" in dic # print(v) # v = "v1" in dic.values() # print(v) # 6、布爾值 # 0 1 # bool(...) # None "" () [] {} 0 ==> False
集合(無序)this
見:PYTHON3集合spa