美文网首页python进阶Java-Python-Django社区Python学习日志
【day 2】python编程:从入门到实践学习笔记-列表以及其

【day 2】python编程:从入门到实践学习笔记-列表以及其

作者: 苍云横渡 | 来源:发表于2018-03-14 01:30 被阅读135次

学习笔记目录
【day 1】python编程:从入门到实践学习笔记-安装、变量和简单数据类型
【day 2】python编程:从入门到实践学习笔记-列表以及其操作
【day 3】python编程:从入门到实践学习笔记-if 语句(末附练习答案代码)
【day 4】python编程:从入门到实践学习笔记-字典(末附练习答案代码)
【day 5】python编程:从入门到实践学习笔记-用户输入和while循环(末附答案代码)
【day 6】python编程:从入门到实践学习笔记-函数(末附练习答案代码)
【day 7】python编程:从入门到实践学习笔记-类(末附练习答案代码)
【day 8】python编程:从入门到实践学习笔记-文件和异常(末附练习答案代码)
【day 9】python编程:从入门到实践学习笔记-测试代码(末附练习答案代码)
【day 10】python编程:从入门到实践学习笔记-Django入门(一)
【day 11】python编程:从入门到实践学习笔记-Django入门(二)
【day 12】python编程:从入门到实践学习笔记-Django入门(三)
【day 13】python编程:从入门到实践学习笔记-Django入门(四)
【day 14】python编程:从入门到实践学习笔记-用户账户(一)
【day 15】python编程:从入门到实践学习笔记-用户账户(二)
【day 16】python编程:从入门到实践学习笔记-用户账户(三)
【day 17】python编程:从入门到实践学习笔记-设计样式和部署(一)
【day 18】python编程:从入门到实践学习笔记-设计样式和部署(二)& 补充

第三章 列表简介

列表是是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目。列表中的元素包括在方括号([])中,每个元素之间用逗号分割。列表是可变的数据类型,可以添加、删除或是搜索列表中的元素。

访问元素
访问列表元素可以通过索引+方括号的形式,记住,索引从0而不是1开始!

shoplist = ['apple', 'mango', 'carrot', 'banana']
print(shoplist)
print(shoplist[0].title())
print('i want a' + ' ' + shoplist[0])

运行结果:
['apple', 'mango', 'carrot', 'banana']
Apple
i want a apple

修改或添加元素
修改元素可以通过直接赋值的方法。

  • append() :将元素附加到列表末尾。
  • insert() :在列表中指定位置添加新元素。
  • del :删除列表中指定位置元素。
  • pop():移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
  • remove():删除列表中第一个指定值的元素。
shoplist = ['apple', 'mango', 'carrot', 'banana']
shoplist.append('duck')
print(shoplist)
shoplist.insert(0, 'chick')
print(shoplist)
del shoplist[1]
print(shoplist)
temp1 = shoplist.pop(0)
print(temp1)
print(shoplist)
shoplist.remove('banana')
print(shoplist)

运行结果:
['apple', 'mango', 'carrot', 'banana', 'duck']
[‘chick’, 'apple', 'mango', 'carrot', 'banana', 'duck']
[‘chick’, 'mango', 'carrot', 'banana', 'duck']
chick
['mango', 'carrot', 'banana', 'duck']
['mango', 'carrot', 'duck']

组织列表

  • sort():对列表进行永久性排序。默认按照字母顺序排列,相反顺序的话可以传递参数reverse=True
  • sorted():对列表进行临时排序,不改变原本列表。
  • reverse():反转列表元素排序。
  • len() :获取列表长度。
shoplist = ['apple', 'mango', 'carrot', 'banana']
shoplist.sort(reverse=True)
print(shoplist)
shoplist = ['apple', 'mango', 'carrot', 'banana']
print(sorted(shoplist))
print(shoplist)
shoplist.reverse()
print(shoplist)
len(shoplist)

运行结果:
['mango', 'carrot', 'banana', 'apple']
['apple', 'banana', 'carrot', 'mango']
['apple', 'mango', 'carrot', 'banana']
['banana', 'carrot', 'mango', 'apple']
4

第四章 操作列表

遍历列表
利用for循环可以快速遍历列表,不要忘记它的冒号

shoplist = ['apple', 'mango', 'carrot', 'banana']
for shop in shoplist:
    print('i want a ' + shop.title())

运行结果:
i want a Apple
i want a Mango
i want a Carrot
i want a Banana

缩进
行首的空白(空格和制表符)决定行的缩进层次,同一层次的语句必须有相同的缩进。
不要混合使用制表符和空格来缩进,在每个缩进层次要么使用单个制表符两个或四个空格

数值列表

  • range():生成指定步长的左闭右开区间的一系列数字。默认步长为1。
  • list() :将参数转化为列表。
  • min() :找出数字列表最小值。
  • max() :找出数字列表最大值。
  • sum() :求数字列表所有值相加的总和。
nums = list(range(1,5))
for num in nums:
    print(num)
print(min(nums))
print(max(nums))
print(sum(nums))

运行结果:
1
2
3
4
1
4
10

列表解析可将for循环和创建新元素的代码合并成一行,并自动附加新元素。

nums = [num*2 for num in range(1,11)]
print(nums)

运行结果:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

使用列表的一部分——切片

  • 创建切片时,要指定切片的第一个元素和最后一个元素的索引,和range()一样,区间为左闭右开。
  • 如果没有指定第一个索引,切片将从列表开头开始。同理,如果没有指定最后一个索引,切片将直到列表末尾结束。负数索引可以输出离列表末尾相应距离的元素。
  • 切片也可以作为列表遍历。
  • 同时省略起始和终止索引可以穿件包含整个列表的切片,相当于复制列表。
nums = list(range(0,11))
print(nums[0:5])
print(nums[:6])
Nums = nums[:]
print(Nums)

运行结果:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[8, 9, 10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

元组
元组不可以修改,使用圆括号标识。元组也可以用循环进行遍历。我们还能给元组的变量赋值。

nums = (1, 2)
print(nums)
nums = (4, 2)
for num in nums:
    print(num)
运行结果:
(1, 2)
4
2

相关文章

网友评论

本文标题:【day 2】python编程:从入门到实践学习笔记-列表以及其

本文链接:https://www.haomeiwen.com/subject/bjkxqftx.html