龙空技术网

python中的列表怎么怎么使用,你知道多少,你会用吗

工控薛同学 68

前言:

眼前看官们对“python列表的列表”都比较注意,大家都想要学习一些“python列表的列表”的相关知识。那么小编同时在网摘上搜集了一些对于“python列表的列表””的相关知识,希望小伙伴们能喜欢,我们快快来了解一下吧!

列表是什么?

列表是用中括号括起来的一组数据,列表中的值为表项,以逗号分隔开来

Season = ['Spring','Summer','Autumn','Winder']print(Season)

运行结果如下

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

['Spring', 'Summer', 'Autumn', 'Winder']

Process finished with exit code 0

用下标获取列表中的单个值

列表中的每项,对应的下标从零开始,依次递增,也可以以负数的形式存在,如-1表示倒数第一个元素,-2表示到时第二个元素

Season = ['Spring','Summer','Autumn','Winder']print(Season)print(Season[0])print(Season[1])print(Season[2])print(Season[3])print(Season[-1])print(Season[-2])print(Season[-3])print(Season[-4])

运行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

['Spring', 'Summer', 'Autumn', 'Winder']

Spring

Summer

Autumn

Winder

Winder

Autumn

Summer

Spring

Process finished with exit code 0

用len()方法获取列表的长度

Season = ['Spring','Summer','Autumn','Winder']print(len(Season))

运行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

4

修改列表中项的值

Season = ['Spring','Summer','Autumn','Winder']print(Season)Season[0] = "春天"Season[2] = "秋天"print(Season)print("----")

运行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

['Spring', 'Summer', 'Autumn', 'Winder']

['春天', 'Summer', '秋天', 'Winder']

----

列表连接和列表复制

+操作符可以连接两个列表,得到一个新列表

*操作符可以用于一个列表和一个整数,实现列表的复制。

Season_English = ['Spring' , 'Summer' , 'Autumn' , 'Winder']Season_Chinese = ['春天','夏天','秋天','冬天']print(Season_English)print(Season_Chinese)print(Season_English + Season_Chinese)print(Season_English * 2)print("------------------------------------------------------------")

执行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

['Spring', 'Summer', 'Autumn', 'Winder']

['春天', '夏天', '秋天', '冬天']

['Spring', 'Summer', 'Autumn', 'Winder', '春天', '夏天', '秋天', '冬天']

['Spring', 'Summer', 'Autumn', 'Winder', 'Spring', 'Summer', 'Autumn', 'Winder']

------------------------------------------------------------

用del删除列表中的某项

Season_Chinese = ['春天','夏天','秋天','冬天']print(Season_English)print(Season_Chinese)del(Season_English[1])print(Season_English)print("------------------------------------------------------------")

执行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

['Spring', 'Summer', 'Autumn', 'Winder']

['春天', '夏天', '秋天', '冬天']

['Spring', 'Autumn', 'Winder']

------------------------------------------------------------

in和not in操作符,判断列表中是否包含某个数据

Season_English = ['Spring' , 'Summer' , 'Autumn' , 'Winder']Season_Chinese = ['春天','夏天','秋天','冬天']Result = '春天' in Season_Chineseprint(Result)Result = '春天' in Season_Englishprint(Result)print("------------------------------------------------------------")

运行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

True

False

用index方法获取列表中对应项的索引

Season_English = ['Spring' , 'Summer' , 'Autumn' , 'Winder']Season_Chinese = ['春天','夏天','秋天','冬天']print((Season_Chinese).index('夏天'))print("------------------------------------------------------------")

运行结果如下:

D:\Python_Test\Scripts\python.exe C:/Users/LynnXue/PycharmProjects/pythonProject1/DateDo.py

1

------------------------------------------------------------

用 append()和 insert()方法在列表中添加值用 remove()方法从列表中删除值用 sort()方法将列表中的值排序

标签: #python列表的列表 #列表在python中