目录
下标
name_list = ['Tom', 'Lily', 'Rose']
print(name_list[0]) # Tom
print(name_list[1]) # Lily
print(name_list[2]) # Rose
index,count,len
index() count() len()有角标的都有此方法
其中js中index()传入的是角标 python是某个具体的值 且有开始结尾范围
count()传入的是某个具体的值 且有开始结尾范围 出现的次数
name_list = ['Tom', 'Lily', 'Rose']
print(name_list.index('Tom')) # 0
print(name_list.count('Tom')) # 1
print(len(name_list)) # 3
in ,not in 判断是否存在
name_list = ['Tom', 'Lily', 'Rose']
print('tom' in name_list) # False
print('tom' not in name_list) # True
类似js的for in 循环
列表的增加
append 更改了原数组
name_list = ['Tom', 'Lily', 'Rose']
name_list.append('zndroid')
print(name_list) # ['Tom', 'Lily', 'Rose', 'zndroid']
name_list = ['Tom', 'Lily', 'Rose']
name_list.append('zndroid')
name_list.append(['haha','hehe'])
print(name_list) # ['Tom', 'Lily', 'Rose', 'zndroid', ['haha', 'hehe']]
extend 继承添加 适合类型一致的数据类型追加
name_list = ['Tom', 'Lily', 'Rose']
name_list.extend('zndroid')
name_list.extend(['haha','hehe'])
print(name_list)
# ['Tom', 'Lily', 'Rose', 'z', 'n', 'd', 'r', 'o', 'i', 'd', 'haha', 'hehe']
insert(下标,数据) 插入数据
name_list = ['Tom', 'Lily', 'Rose']
name_list.insert(0,'zndroid')
print(name_list)
# ['zndroid', 'Tom', 'Lily', 'Rose']
删除
del 删除指定位置的数据 del 列表
name_list = ['Tom', 'Lily', 'Rose']
del name_list[0]
print(name_list)
#['Lily', 'Rose']
pop 删除指定位置的数据 默认删除最后一位,并返回删除的数据 列表.pop(x)
name_list = ['Tom', 'Lily', 'Rose']
name=name_list.pop()
print(name)
print(name_list)
# Rose
# ['Tom', 'Lily']
remove 删除指定项
name_list = ['Tom', 'Lily', 'Rose']
name_list.remove('Tom')
print(name_list)
# ['Lily', 'Rose']
clear 清空数据
修改数据
角标修改
name_list = ['Tom', 'Lily', 'Rose']
name_list[0]='zndroid'
print(name_list)
# ['zndroid', 'Lily', 'Rose']
reverse 反着排
name_list = ['Tom', 'Lily', 'Rose']
name_list.reverse()
print(name_list)
# ['Rose', 'Lily', 'Tom']
sort() 排序 默认正序
name_list = ['Tom', 'Lily', 'Rose']
name_list.sort()
print(name_list)
# ['Lily', 'Rose', 'Tom']
name_list.sort(reverse=True)
print(name_list)
#['Tom', 'Rose', 'Lily']
copy 复制
name_list = ['Tom', 'Lily', 'Rose']
name=name_list.copy()
print(name)
#['Tom', 'Lily', 'Rose']
循环
简单循环
name_list = ['Tom', 'Lily', 'Rose']
i=0
while i< len(name_list):
print(name_list[i])
i+=1
for x in name_list:
print(x)