python学习之列表、元组、集合、字典随笔

数 据  结 构

一、【列表】操作列表的方法如下:

 列表是可变序列,通常用于存放同类项目的集合。
 
list_one = [1, 2, 3, 4, True, False, 'pig', 1, 1, 1, 1, 0, 0]
list_two = [1, 8, 10, 50, 400, 1000, 600, 2, 3, 99]
 
# 1、添加元素,在列表的末尾添加一个元素
list_one.append('U')
print(list_one)

# 2、扩展列表,使用可迭代对象中的所有元素进行扩展
list_one.extend(list_one)
print(list_one)

# 3、插入, 给指定位置插入元素
list_one.insert(1, 'A')
print(list_one)

# 4、移除,移除列表中第一个值,如果没有就抛ValueError异常
list_one.remove('A')
print(list_one)

# 5、删除,删除给定位置的元素,如果没有给定位置就默认删除列表中最后一个元素
list_one.pop(1)     # 给定位置,删除的就是制定位置的元素
print(list_one)
list_one.pop()      # 没给定位置,默认删除列表中最后一个元素
print(list_one)

# 6、清空,清空列表中所有的元素
list_one.clear()
print(list_one)
 
# 7、索引,返回列表中第一个元素的从零开始的索引
print(list_one.index(2))
print(list_one.index('pig', 6))

# 8、元素出现的次数
# True和False在列表中代表 1、0
print(list_one.count(0))

# 10、排序, 对列表中列表进行排序
# str类型和int类型之间不支持排序
list_two.sort()     # 不传参数正序排序
print(list_two)
list_two.sort(reverse=False)    # reverse=False 正序排序
print(list_two)
list_two.sort(reverse=True)     # reverse=True 逆序排序
print(list_two)

# 11、反转列表中的元素
list_one.reverse()
print(list_one)

# 12、复制,浅拷贝
list_tree = list_one.copy()
print(list_tree)
 

# 列表推导式创建新列表
# 公式:[计算公式 for 循环 if 条件判断]
squares = []
for i in range(5):
    squares.append(i)
print(squares)

list_tour = [j+1 for j in range(5)]
print(list_tour)

list_five = [(x, y) for x in squares for y in list_tour if x != y]
print(list_five)
 
# del 语句,del语句从列表中移除切片或者清空整个列表
del list_one[0]     # 移除一个元素
print(list_one)
del list_one[4:7]   # 移除下标 4-7的元素
print(list_one)
del list_one[:]     # 移除整个列表中的元素
print(list_one)
del list_one        # 删除整个list_one变量
 

二、【元组】操作元组的方法如下:

元组是不可变序列,通常用于储存异构数据的多项集 
 
# 1、一个元组由几个被都好隔开的值组成,例如:
tuple_one = 1234, 5463, 888
print('tuple_one类型:{}'.format(type(tuple_one)))
 
# 元组是不可变的,不允许修改里面的值
# 元组再输出时总要被圆括号包含,以便正确表示元组
# 空元组可以直接使用一对括号创建
# 含有一个元素的元组可以通过在这个元素后面添加一个逗号来构建
tuple_two = (1, 2, 'hello')  # 元组
print('tuple_two类型:{}'.format(type(tuple_two)))
tuple_three = ()    # 空元组
print('tuple_three类型:{}'.format(type(tuple_three)))
tuple_four = ('hello baby',)    # 元组
print('tuple_four类型:{}'.format(type(tuple_four)))
 
# 2、元组取值,直接使用下标及切片取值即可
print(tuple_one[0])
print(tuple_one[:2])
print(tuple_one[:])
 

三、【集合】操作集合的方法如下:

# 1、集合是由不重复的元素组成的无序的集,它会成员检测并消除重复元素
basket = {'hello world', 'apple', 'orange', 'banana', 'orange'}
print('basket类型{}:'.format(type(basket)))

# 集合创建使用花括号及set(),如若创建空集合只能使用set(),不能使用{}创建,后者是创建了一个空字典
# 集合set()中只能放字符串(str)类型的元素
gather_one = set()
print('gather_one类型:{}'.format(type(gather_one)))
gather_two = set('hello')
print('gather_two类型:{}'.format(type(gather_two)))

# 集合推导式创建集合
gather_three = {z for z in 'abcdefghijk'}
print(gather_three)
 

四、【字典】操作字典的方法如下:

 
# 字典的键几乎可以使任何值,但字典的键是不可变的
# 创建字典,字典可以通过将以逗号分隔的 键: 值 对列表包含于花括号之内来创建,也可以通过 dict 构造器来创建。
dict_one = {'jack': 4098, 'sjoerd': 4127}
print(dict_one)
dict_two = {4098: 'jack', 4127: 'sjoerd'}
print(dict_two)
dict_three = dict(one=1, wto=2, three=3)    # 构造器创建字典
print(dict_three)
dict_four = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
print(dict_four)
dict_five = dict({'one': 1, 'two': 2, 'three': 3})
print(dict_five)
dict_six = {}   # 创建空字典
print(dict_six)
 
print(list(dict_one))                    # 返回字典dict_one中使用的所有键的列表。
print(len(dict_one))                   # 返回字典dict_one中的项数
print(dict_one['jack'])                # 返回字典dict_one中'jack'的值,如果是不存在的key则会抛KeyError
dict_one['jack'] = 'hello'       # 修改dict_one中'jack'的值
print(dict_one)
print(dict_one.copy())           # 浅复制dict_one字典
print(dict_one.get('jack'))      # 取字典中的值,如果存在就是返回值,不存在就返回默认值,如果未给默认值则默认我None
dict_two.clear()                  # 清空字典
print(dict_two)
del dict_five['one']                   # 将dict_five中的'one',从dict_one中移除,如果不存在就返回KeyError
print(dict_five)
print(dict_four.items())            # 返回由字典项 ((键, 值) 对) 组成的一个新视图
print(dict_four.keys())             # 返回由字典键组成的一个新视图
print(dict_four.values())          # 返回由字典值组成的一个新视图
 
# 字典推导式创建字典
dict_seven = {x: y for x in [1, 2, 3, 4] for y in [4, 5, 6, 7]}
print(dict_seven)
 
 

猜你喜欢

转载自www.cnblogs.com/lifeng0402/p/11788760.html
今日推荐