今天是学习python的第三天了,昨天忘记整理了,所以没有发,这不今天就补上,其实前面的部分学的比较简单,但是实操有点费时间,哈哈哈,xdm加油,我也是!
对列表中的元素进行相应的排序
使用sort方法对列表进行永久的排序,sort方法***默认对列表按照字母顺序进行排序***
car = ['audi','bwv','toyota','subaru']
car.sort()
print(car)
结果:
[‘audi’, ‘bwv’, ‘subaru’, ‘toyota’]
与方法sort排序的方法相反需要在括号加入reverse=True。 reverse:排序规则. reverse = True 降序 或者 reverse = False 升序,有默认值的参数为空就默认reverse = False
car = ['audi','bwv','toyota','subaru']
car.sort(reverse=True)
print(car)
结果:
[‘toyota’, ‘subaru’, ‘bwv’, ‘audi’]
临时性的改变列表 不改变其列表的顺序,只使其按照特定的顺序排序可以使用函数 sorted()
car = ['audi','bwv','toyota','subaru']
print(sorted(car))
print(car)
结果:
[‘audi’, ‘bwv’, ‘subaru’, ‘toyota’]
[‘audi’, ‘bwv’, ‘toyota’, ‘subaru’]
对比一下上述的两个结果就会发现利用 sorted()函数是临时性改变的
向sorted()函数传递参数reverse=True 实现列表方向排序。格式参考:sorted(list,reverse=True)
cars = ['audi','bwv','toyota','subaru']
print(f"Here is the original list:\n {cars}")
print(f"Here is the sorted list: ")
print(sorted(cars,reverse=True))
结果:
Here is the original list:
[‘audi’, ‘bwv’, ‘toyota’, ‘subaru’]
Here is the sorted list:
[‘toyota’, ‘subaru’, ‘bwv’, ‘audi’]
倒着打印列表(将列表中的顺序倒过来,仅仅是倒过来)
使用方法reverse(),该方法时永久性的改变列表的内容
cars = ['audi','bwv','toyota','subaru']
print("Here is the original list:")
print(cars)
cars.reverse()
print(f"here is the list that is changed {cars}")
结果:
Here is the original list:
[‘audi’, ‘bwv’, ‘toyota’, ‘subaru’]
here is the list that is changed [‘subaru’, ‘toyota’, ‘bwv’, ‘audi’]
确定列表的长度:使用的时len()函数。
作用:可以明确列表中有多少元素
cars = ['audi', 'bwv', 'toyota', 'subaru']
print(len(cars))
结果:
4
这里是要注意的
使用列表时减少检索错误
1. 一般是从0开始而不是1
2. 可以通过len()函数预先晓得列表的长度
3. 别忘了索引还有“-1”这种索引方式
遍历整个列表
我们使用for循环可以使列表中的元素一一按照顺序全部输出
格式:for 列表名 in 列表名
print(列表名)
people = ['yin yang','li jian bo','qi qian hua','hu kun','he han quan']
for person in people: #for 任意变量名 in 列表名 + print(变量名)
print(person)
结果:
yin yang
li jian bo
qi qian hua
hu kun
he han quan
分析:
第二行代码让Python获取列表people中的第一个值,再将其与变量相关联再读取第三行的代码输出person 由于列表中还有其它元素所以Python返回到for进行循环直到元素全部输出完
在for中实现更多的操作
people = ['yin yang','li jian bo','qi qian hua','hu kun','he han quan']
for peopl in people:
print(f"{peopl.title()},that was a great trick!")#在for中实现更多的操作需要缩进在for的范围内
print(f"{peopl.title()},I can't wait to see your nect trick.\n")
结果:
Yin Yang,that was a great trick!
Yin Yang,I can't wait to see your nect trick.
Li Jian Bo,that was a great trick!
Li Jian Bo,I can't wait to see your nect trick.
Qi Qian Hua,that was a great trick!
Qi Qian Hua,I can't wait to see your nect trick.
Hu Kun,that was a great trick!
Hu Kun,I can't wait to see your nect trick.
He Han Quan,that was a great trick!
He Han Quan,I can't wait to see your nect trick.
在for循环结束后执行一些操作
people = ['yin yang','li jian bo','qi qian hua','hu kun','he han quan']
for peopl in people:
print(f"{peopl.title()},that was a great trick!")
print(f"{peopl.title()},I can't wait to see your nect trick.\n")
print("Thank you,everyone.That was a great magic show!")
只要不在for的循环范围就可以在结束后执行一些操作,注意缩进
使用for语句常见的错误:
1. 忘记缩进,这个得看编译器的功能或者自己的意识
2. 不必要的缩进
3. 循环后不必要的缩进
4. 遗漏了冒号
第三天,就到这里了,兄弟们,可能有点慢,但是起码有收获对吧。
继续加油吧!
若有语句问题,以及术语问题,请再评论区指出,谢谢!!!