标签:默认 rev 循环 布尔值 获取当前值 bsp copy 迭代 索引
list
[]中括号括起来,逗号,分割每个元素,列表元素可以是数字,字符串,列表,布尔值
所以都能放进去
索引,切片
test=["ds","df",433,["dfsdf"],"ds"]
del test[1]# 删除
print(test)
test[0]=[12,4,5,5]#修改
print(test)//[‘ds‘, 433, [‘dfsdf‘], ‘ds‘],[[12, 4, 5, 5], 433, [‘dfsdf‘], ‘ds‘]
数字无法for循环
字符串转换列表,内部使用for循环
test=["ds","df","433","dfsdf","ds"]
v=" ".join(test)#只有字符串join ,有数字无法for循环
print(v)//ds df 433 dfsdf ds
s=" "
for i in test:
s+=str(i)
print(s)//dsdf433dfsdfds
test=["ds","df",433,"dfsdf","ds"]
test.append(12)#在追加元素
print(test)//[‘ds‘, ‘df‘, 433, ‘dfsdf‘, ‘ds‘, 12]
2 清空列表
# li.clear()
# print(li)
# 3 拷贝,浅拷贝
# v = li.copy()
# print(v)
# 4. 计算元素出现的次数
# v = li.count(22)
# print(v)
# 5. 扩展原列表,参数:可迭代对象
# 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, ‘不得了‘]
# 6. 根据值获取当前值索引位置(左边优先)
# li = [11, 22, 33, 22, 44]
# v= li.index(22)
# print(v)1
# 7. 在指定索引位置插入元素
# li = [11, 22, 33, 22, 44]
# li.insert(0,99)
# print(li)[99,..]
删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值
test=["ds","df",433,"dfsdf","ds"]
v=test.pop()
print(test)
print(v)
9. 删除列表中的指定值,左边优先
# li = [11, 22, 33, 22, 44]
# li.remove(22)
# print(li)
# PS: pop remove del li[0]
# 将当前列表进行翻转
li = [11, 22, 33, 22, 44]
li.reverse()
print(li)//[44, 22, 33, 22, 11]
# 11 列表的排序
# li = [11,44, 22, 33, 22]
# li.sort()//[11, 22, 22, 33, 44]
li.sort(reverse=True)
print(li)//[44, 33, 22, 22, 11]
# 补充:字符串创建后,不可修改
# 列表,有序;元素可以被修改
标签:默认 rev 循环 布尔值 获取当前值 bsp copy 迭代 索引
原文地址:https://www.cnblogs.com/yikedashuyikexiaocao/p/9194305.html