标签:位置 none 字符串转换 pop new 修改 迭代 切片 inf
中括号( [ ] )括起来,如 li = [ 1, 2, 3 ],以逗号分隔每个元素。
列表中的元素可以是数字、字符串、列表、元组、布尔值、字典以及集合,所有的都能放进去。
li = [ 1 , 2 , 3] print(li[0]) # 结果:1
列表的切片结果也是列表
li = [ 1 , 2 , 3, 4, 5 , 6] print( li[3:-1]) # 结果:[4, 5] 大于等于3,小于-1, 左闭右开
li = [ 1 , 2 , 3, 4, 5 , 6] for item in li: print(item)
# li = [1, 12, 9, "age", [ ["19", 10], (1, 2 )], "alex", True] # li[1] = 120 # print(li) # li[1] = [11,22,33,44] # print(li)
# li = [1, 12, 9, "age", [ ["19", 10], (1, 2 )], "alex", True] # li[1:3] = [120,90] # print(li)
# li = [1, 12, 9, "age", [ ["19", 10], (1, 2 )], "alex", True] # 删除,第一种方式 # del li[1] # print(li) # 切片删除 # del li[2:6] # print(li)
# li = [1, 12, 9, "age", [ ["19", 10], (1, 2 )], "alex", True] # v1 = 2 in li # print(v1) # v2 = "age" in li # print(v2)
# li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True] # li[4][1][0] # [1]
# 字符串转换列表 li = list("asdfasdfasdf"), 内部使用for循环 # s = "pouaskdfauspdfiajsdkfj" # new_li = list(s) # print(new_li)
# 列表转换成字符串, # 需要自己写for循环一个一个处理: 既有数字又有字符串 # li = [11,22,33,"123","alex"] # # r = str(li) # ‘[11,22,33,"123","alex"]‘ # # print(r) # s = "" # for i in li: # s = s + str(i) # print(s) # 直接使用字符串join方法:列表中的元素只有字符串 # li = ["123","alex"] # v = "".join(li) # print(v)
### 补充:字符串创建后,不可修改 # v = "alex" # v = v.replace(‘l‘,‘el‘) # print(v) # li = [11,22,33,44] # li[0] # li[0] = 999 # s = "alex" # li[0] # s[0] = "E" # 列表,有序;元素可以被修改
# li = [11, 22, 33, 22, 44] # 1. append 原来值最后追加 # 对象.方法(..) # li对象调用append方法 # li.append(5) # li.append("alex") # li.append([1234,2323]) # print(li) # 2. clear 清空列表 # li.clear() # print(li) # 3. copy 拷贝,浅拷贝 # v = li.copy() # print(v) # 4. count 计算元素出现的次数 # v = li.count(22) # print(v) # 5. append extend 扩展原列表,参数:可迭代对象 # 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) # 6. index 根据值获取当前值索引位置(左边优先) # li = [11, 22, 33, 22, 44] # v= li.index(22) # print(v) # 7. insert 在指定索引位置插入元素 # li = [11, 22, 33, 22, 44] # li.insert(0,99) # print(li) # 8、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) # 9. remove 删除列表中的指定值,左边优先,只删除第一个 # li = [11, 22, 33, 22, 44] # li.remove(22) # print(li) # PS: pop remove del li[0] del li[7:9] clear # 10. reverse 将当前列表进行翻转 # li = [11, 22, 33, 22, 44] # li.reverse() # print(li) # 11. sort 列表的排序 # li = [11,44, 22, 33, 22] # li.sort() # li.sort(reverse=True) # print(li)
tuple 元组,元组元素不可被修改,不能被增加或者删除(指元组的一级元素,元组中包含的列表,列表中的元素可以修改)
如:tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
一般写元组的时候,推荐在最后加入逗号(,),元组用小括号括起来。
元组几乎支持和列表同样的操作,除了元组的一级元素不能被修改,如索引、切片、for循环、转换格式等都能进行操作。
# tu = (11,22,33,44) # tu.count(22) # 获取指定元素在元组中出现的次数 # tu.index(22) # 索引取值
字典(dict) 字典由键值对(key value)组成,用大括号括起来,键值对用冒号(:)链接。
# info = { # "k1": "v1", # 键值对 # "k2": "v2" # }
# info = { # "k1": 18, # "k2": True, # "k3": [ # 11, # [], # (), # 22, # 33, # { # ‘kk1‘: ‘vv1‘, # ‘kk2‘: ‘vv2‘, # ‘kk3‘: (11,22), # } # ], # "k4": (11,22,33,44) # } # print(info)
# info ={ # 2: ‘asdf‘, # "k1": ‘asdf‘, # True: "123", # # [11,22]: 123 # (11,22): 123, # # {‘k1‘:‘v1‘}: 123 # } # print(info)
当有数字1和布尔值True同时作为key时,字典只保留一个键值对。
# info = { # "k1": 18, # "k2": True, # "k3": 3 # } # 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)
# 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)
# 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: # 默认只输出key值 # print(item) # # for item in info.keys(): # 指定只输出key值 # print(item) # for item in info.values(): # 指定指输出value值 # print(item) # for item in info.keys(): # 同时输出key、value # print(item,info[item]) # for k,v in info.items(): # python内置items方法同时输出key、value # print(k,v)
# dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # 1 fromkeys 根据序列,创建字典,并指定统一的值 # v = dict.fromkeys(["k1",123,"999"],123) # print(v) # 2 get 根据Key获取值,key不存在时,可以指定默认值(None) # v = dic[‘k11111‘] # print(v) # v = dic.get(‘k1‘,111111) # print(v) # 3 pop popitem 删除并获取值 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # v = dic.pop(‘k1‘,90) # print(dic,v) # k,v = dic.popitem() # print(dic,k,v) # 4 setdefault 设置值 # 已存在,不设置,获取当前key对应的值 # 不存在,设置,获取当前key对应的值 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # v = dic.setdefault(‘k1111‘,‘123‘) # print(dic,v) # 5 update 更新 # dic = { # "k1": ‘v1‘, # "k2": ‘v2‘ # } # dic.update({‘k1‘: ‘111111‘,‘k3‘: 123}) # print(dic) # dic.update(k1=123,k3=345,k5="asdf") # print(dic) # 6 keys() 7 values() 8 items() get update
标签:位置 none 字符串转换 pop new 修改 迭代 切片 inf
原文地址:https://www.cnblogs.com/yangyangjia/p/12344444.html