python3列表的基本使用操作

列表操作包含以下函数:
1、cmp(list1, list2):比较两个列表的元素 
2、len(list):列表元素个数 
3、max(list):返回列表元素最大值 
4、min(list):返回列表元素最小值 
5、list(seq):将元组转换为列表 
列表操作包含以下方法:
1、list.append(obj):在列表末尾添加新的对象
2、list.count(obj):统计某个元素在列表中出现的次数
3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
5、list.insert(index, obj):将对象插入列表
6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7、list.remove(obj):移除列表中某个值的第一个匹配项
8、list.reverse():反向列表中元素
9、list.sort([func]):对原列表进行排序
 

#列表的添加,在列表末尾添加新的对象

list1.append(999)

#删除列表第几个元素

del list1[5]

#将对象插入列表

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

#移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

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

以上实例输出结果如下:

列表现在为 :  ['Google', 'Runoob']
列表现在为 :  ['Google']
list1 = ['Google', 'Runoob', 'Taobao']
list_pop=list1.pop(1)
print ("删除的项为 :", list_pop)
print ("列表现在为 : ", list1)

以上实例输出结果如下:

删除的元素为 : Runoob
列表现在为 :  ['Google', 'Taobao']

#移除列表中某个值的第一个匹配项

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

以上实例输出结果如下:

列表现在为 :  ['Google', 'Runoob', 'Baidu']
列表现在为 :  ['Google', 'Runoob']

list.sort( key=None, reverse=False)
用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数

# 获取列表的第二个元素

def takeSecond(elem):

    return elem[1]

# 列表 random = [(2, 2), (3, 4), (4, 1), (1, 3)]

# 指定第二个元素排序 random.sort(key=takeSecond)

# 输出类别 print ('排序列表:', random)

以上实例输出结果如下:

排序列表:[(4, 1), (2, 2), (1, 3), (3, 4)]
list.count(obj)用于统计某个元素在列表中出现的次数
list3 = ["a", "b", "c", "d","a","a","d","c","b","c","cdd"]
print(list3.count('a'))
from collections import Counter
c=Counter(list3)
print(c)

输出:Counter({'a': 3, 'c': 3, 'b': 2, 'd': 2, 'cdd': 1})

将列表元素都转换为int:list(map(int,s))

列表拼接:("".join(str(n) for n in kk))

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#python3列表操作大全 列表操作方法详解

#创建列表
list = ['a', 'b', 'c', 'd', 'e', 'f']
#取出列表
print(list[0], list[5]) #a f
#列表切片
print(list[1:3]) #['b', 'c']
print(list[-3:-1]) #['d', 'e']
#取的时候始终都是按照从左到右的顺序进行切片,动作都是顾头不顾尾。

#添加列表元素
#在最后面添加列表元素
list.append('g')
print(list) #['a', 'b', 'c', 'd', 'e', 'f', 'g']
#在任意位置插入列表元素
list.insert(1, 'b1')
print(list) #['a', 'b1', 'b', 'c', 'd', 'e', 'f', 'g']
#修改列表中的元素
list[1] = 'b2'
print(list) #['a', 'b2', 'b', 'c', 'd', 'e', 'f', 'g']
#删除列表中的元素
#方法1  删除指定位置
list.remove('b2')
print(list) #['a', 'b', 'c', 'd', 'e', 'f', 'g']
#方法2  删除指定位置
del list[1]
print(list) #['a', 'c', 'd', 'e', 'f', 'g']
#方法3
return_result = list.pop()
print(list) #['a', 'c', 'd', 'e', 'f']
#pop()方法是删除最后一个元素,并返回删除的元素
print(return_result) #g  返回删除的元素
#删除指定位置
list.pop(1) #list.pop(1) = del list[1]
print(list) #['a', 'd', 'e', 'f']

#列表查找 查找列表元素
#查找列表中某一元素所在的位置
list = ['a', 'b', 'c', 'b', 'e', 'b']
print(list.index('b')) #1  只能找到第一个
print(list[list.index('b')]) #b
#统计列表中同一个元素的数量
print(list.count('b')) #3
#清除列表中的元素
list.clear() #没有返回值
print(list) #[]
list = ['a', 'b', 'c', 'd', 'e', 'f']
#列表反转
list.reverse() #没有返回值
print(list) #['f', 'e', 'd', 'c', 'b', 'a']
#列表排序
list.sort() #默认升序排序  也可以降序排序 sort(reverse=True)
print(list) #['a', 'b', 'c', 'd', 'e', 'f']
#排序规则,由大到小依次为:特殊符号  数字  大写字母 小写字母
#合并列表
list_01 = [1, 2, 3, 4]
list_02 = ['a', 'b', 'c', 'd']
list_01.extend(list_02)
print(list_01) #[1, 2, 3, 4, 'a', 'b', 'c', 'd']
#删除整个列表
del list_02
#print(list_02)  #NameError: name 'list_02' is not defined

#列表复制
#浅复制 浅copy  仅复制一层
#方法1
list = ['a', 'b', ['c','d'], 'e']
list_copy = list[:]
print(list_copy) #['a', 'b', ['c', 'd'], 'e']
list[2][0] = 11
print(list_copy) #['a', 'b', [11, 'd'], 'e'] 修改list,list_copy中[11, 'd']也会改变,因为浅复制仅复制了[11, 'd']的地址

#方法2
list = ['a', 'b', ['c','d'], 'e']
list_copy02 = list.copy()
print(list_copy02) #['a', 'b', ['c', 'd'], 'e']
list[2][0] = 11
print(list_copy02) #['a', 'b', [11, 'd'], 'e'] 修改list,list_copy中[11, 'd']也会改变,因为浅复制仅复制了[11, 'd']的地址

#方法3
import copy
list = ['a', 'b', ['c','d'], 'e']
list_copy03 = copy.copy(list)
print(list_copy03) #['a', 'b', ['c', 'd'], 'e']
list[2][0] = 11
print(list_copy03) #['a', 'b', [11, 'd'], 'e']
#print(list_copy02)
#深复制 深copy
import copy
list = ['a', 'b', ['c','d'], 'e']
list_copy04 = copy.deepcopy(list)
print(list_copy04) #['a', 'b', ['c', 'd'], 'e']
list[2][0] = 11
print(list) #['a', 'b', [11, 'd'], 'e']
print(list_copy04) #['a', 'b', ['c', 'd'], 'e']
#lsit改变了,但是list_copy04却没有改变,deepcopy()方法是将list中的所有元素完全克隆了一份给list_copy04

猜你喜欢

转载自blog.csdn.net/qq_35924690/article/details/119983046