提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
本文主要介绍python中列表的一些操作
提示:以下是本篇文章正文内容,下面案例可供参考
一、列表的定义?
list 列表–容器的一种
names = ['jack','tom','lucy','superman'.'ironmam']
computer_brands = []
print(id(names))
print(id(computer_brands))
二、列表的增删改查的操作
1.查找
print(names[0])
print(names[1])
print(names[-1])
print(name[len(names)-1])
print(names[-5])
for i in'hello':
print(i)
print('************')
for name in names:
print(name)
for name in names:
if name == 'superman':
print('有超人在里面')
break
else:
print('没有找到超人在里面')
if 'superman' in names:
print('超人在里面')
else:
print('没有找到超人在里面')
2.列表的该其实就是找出来后赋值
brands=['hp','dell','thinkpaid','支持华为','mac','lenovo','神州']
brands[-1]='HASEE'
'''
print('-------------')
for brand in brands:
if '华为' in brand:
brand='HUAWEI'
print(brands)
'''
for i in range(len(brands)):
if '华为' in brands[i]:
brands[i]='HUAWEI'
break
print(brands)
3 列表中的删除 del
del brands[2]
print(brands)
'''
print('---------删除--------------')
l=len(brands)
for i in range(l):
if 'hp' in brands[i] or 'mac' in brands[i]:
del brands[i]
l-=1
print(brands)
'''
l=len(brands)
i=0
while i<l:
if 'hp' in brands[i] or 'mac' in brands[i]:
del brands[i]
l-=1
i+=1
print(brands)
'''
They are students
yews
['hello','good','apple','world','digit','alpha']
hello
'''
4 漏删问题
world=['hello','good','apple','world','digit','alpha']
while True:
worlds=input('请输入你想输入的单词')
l=len(world)
i=0
while i<l:
if worlds in world[i]:
del world[i]
l-=1
i+=1
print(world)
answer=input('请输入是否继续y/n')
if answer == 'y':
continue
else:
break
5 in 的问题
world=['hello','good','apple','world','digit','alpha']
w=input('请输入一个单词:')
for word in world:
if w in word:
print('存在此单词!')
break
if 'good'=='good':
print('相等')
if 'good' in 'goods':
print('相等或者包含')
i=1
if 'good' in ['goods','good','abc','aaa']:
print('包含...',i)
i+=1
for w in ['goods','good','abc','aaa']:
print('good' in w)
print('----------->',i)
i+=1
'''
if 让 in 判断作为一个条件表达式:
if 'a' in 'abc':
pass
if 'a' in ['a','b','c']
pass
但是:
for ... in 循环条件
for 变量 in 字符串|列表:
pass
'''
'''
for word in worlds:
if w in word:
del word
break
print(worlds)
'''
6 列表的切片
list1 = ['杨超越','热巴','佟丽娅'.'杨幂','赵丽颖','刘亦菲','黑嘉嘉',100,99]
print(list1)
print(list1[3])
print(list1[3:6])
print(list1[-1:])
print(list1[-3:-1])
ptint(list1[::2])
print(list1[-5:-1:2])
print(list[-1::-1])
print(list1[-1::-2])
7 列表的添加
girls=[]
while True:
name =input('请输入你心目中的美女名字')
if name =='quit':
break
girls.append(name)
print(girls)
names=['黑嘉嘉','王丽坤','巩俐']
name = input('请输入你心目中的名字:')
girls.extend(names)
print(girls)
girls = girls + names
print(girls)
girls.insert(1,'刘涛')
print(girls)
总结
提示:这里对文章进行总结: 主要是介绍了Python的一些增删改查的操作。
|