第一章 数据结构 -- 列表、元组和切片

第零章 学前准备
第一章 数据结构 – 基本数据类型
第一章 数据结构 – 字符串
第一章 数据结构 – 列表、元组和切片


第一章 数据结构 – 列表、元组和切片

1.2 组合数据类型

1.2.2 序列:列表

1.2.2.1 API
test_list_first = [1, 2, [1, 2], "1", "2"]
test_list = [1, 2, 3]
# 1. clear
test_list.clear()
print("clear", test_list)
# 2. copy(shallow copy)
test_list_first = [1, 2, [1, 2], "1", "2"]
test_list_copy = test_list_first.copy()
print("copy", id(test_list_first), id(test_list_copy),
      id(test_list_first[2]), id(test_list_copy[3]))
# 3. append和extend
test_list_first.append(3)
print("append(3)", test_list_first)
test_list_first.append([4, 5])
print("append([4,5])", test_list_first)
test_list_copy.append(3)
print("extend(3)", test_list_copy)
test_list_copy.extend([4, 5])
print("extend(4,5)", test_list_copy)
# 4. pop
print("pop", test_list_first.pop(), test_list_first)
# 5", "index,列表不存在抛出ValueError异常
print("index", test_list_first.index(3))
# 6", "count
print("count", test_list_first.count(2))
# 7", "insert
test_list_first.insert(0, 1)
print("insert", test_list_first)
# 8", "remove(只会移除第一个)
test_list_first.remove(1)
print("remove", test_list_first)
# 9", "reverse
test_list_first.reverse()
print("reverse", test_list_first)
# 10", "sort,需要元素可以比较,否则抛出TypeError。有两个参数,一个key,一个reverse
test_list_first = [1, 6, 3, 6, ]
test_list_first.sort()
print(test_list_first)
test_list_first = [[1, 3], 1, 2]


def sort_key(x):
    if isinstance(x, list):
        return x[-1]
    return x


test_list_first.sort(key=sort_key, reverse=True)
print(test_list_first)
1.2.2.2 列表使用小技巧
  1. 列表推导(list comprehension

    列表推导是构建列表(list)的快捷方式,使用列表推导可以帮助我们把一个序列或是其他可迭代类型中的元素过滤或是加工,然后再新建一个列表。
test_string = "123456"
codes = [ord(char) for char in test_string if char < '4']
print(codes)
test_list = [1, 2, 3, 4, 5, 6]
double = [ord(char) * value for value in test_list for char in test_string]
print(double)
[49, 50, 51]
[49, 50, 51, 52, 53, 54, 98, 100, 102, 104, 106, 108, 147, 150, 153, 156, 159, 162, 196, 200, 204, 208, 212, 216, 245, 250, 255, 260, 265, 270, 294, 300, 306, 312, 318, 324]

1.2.3 序列:元组

1.2.3.1 不可变列表

元组一般情况下可以看作不可变的列表。元组操作和列表一样,除了不可变。有两个常用的函数: countindex

1.2.3.2 不仅仅是不可变列表

元组中的每个元素都存放了记录中一个字段的数据,外加这个字段的位置。正是这个位置信息给数据赋予了意义。所以,跟位置结合使用,可以用于没有字段名的记录。比如:

city, year, pop, chg, area = ('Tokyo', 2003, 32450, 0.66, 8014)
print(city, year, pop, chg, area)
Tokyo 2003 32450 0.66 8014
1.2.3.3 元组拆包

在上面的例子中,元组 ('Tokyo', 2003, 32450, 0.66, 8014) 里的元素分别赋值给变量 cityyearpopchgarea ,这样的操作称为拆包。

# 1. 平行赋值,不使用中间变量交换两个变量的值
a, b = 1, 2
b, a = a, b
print(a, b)
# 2.使用*运算符把一个可迭代对象拆开
print("divmod(20,8)", divmod(20, 8))
params = (20, 8)
print("divmod(*params)", divmod(*params))
# 3、占位符_和元祖拆包结合提取港兴趣数据
*_, file_name = "/home/luciano/.ssh/idrsa.pub".split("/")
_, _, user_name, *_ = "/home/luciano/.ssh/idrsa.pub".split("/")
print(file_name, user_name)
# 4、嵌套元祖拆包
name, cc, pop, (latitude, longitude) = (
    'Tokyo', 'JP', 36.933, (35.689722, 139.691667))
print(name, cc, pop, latitude, longitude)

1.2.4 切片

Python 里,像列表( list )、元组( tuple )和字符串( str )这类序列类型都支持切片操作。

test_string = "0123456789"
test_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
test_tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# 1、切片基本用法
print(test_string[0:3], test_list[0:3], test_tuple[0:3])
print(test_string[:3], test_list[:3], test_tuple[:3])
print(test_string[-3:], test_list[-3:], test_tuple[-3:])
print(test_string[:], test_list[:], test_tuple[:])
print(test_string[::-1], test_list[::-1], test_tuple[::-1])
print(test_string[:8:2], test_list[:8:2], test_tuple[:8:2])
# 2、使用切片对象
slice_1 = slice(None, 3)
slice_2 = slice(None, None, -1)
slice_3 = slice(7, None)
print(test_list[slice_1], test_string[slice_2], test_tuple[slice_3])
# 3、给切片赋值
test_list[1:2] = [2, 3, 4]
print(test_list)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值