Python列表操作

https://www.liaoxuefeng.com/wiki/1016959663602400/1017316949097888

#列表的创建
#变量名=[元素1,元素2,....]
list = ['a' , 'b' , 'd' , 1 , 2 , 3 , 4]
print(list)
list1 = []
print(list1)
#列表的取值
list = ['张三' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']
print(list)
#取值的语法: 变量 = 列表变量[索引值]
zhaoliu = list[3]
print(zhaoliu)
zhaoliu = list[-3]
print(zhaoliu)

#范围取值:列表变量 = 原来列表变量[起始索引:结束索引]
#在python中列表范围取值  是   左闭右开
list1 = list[1:4]
print(list1)
print(list1[-1])

#列表中的index函数用于获取制定元素的索引值
zhaoliu_index = list.index('赵六',)
print(zhaoliu_index)
#遍历列表
persons = ['张三' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']

count = len(persons)#获取列表长度
print(count)
#for循环用于遍历列表
#for 迭代变量 in 可迭代对象(列表)
i = 0
print(persons)
for p in persons:
    #print(p+" ",end="")
    if p == '赵六':
        ri = count * -1 + i
        print(p, i , ri)
    i += 1

i = 0
while i < len(persons):
    p = persons[i]
    if p == '赵六':
        ri = count * -1 + i
        print(p, i , ri)
    i += 1
#列表的反转与排序
persons = ['张三' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']
persons.reverse() #reverse 用于反转列表
print(persons)

numbers = [28,32,14,12,53,42]
numbers.sort()
print(numbers)
numbers.sort(reverse = True) #sort()用于排序,reverse = Ture 代表降序排列
print(numbers)
#列表的写操作
persons = ['张三' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']
#列表的追加
persons.append("杨九")
print(persons)
#列表的插入
persons.insert(2,"刘二")
print(persons)
persons.insert(len(persons),'候大')
print(persons)
# 列表的更新
persons[2] = '宋二'
print(persons)
# 列表范围取值是"左闭右开"
persons[3:5] = ['王五' , '李四']
print(persons)
#列表的删除操作
#按元素内容删除
persons.remove('宋二')
print(persons)
persons.pop(4)
print(persons)
persons[4:7] = []
print(persons)
# 其他常用方法
persons = ['张三' , '赵六' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']

#统计出现次数
cnt = persons.count('赵六')
print(cnt)
#追加操作
#append将整个列表追加到末尾,extend则是将列表中的元素追加到原始列表末尾
persons.append(['杨九' , '吴十'])
print(persons)
persons.extend(['杨九' , '吴十'])
print(persons)

#in(成员运算符) 运算符用于判断数据是否在列表中存在,存在反悔Ture,不存在返回False

b = '张三' in persons
print(b)

#copy 函数用于复制列表
persons1 = persons.copy()
persons2 = persons
#is 身份运算符用于判断两个变量是否指向同一块内存
print(persons1 in persons)
print(persons2 in persons)

#clear用于清空列表
persons.clear()
print(persons)
print(persons1)
print(persons2)
# 其他常用方法
persons = ['张三' , '赵六' , "李四" , '王五' , '赵六' , '钱七' ,'孙八']

#统计出现次数
cnt = persons.count('赵六')
print(cnt)
#追加操作
#append将整个列表追加到末尾,extend则是将列表中的元素追加到原始列表末尾
persons.append(['杨九' , '吴十'])
print(persons)
persons.extend(['杨九' , '吴十'])
print(persons)

#in(成员运算符) 运算符用于判断数据是否在列表中存在,存在反悔Ture,不存在返回False

b = '张三' in persons
print(b)

#copy 函数用于复制列表
persons1 = persons.copy()
persons2 = persons
#is 身份运算符用于判断两个变量是否指向同一块内存
print(persons1 in persons)
print(persons2 in persons)

#clear用于清空列表
persons.clear()
print(persons)
print(persons1)
print(persons2)
#多维列表(嵌套列表)
# [[姓名,年龄,工资],[姓名,年龄,工资],[姓名,年龄,工资],[姓名,年龄,工资]]
# 字符串:"姓名,年龄,工资" 例如:"张三,30,2000"
#str = "张三,30,2000"
#l = str.split(",")
#print(str)
#print(l)
emp_list = []
while True:
    info =input('请输入员工信息:')
    if info == "":
        print("程序结束")
        break
    info_list = info.split(',')
    if len(info_list) !=3:
        print("输入格式不正确,请重新输入")
        continue
    emp_list.append(info_list)
    #print(emp_list)
    for emp in emp_list:
        print("{n},年龄{a},工资:{s}".format(n = emp[0],a = emp[1], s = emp[2]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值