10 月 222020
 
# -*- coding:utf-8 -*-
# 数据结构,包括列表lists,字典dictionaries,元组tuples,集合sets

# 元组tuples中的元素是固定的,不可以修改,但可以查看索引
letters = ('a', 'b', 'c', 'd', 'e', 'f')
print(letters[0])

# 集合中的元素是无序的,不重复的任意对象
# 集合可以判断数据的从属关系
# 集合可以把数据结构中重复的元素减掉
# 集合不能被切片也不能被索引
a_set = {1, 2, 3, 4}
a_set.add(5)
print(a_set)
a_set.discard(4)
print(a_set)

# 对列表中的元素进行排序
num_list = [6, 2, 7, 4, 1, 3, 5]
print(sorted(num_list))  # 排序函数sorted()
print(sorted(num_list, reverse=True))  # 倒序排序

# 使用zip()函数同时操作多个列表
a = [3, 6, 9]
b = ['x', 'y', 'z']
# zip()函数的参数为迭代器(iterable)即可迭代的对象,可以为一个或多个
c = zip(a, b)  # zip()函数将对象中对应的元素打包成一个个元组tuples,然后返回由这些元组组成的对象
print(c)  # zip函数的返回值是zip类的对象,可以通过list()强制转为list列表
print(list(c))
# 多重循环
for a, b in zip(a, b):
    print(b, 'is', a)
C:\Users\harveymei\PycharmProjects\hellopython\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/hellopython/datastructure.py
a
{1, 2, 3, 4, 5}
{1, 2, 3, 5}
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
<zip object at 0x0000029CD11E7080>
[(3, 'x'), (6, 'y'), (9, 'z')]
x is 3
y is 6
z is 9

Process finished with exit code 0

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)