5.Python列表-索引值

1.列表

#方括号[]创建列表
#len()计算列表元素的数量。
#max(),min()最大值和最小值。
#count()计算某个值出现的次数。
#{}  列表元素
a_list=[1,2,3]
print("{}".format(a_list))
print("a_list 列表有 {} 个元素".format(len(a_list)))
print("最大值:{}".format(max(a_list)))
print("最小值:{}".format(min(a_list)))
another_list=['printer',5,['star','circle',9]]
print("{}".format(another_list))
print("another_list 有 {} 个元素".format(len(another_list)))
print("5 出现 {} 次".format(another_list.count(5)))

#结果:
[1, 2, 3]
a_list 列表有 3 个元素
最大值:3
最小值:1
['printer', 5, ['star', 'circle', 9]]
another_list 有 3 个元素
5 出现 1 次

2.索引值

#使用索引值访问特定元素。
a_list=[1,2,3]
another_list=['printer',5,['star','circle',9]]
print("{}".format(a_list[0]))
print("{}".format(a_list[1]))
print("{}".format(a_list[2]))
print("{}".format(a_list[-1])) #倒数第一个。
print("{}".format(a_list[-2])) #倒数第二个
print("{}".format(a_list[-3])) #倒数第三个。
print("{}".format(another_list[2]))
print("{}".format(another_list[-1]))

#结果:
1
2
3
3
2
1
['star', 'circle', 9]
['star', 'circle', 9]
#列表索引从0开始。
#列表的尾部从-1开始。

3.列表切片。

a_list=[1,2,3]
another_list=['printer',5,['star','circle',9]]
print("{}".format(a_list[0:2])) #从第一位开始取2位。
print("{}".format(another_list[:2])) #从第一位开始取2位。
print("{}".format(a_list[1:3])) #从第二位开始取3位。元素不够,只能娶到2个。
print("{}".format(another_list[1:])) #从第二位开始取到末尾

#结果:
[1, 2]
['printer', 5]
[2, 3]
[5, ['star', 'circle', 9]]

4.列表复制 

a_new_list=a_list[:] #[:]表示列表中所有元素  
print("{}".format(a_new_list)) #从第二位开始取到末尾

#结果
[1, 2, 3]

5.列表连接

a_list=[1,2,3]
another_list=['printer',5,['star','circle',9]]
a_longer_list=a_list+another_list
print("{}".format(a_longer_list))

#结果
[1, 2, 3, 'printer', 5, ['star', 'circle', 9]]
#可以使用 + 号将两个列表连接。

6.使用in和not in

a_list=[1,2,3]
a = 2 in a_list 
print("{}".format(a))
if 2 in a_list:
	print("2 is in {}".format(a_list))
b=6 not in a_list
if 6 not in a_list:
	print("6 is not in {}".format(a_list))
	
#结果
True
2 is in [1, 2, 3]
6 is not in [1, 2, 3]

7.追加-删除-弹出元素

a_list=[1,2,3]
a_list.append(4)
a_list.append(5)
a_list.append(6)
print("{}".format(a_list))
a_list.remove(5)
print("{}".format(a_list))
a_list.pop()
print("{}".format(a_list))
a_list.pop()
print("{}".format(a_list))

#结果:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]
[1, 2, 3]
#pop函数删除末尾的值。
#append向末尾追加。

8.列表反转

a_list=[1,2,3]
a_list.reverse() 
print("{}".format(a_list))
a_list.reverse() 
print("{}".format(a_list))
#结果
[3, 2, 1]
[1, 2, 3]

9.列表排序

unordered_list=[3,5,1,7,2,8,4,9,0,6]
print("{}".format(unordered_list))
list_copy=unordered_list[:]
list_copy.sort()
print("{}".format(list_copy))
print("{}".format(unordered_list))

#结果 
[3, 5, 1, 7, 2, 8, 4, 9, 0, 6]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[3, 5, 1, 7, 2, 8, 4, 9, 0, 6]
#通过sort方法可以列表进行排序。

10.sorted排序函数

#sorted()对一个列表集合按照列表中某个位置的元素进行排序。
my_lists=[[1,2,3,4],[4,3,2,1],[2,4,1,3]]
my_lists_sorted_by_index_3=sorted(my_lists,key=lambda index_value:index_value[3]) #按照第4个列表元素值排序。
print("{}".format(my_lists_sorted_by_index_3))

#结果 
[[4, 3, 2, 1], [2, 4, 1, 3], [1, 2, 3, 4]]
#第四个列表元素分别是:1,3,4 按照从小到大排序。
#sorted 不改变元素在列表中的顺序。

11.operator模块中itemgetter()

对一个列表集合按照两个索引位置来排序。

#!/usr/bin/env python3 
from math import exp,log,sqrt 
import re 
from datetime import date,time,datetime,timedelta 
from operator import itemgetter
my_lists=[[123,2,2,444],[22,6,6,444],[354,4,4,678],[578,1,1,290],[461,1,1,290]]
my_lists_sorted_by_index_3_and_0=sorted(my_lists,key=itemgetter(3,0))
print("{}".format(my_lists))
print("{}".format(my_lists_sorted_by_index_3_and_0))

#结果。
[[123, 2, 2, 444], [22, 6, 6, 444], [354, 4, 4, 678], [578, 1, 1, 290], [461, 1, 1, 290]]
[[461, 1, 1, 290], [578, 1, 1, 290], [22, 6, 6, 444], [123, 2, 2, 444], [354, 4, 4, 678]]
#1.先按第4位的值排序:290,290,444,444,678
#2.再按照第1位的值排序:两个有290元素的列表:461,578 ,两个有444原始的 列表:22,123 

12.总结

列表索引从0开始,列表尾部从-1 开始。

列表元素用:{} 表示。

列表用:[] 表示。

sorted:按照一个元素的值排序

itemgetter:按照两个元素的值排序。

append:追加

remove:删除

pop:删除末尾。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值