ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

打卡记录——python入门到实践

2022-01-26 15:03:39  阅读:133  来源: 互联网

标签:honda 入门 yamaha python 元素 列表 print 打卡 motorcycles


3.4使用列表时,避免索引错误

假如你有一个三个包含三个元素的列表,却要求获取第四个元素(类似于C中的数组上溢,非法访问内存)

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])

导致索引错误:

Traceback (most recent call last):
  File "Day05.py", line 3, in <module>
    print(motorcycles[3])
IndexError: list index out of range

这是因为python等语言都是从C进化来的,也就是说,都是从0开始计数的。遇到这种情况,可以尝试将索引-1,再次运行。

一般,如果是获取最后一个元素的话,可以直接用索引-1(只要列表不为空,就能返回最后一个元素)。由于程序对代码进行了动态处理,所以当遇到索引问题时,可以将列表或其长度打印出来以解决逻辑问题。

3.5小结

列表是什么?

列表由一系列按特定顺序排序的元素组成的,就像数学里面的集合。元素可以是任何值得记录的事物,如家庭成员姓名,字母,数字等等。(元素之间可以没有任何关系)因为元素通常不止一个,所以列表通常引用复数名称。

如何使用其中的元素?

通过切片和索引两种方式。索引是从中获取单个值的方法,切片是获取多个值的方法(感觉得到的还是一个列表)。

motorcycles = ['honda', 'yamaha', 'suzuki']
#索引,有正向和逆向两种数数方式
print(motorcycles[0])
print(motorcycles[1])
print(motorcycles[2])
print(motorcycles[-1])
print(motorcycles[-2])
print(motorcycles[-3])
#切片,下例表示打印从motorcycles第一个元素开始到第二个元素,也就是对应着motorcycles[0]、[1],而不打印motorcycles[2]
print(motorcycles[0:2])
-----------------------------输出---------------------------------------------------------
honda
yamaha
suzuki
suzuki
yamaha
honda
['honda', 'yamaha']

如何定义列表以及增删元素?

类似于数组定义方式,但是不需要指定元素长度,也不可以省略元素值(???)

增加元素可以通过——append()方法从列表末尾增加;也可以是方法insert()增加至列表中指定位置处(需要指定索引)。

删除元素可使用del、pop、remove等三种方式。

del语句可删除任意位置处的列表元素(只要知道索引),但删除后你无法在访问那个值了。

方法pop()删除列表末尾的元素,但你能够接着使用它。(列表就像一个栈,删除列表末尾元素就相当于弹出栈顶元素。)

方法remove()根据值删除元素,也可以接着使用(使用一个中间变量将其值保留下来)

#Day05 2022/1/26
motorcycles = ['honda', 'yamaha', 'suzuki']
#方法append()
motorcycles.append('ducati')
print(motorcycles)
#方法insert()
motorcycles.insert(2, 'emmmm')
print(motorcycles)
#del
del motorcycles[2]
print(motorcycles)
#方法pop()
tem = motorcycles.pop()
print(motorcycles)
print(tem)
#方法remove()
motorcycles.remove('suzuki')
print(motorcycles)
---------------------输出-------------------
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'emmmm', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
ducati
['honda', 'yamaha']

如何进行永久性排序?

方法sort()按字母表顺序进行永久性排序,可传递参数reverse = True进行反向排序。 

方法reverse()颠倒着打印列表,与字母表顺序无关,就是和原列表顺序相反。

#sort()永久性对列表排序
cars = ['bmw', 'audi', 'toyuta', 'subaru']
cars.sort()
print(cars)
#与字母顺序相反传递参数reverse=True
cars.sort(reverse = True)
print(cars)
#reverse永久性颠倒顺序,不是按照字母表顺序,而是与原顺序相反
cars.reverse()
print("Now reverse it:")
print(cars)
------------输出-----------------------------------------
['audi', 'bmw', 'subaru', 'toyuta']
['toyuta', 'subaru', 'bmw', 'audi']
Now reverse it:
['audi', 'bmw', 'subaru', 'toyuta']

如何为展示列表进行临时排序?

函数sorted(),也可传递参数reverse = True进行反向临时排序

#sorted对列表临时排序 也可以排序reveerse=True
print("Here is the sorted list:")
print(sorted(cars))
print("Here is the orignal list:")
print(cars)
-----------输出------------------------------
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyuta']
Here is the orignal list:
['bmw', 'audi', 'toyuta', 'subaru']

如何确定列表长度?

函数len()快速获得

#获取表的长度
cars = ['bmw', 'audi', 'toyuta', 'subaru']
print("Got the length of the list:")
print(len(cars))
----------------输出----------------------
Got the length of the list:
4

标签:honda,入门,yamaha,python,元素,列表,print,打卡,motorcycles
来源: https://blog.csdn.net/m0_57890928/article/details/122698097

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有