1,遍历整个列表
magicians=['zhang','hua','tom']for magician in magicians: print(magician.Title())
定义临时变量magician,最好临时变量能够准确描述要表达的列表内容。
(合并拼接+)
magicians=['zhang','hua','tom']for magician in magicians: print(magician.Title()+','+'is good')
magicians=['zhang','hua','tom']for magician in magicians: print(magician.Title()+','+'is good') print('I am very like'+' '+magician.Title()+'.\n')#\n表示每次循环一遍,都插入一个空行
magicians=['zhang','hua','tom']for magician in magicians: print(magician.Title()+','+'is good') print('I am very like'+' '+magician.Title()+'.\n')#\n表示每次循环一遍,都插入一个空行print('Thank you ')
2,避免缩进错误
2.1忘记缩进
通常跟在for循环后面的,需要缩进。
magicians=['zhang','hua','tom']for magician in magicians:print(magician.Title()+','+'is good')
当期望某项 *** 作将针对每个列表元素都执行一次,但却只执行了一次,检查逻辑错误。
magicians=['zhang','hua','tom']for magician in magicians: print(magician.Title()+','+'is good')print('I am very like'+' '+magician.Title()+'.\n')#\n表示每次循环一遍,都插入一个空行
2.4循环后不必要的缩进(和2.2的逻辑错误一样)
2.5遗忘了冒号for magician in magicians:
3,创建数值列表range()
for value in range(1,5):#只打印1,2,3,4 print(value)
#可以指定步长even_numbers=List(range(1,11,2)) #[1, 3, 5, 7, 9]奇数print(even_numbers)even_numbers=List(range(2,11,2)) #[2, 4, 6, 8, 10]偶数print(even_numbers)
#创建一个列表,包含1-10的平方squares=[]#建空列表for value in range(1,11): square=value**2 #平方 squares.append(square)#按照顺序添加print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]#也可以不使用临时变量squares=[]#建空列表for value in range(1,11): squares.append(value**2 )#按照顺序添加print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4,对数字列表简单的统计计算
#min max summany_numbers=range(1,1000)print(min(many_numbers))#1print(max(many_numbers))#999print(sum(many_numbers))#499500
5,列表解析
#创建一个列表,包含1-10的平方squares=[value**2 for value in range(1,11)]print(squares)#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
6,列表切片
player=['hua','xi','liu','ha','oo']print(player[0:2])#['hua', 'xi']print(player[1:3])#['xi', 'liu']print(player[:4])#['hua', 'xi', 'liu', 'ha']print(player[2:])#['liu', 'ha', 'oo']print(player[-2:])#['ha', 'oo']print(player[-3:-1])#['liu', 'ha']#遍历切片players=['hua','xi','liu','ha','oo']for player in players[:3]: print(player.Title())
#复制列表my_foods=['haha','hehe','hoho']frIEnd_foods=my_foods[:]print(my_foods)print(frIEnd_foods)my_foods.append('coco')frIEnd_foods.append('koko')print(my_foods)print(frIEnd_foods)'''['haha', 'hehe', 'hoho']['haha', 'hehe', 'hoho']['haha', 'hehe', 'hoho', 'coco']['haha', 'hehe', 'hoho', 'koko']'''my_foods=['haha','hehe','hoho']frIEnd_foods=my_foodsprint(my_foods)print(frIEnd_foods)my_foods.append('coco')frIEnd_foods.append('koko')print(my_foods)print(frIEnd_foods)'''['haha', 'hehe', 'hoho']['haha', 'hehe', 'hoho']['haha', 'hehe', 'hoho', 'coco', 'koko']['haha', 'hehe', 'hoho', 'coco', 'koko']'''
remark:这种语法实际上是让python将新变量frIEnd_foods关联到包含在my_foods中的列表,因此这两个列表都指向同一个列表。
元组():不可变的列表
#定义元组dimensions=(10,20)#元组 圆括号print(dimensions[0])print(dimensions[1])#dimensions[0]=13#不可修改元组内容#print(dimensions)print('---------------------------------------------')#遍历元组dimensions=(10,20,30,38)for dimension in dimensions: print(dimension)#不能直接修改元组元素,但是可以重新赋值dimensions=(10,20,30,38)print('old:')for dimension in dimensions: print(dimension)dimensions=(10,22)print('new:')for dimension in dimensions: print(dimension)
总结 以上是内存溢出为你收集整理的python自学3- *** 作列表全部内容,希望文章能够帮你解决python自学3- *** 作列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)