Python3 列表

基础

list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

访问列表中的值

list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
 
print ("list1[0]: ", list1[0])#list1[0]:  Google
print ("list2[1:5]: ", list2[1:5])#list2[1:5]:  [2, 3, 4, 5]

更新列表

list = ['Google', 'Runoob', 1997, 2000]
 
print ("第三个元素为 : ", list[2])#第三个元素为 :  1997
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])#更新后的第三个元素为 :  2001

删除列表元素

list = ['Google', 'Runoob', 1997, 2000]
 
print (list)#['Google', 'Runoob', 1997, 2000]
del list[2]
print ("删除第三个元素 : ", list)#删除第三个元素 :  ['Google', 'Runoob', 2000]

Python列表脚本操作符

print(len([1, 2, 3]))#长度
print([1, 2, 3] + [4, 5, 6])#组合
print(['Hi!'] * 4)#重复
print(3 in [1, 2, 3])#元素是否存在于列表中

for x in [1, 2, 3]: 
    print(x, end="\n")#迭代

Python列表截取与拼接

L=['Google', 'Runoob', 'Taobao']
print(L[2])#Taobao
print(L[-2])#Runoob
print(L[1:])#['Runoob', 'Taobao']

squares = [1, 4, 9, 16, 25]
print(squares + [36, 49, 64, 81, 100])
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

嵌套列表

a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]

print(x)#[['a', 'b', 'c'], [1, 2, 3]]
print(x[0])#['a', 'b', 'c']
print(x[0][1])#b

Python列表函数&方法

列表元素个数len(list)

list1 = ['Google', 'Runoob', 'Taobao']
print (len(list1))

返回列表元素最大值max(list)

list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200]

print ("list1 最大元素值 : ", max(list1))#list1 最大元素值 :  Taobao
print ("list2 最大元素值 : ", max(list2))#list2 最大元素值 :  700

返回列表元素最小值min(list)

print ("list1 最小元素值 : ", min(list1))#list1 最小元素值 :  Google
print ("list2 最小元素值 : ", min(list2))#list2 最小元素值 :  200

将元组转换为列表 Error

# aTuple = (123, 'Google', 'Runoob', 'Taobao')
# list1 = list(aTuple)
# print ("列表元素 : ", list1)

# str="Hello World"
# list2=list(str)
# print ("列表元素 : ", list2)

在列表末尾添加新的对象 list.append(obj)

list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("更新后的列表 : ", list1)#更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']

统计某个元素在列表中出现的次数list.count(obj)

aList = [123, 'Google', 'Runoob', 'Taobao', 123];

print ("123 元素个数 : ", aList.count(123))#123 元素个数 :  2
print ("Runoob 元素个数 : ", aList.count('Runoob'))#Runoob 元素个数 :  1

在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) list.extend(seq)

list1 = ['Google', 'Runoob', 'Taobao']
list1.extend(['Baidu','Bing'])  # 扩展列表
print ("扩展后的列表:", list1)#扩展后的列表: ['Google', 'Runoob', 'Taobao', 'Baidu', 'Bing']

从列表中找出某个值第一个匹配项的索引位置list.index(obj)

list1 = ['Google', 'Runoob', 'Taobao']
print ('Runoob 索引值为', list1.index('Runoob'))#Runoob 索引值为 1
print ('Taobao 索引值为', list1.index('Taobao'))#Taobao 索引值为 2

将对象插入列表list.insert(index, obj)

list1 = ['Google', 'Runoob', 'Taobao']
list1.insert(1, 'Baidu')
print ('列表插入元素后为 : ', list1)#列表插入元素后为 :  ['Google', 'Baidu', 'Runoob', 'Taobao']

移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 list.pop(obj=list[-1])

list1 = ['Google', 'Runoob', 'Taobao']
list1.pop()
print ("列表现在为 : ", list1)#列表现在为 :  ['Google', 'Runoob']
list1.pop(1)
print ("列表现在为 : ", list1)#列表现在为 :  ['Google']

移除列表中某个值的第一个匹配项list.remove(obj)

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.remove('Taobao')
print ("列表现在为 : ", list1)#列表现在为 :  ['Google', 'Runoob', 'Baidu']
list1.remove('Baidu')
print ("列表现在为 : ", list1)#列表现在为 :  ['Google', 'Runoob']

反向列表中元素list.reverse()

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.reverse()
print ("列表反转后: ", list1)

对原列表进行排序list.sort([func])

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.sort()
print ("列表排序后 : ", list1)#列表排序后 :  ['Baidu', 'Google', 'Runoob', 'Taobao']

清空列表 list.clear()

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.clear()
print ("列表清空后 : ", list1)#列表清空后 : 

复制列表list.copy()

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list2 = list1.copy()
print ("list2 列表: ", list2)#list2 列表:  ['Google', 'Runoob', 'Taobao', 'Baidu']

参考及引用

Python3 列表

转载于:https://my.oschina.net/jallenkwong/blog/1797766

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值