python 常用数据结构

使用列表

数值类型是标量类型,也就是说这种类型的对象没有可以访问的内部结构。

符串类型是一种结构化的、非标量类型,所以才会有一系列的属性和方法。

列表(list),也是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在[]中,多个元素用,进行分隔,可以使用for循环对列表元素进行遍历,也可以使用[][:]运算符取出列表中的一个或多个元素。

如何定义列表、如何遍历列表,列表的下标运算。

list = [1 , 2 , 3 , 4 , 5]
print(list)#[1, 2, 3, 4, 5]

#*表示列表元素的重复
list2 = ['word'] * 3
print(list2)#['word', 'word', 'word']

# 计算列表长度(元素个数)
print(len(list1))#5

# 下标(索引)运算
print(list1[0])#1
print(list1[4])#5
print(list1[5])#IndexError: list index out of range
print(list1[-1])#5
print(list1[-3])#3
list1[2] = 200#[1, 2, 200, 4, 5]

# 通过循环用下标遍历列表元素
for index in range(len(list1)):
    print(list1[index])
#
1
2
3
4
5
# 通过for循环遍历列表元素
for elem in list1:
    print(elem)
# 通过enumerate函数处理列表之后再遍历可以同时获得元素索引和值
for index, elem in enumerate(list1):
    print(index, elem)
#
0 1
1 2
2 3
3 4
4 5







如何向列表中添加元素以及如何从列表中移除元素。

list1 = [1, 2, 3, 4, 5]
#添加元素
list1.append(100)
list1.insert(1,250)
list1.extend([1000, 2000])#list1 += [1000, 2000]
print(list1)#[1, 250, 2, 3, 4, 5, 100, 1000, 2000]
print(len(list1))#9

# 先通过成员运算判断元素是否在列表中,如果存在就删除该元素
if 3 in list1:
    list1.remove(3)
if 300 in list1:
    list1.remove(300)
print(list1)#[1, 2, 4, 5]

# 从指定的位置删除元素
list1.pop(0)
list1.pop(len(list1) - 1)
print(list1)#[2, 4]

# 清空列表元素
list1.clear()
print(list1)#[]








和字符串一样,列表也可以做切片操作,通过切片操作我们可以实现对列表的复制或者将列表中的一部分取出来创建出新的列表,

color = ['red', 'yellow', 'blue', 'black',]
color +=['green', 'orange', 'purple']
color2 = color[1:4]#列表切片
print(color2)#['yellow', 'blue', 'black']
# 可以通过完整切片操作来复制列表
color3 = color[:]
print(color3)
#['red', 'yellow', 'blue', 'black', 'green', 'orange', 'purple']

color4 = color[-3: -1]
print(color4)#['green', 'orange']

# 可以通过反向切片操作来获得倒转后的列表的拷贝
color5 = color[:: -1]
print(color5)
#['purple', 'orange', 'green', 'black', 'blue', 'yellow', 'red']

对列表的排序操作。

color = ['red', 'yellow', 'blue', 'black',]
color2 = sorted(color)
# sorted函数返回列表排序后的拷贝不会修改传入的列表
# 函数的设计就应该像sorted函数一样尽可能不产生副作用
color3 = sorted(color, reverse=True)
color4 = sorted(color, key=len)
# 通过key关键字参数指定根据字符串长度进行排序而不是默认的字母表顺序
print(color)#['red', 'yellow', 'blue', 'black']
print(color2)#['black', 'blue', 'red', 'yellow']
print(color3)#['yellow', 'red', 'blue', 'black']
print(color4)#['red', 'blue', 'black', 'yellow']
color.sort(reverse=True)
print(color)#['yellow', 'red', 'blue', 'black']

生成式和生成器

我们可以使用列表的生成式语法来创建列表,

f = [x for x in range(1, 10)]
print(f)#[1, 2, 3, 4, 5, 6, 7, 8, 9]
f = [x + y for x in 'ABCDE' for y in '1234567']
print(f)

Python中还有另外一种定义生成器的方式,就是通过yield关键字将一个普通函数改造成生成器函数

ef fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
        yield a
def main():
    for val in fib(20):
        print(val)


if __name__ == '__main__':
    main()

使用元组

ython中的元组与列表类似也是一种容器数据类型,可以用一个变量(对象)来存储多个数据,不同之处在于元组的元素不能修改,在前面的代码中我们已经不止一次使用过元组了,

# 定义元组
t = ('怼怼',38 ,True, '四川')
print(t)
# 获取元组中的元素
print(t[0])
print(t[3])
# 遍历元组中的值
for x in t:
    print(x)
# 重新给元组赋值
t = ('张三', 20, True, '云南')
print(t)
# 将元组转换成列表
person = list(t)
print(person)
# 将列表转换成元组
color_list = ['red', 'yellow', 'blue']
color_tuple = tuple(color_list)
print(color_tuple)

使用集合

python中的集合跟数学上的集合是一致的,不允许有重复元素,而且可以进行交集、并集、差集等运算。

# 创建集合的字面量语法
set1 = {1, 2, 3, 3, 3, 2}
print(set1)#{1, 2, 3}
print('Length =', len(set1))#Length = 3
# 创建集合的构造器语法
set2 = set(range(1, 10))
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3)#{1, 2, 3, 4, 5, 6, 7, 8, 9}{1, 2, 3}
# 创建集合的推导式语法(推导式也可以用于推导集合)
set4 = {num for num in range(1, 100)if num %3 == 0 or num % 5 ==0}
print(set4)

向集合添加元素和从集合删除元素,

set1.add(4)
set1.add(5)
set2.update([11,12])
set2.discard(5)
if 4 in set2:
    set2.remove(4)
print(set1, set2)
print(set3.pop())
print(set3)

未完

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值