Python数据类型 List列表及常见用法

列表List
  • 序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
  1. 列表创建,创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可
list((3, 5, 7, 9, 11))
list(range(1, 10, 2))
""" 字符串、集合、字典的键、字典的键:值对转列表 """
list('hello, world')
list({3, 7, 8})
list({'a':3, 'b':9, 'c':78})
list({'a':3, 'b':9, 'c':78}.items())
""" 空列表 """
x = list()
x = []
  1. 访问列表中的值
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
 
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

输出结果

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]
  1. 列表的删除
list1 = ['physics', 'chemistry', 1997, 2000]
 
print list1
del list1[2]
print "After deleting value at index 2 : "
print list1

输出结果

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
  1. 列表元素访问及修改
x = list('python')
print(x)
""" 正数顺序访问,负数倒序访问 """
print(x[1])
print(x[-1])
""" 用下标进行修改 """
x[2] = 'a'
print(x)

输出结果

['p', 'y', 't', 'h', 'o', 'n']
y
n
['p', 'y', 'a', 'h', 'o', 'n']
  1. [增]操作:append()、insert()、extend()
    x = [1, 2, 3]
""" append()尾部追加单个元素 """
x.append(4)
print(x)
""" insert()指定插入单个元素 """
x.insert(0, 0)
print(x)
""" extend()尾部追加列表 """
x.extend([5, 6, 7])
print(x)

输出结果

[1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 7]
  1. [删]操作:pop()、remove()、clear()、del
x = [1, 2, 3, 4, 5, 6, 7]
""" pop()删除并返回指定位置元素(默认结尾处)(也可用负数来逆序弹出) """
print(x.pop())
print(x.pop(-2))
""" remove()删除首个指定的值,没有则抛出异常 """
x.remove(4)
print(x)
""" del 删除指定位置元素,后续元素会自动收缩 """
del x[2]
print(x)
""" clear()删除所有元素 """
x.clear()
print(x)

输出结果

7
5
[1, 2, 3, 6]
[1, 2, 6]
[]
  1. [查]操作:下标、count()、index()
 = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
""" 下标查指定位置元素 """
print(x[4])
""" count()查指定元素出现次数 """
print(x.count(4))
""" index()查指定元素首次出现索引 """
print(x.index(3))

输出结果

3
4
3
  1. 排序操作:sort()、reverse()
x = list(range(11))
import random
""" 洗牌操作(打乱顺序) """
random.shuffle(x)
print(x)
""" sort()排序 """
""" 按字符串长度降序排序 """
x.sort(key=lambda item:len(str(item)), reverse=True)
print(x)
""" 按字符串大小升序排序:从左边开始第一个字符依次比较 """
x.sort(key=str)
print(x)
""" 默认排序(升序) """
x.sort()
print(x)
""" reverse()翻转 """
x.reverse()
print(x)

输出结果

[5, 0, 6, 4, 1, 9, 10, 3, 2, 8, 7]
[10, 5, 0, 6, 4, 1, 9, 3, 2, 8, 7]
[0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  1. 复制:copy()浅复制、deepcopy()深复制
x = [1, 2, [3, 4]]
y = x.copy()
import copy
z = copy.deepcopy(x)
  1. 常见内置函数:max()、min()、sum()、len()、all()、any()、zip()、enumerate()
x = list(range(11))
""" all()检测是否所有元素等价于True """
print(all(x))
""" any()检测是否存在元素等价于True """
print(any(x))
""" 最大的数 """
print(max(x))
""" 最小的数 """
print(min(x))
""" 列表求和 """
print(sum(x))
""" 列表长度 """
print(len(x))
""" 多列表重组成元组 """
print(list(zip(x, [2]*11)))
""" 枚举列表元素 """
print(list(enumerate(x)))

输出结果

False
True
10
0
55
11
[(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2), (10, 2)]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10)]
  1. 对列表的+ 和 * 操作
""" + 操作(非原地操作-->返回新列表) """
x = [1, 2, 3]
print(id(x))
x = x + [4]
print(x)
print(id(x))
""" += 操作类似于append()原地操作 """
x += [5]
print(x)
print(id(x))

""" * 操作(非原地操作-->返回新列表) """
x = [1, 2, 3, 4]
print(id(x))
x = x * 2
print(x)
print(id(x))
x *= 2
print(x)
print(id(x))
""" 重复0次清空 """
x *= 0
print(x)

输出结果

2541630195976
[1, 2, 3, 4]
2541630408712
[1, 2, 3, 4, 5]
2541630408712
2541630195976
[1, 2, 3, 4, 1, 2, 3, 4]
2541630408712
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
2541630408712
[]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值