标签:元素查找 数据 python 切片 报错 方法 3.5 输入 lag
中括号,已逗号为分割,可以方任意得数据类型,甚至是对象
li = [‘wangys‘,‘lala‘,123,[1,2,3],{‘name‘:‘wangys‘}]
li = [‘wangys‘,‘lala‘,123,[1,2,3],{‘name‘:‘wangys‘}]
l1=li[0]
print(l1,type(l1))
l2=li[2]
print(l2,type(l2))
l3=li[3]
print(l3,type(l3))
li = [‘wangys‘,‘lala‘,123,[1,2,3],{‘name‘:‘wangys‘}]
l4=li[0:2]
print(l4,type(l4))
增加元素有三种操作
li = [‘wangys‘,‘gongzhu‘]
li.append(‘caomei‘)
li.append(‘1‘)
print(li)
# 需求: 员工列表,往员工列表里添加员工,可以持续添加,当输入q时退出
people = [‘wangys‘,‘huisz‘]
flag = True
while flag:
add_people = input("请输入员工姓名:").strip()
if add_people.upper() == ‘Q‘:
flag = False
else:
people.append(add_people)
print(people)
name = [‘wangys‘,‘chaoyf‘]
name.insert(1,‘huisz‘)
print(name)
整型是不可迭代对象
name = [‘wangys‘,‘chaoyf‘]
name.extend(‘lala‘)
print(name)
name.extend([1,2,3])
print(name)
1: pop 有返回值,该返回值是被删除的数据
2:按索引删除
3:不指定索引,把最后一个元素删除
name = [‘wangys‘,‘lala‘]
delete_name = name.pop()
print(delete_name,name)
name = ["wangys","haha"]
name.pop(0)
print(name)
name = ["wangys","hehe"]
print(name.pop())
name = [‘wangys‘,‘dede‘]
name.remove(‘dede‘)
print(name)
name = ["wangys","dsad"]
name.clear()
print(name)
name = [‘wangys‘]
del name
print(name)
name = [‘wangys‘,"lala",‘haha‘]
del name[1:]
print(name)
是按照索引修改,插入什么就是什么,单个元素插入
name = ["wangys","haha"]
name[1] = ‘xiaogongzhu‘
print(name)
name = ["wangys","sdfsdf"]
name[1] = [‘xiaogongzhu‘,‘chaomei‘]
print(name)
1:首先把切片元素删除
2:将添加元素以迭代方式插入
name = ["wangys","lala"]
name[1:] = ‘haha‘
print(name)
name = ["wangys","lala"]
name[1:] = ["xiaogongzhu","chaomei"]
print(name)
name = [‘wangys‘,‘xiaogongzhu‘]
for i in name:
print(i)
查出来的是一个列表
name = [‘wangys‘,‘xiaogongzhu‘]
print(name[:2])
wangys_info = ["wangys",23,‘male‘]
l = len(wangys_info)
print(l)
num = [1,2,3,1,2,4]
CountOf1=num.count(1)
print(CountOf1)
如果元素不存在报错
wangys_info = ["wangys",23,‘male‘]
print(wangys_info.index(‘male‘))
print(wangys_info.index(‘malea‘))
正向排序
num = [3,6,2,5,4]
num.sort()
print(num)
反向排序
num = [3,6,2,5,4]
num.sort(reverse=True)
print(num)
num = [3,6,2,5,4]
num.reverse()
print(num)
wangys_info = [‘wangys‘,23,‘male‘,[‘sing‘,‘play‘,22],25]
取出wangys里的a
wangys_info = [‘wangys‘,23,‘male‘,[‘sing‘,‘play‘,22],25]
print(wangys_info[0][1])
将列表的第一个元素大写并替换之前的元素
wangys_info = [‘wangys‘,23,‘male‘,[‘sing‘,‘play‘,22],25]
wangys_info[0] = wangys_info[0].capitalize()
print(wangys_info)
将王诚替换为王永胜
wangys_info = [‘wangys‘,23,‘王诚‘,[‘sing‘,‘play‘,22],25]
wangys_info[2] = wangys_info[2].replace(‘诚‘,‘永胜‘)
print(wangys_info)
wangys_info = [‘wangys‘,23,‘王诚‘,[‘sing‘,‘play‘,22],25]
wangys_info[3][1] = wangys_info[3][1].upper()
print(wangys_info)
标签:元素查找 数据 python 切片 报错 方法 3.5 输入 lag
原文地址:https://www.cnblogs.com/wc89/p/10357731.html