列表、元组、字典、集合类型及其内置方法
列表类型内置方法:
按索引取值及赋值及切片取值
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
print(my_hobby_list[2])
print(my_hobby_list[2:])
print(my_hobby_list[-1:1:-1])
my_hobby_list[3] = 'eating'
print(my_hobby_list)
长度
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
print(len(my_hobby_list))
成员运算
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
print('gaming' in my_hobby_list)
print('cooking' not in my_hobby_list)
追加值
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
if True:
my_hobby_list.append('drinking')
print(my_hobby_list)
删除
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
del my_hobby_list[3]
print(my_hobby_list)
del my_hobby_list
循环
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
for i in my_hobby_list:
print(i)
其他常用方法
my_hobby_list = ['dancing', 'reading', 'coding', 'cooking']
# insert 根据索引,插入
my_hobby_list.insert(0, 'where is me')
print(my_hobby_list)
# pop 删除,若无指定索引,默认删除最后一个
my_hobby_list.pop()
my_hobby_list.pop(0)
print(my_hobby_list)
# remove 根据内容而不是索引删除
my_hobby_list.remove('dancing')
print(my_hobby_list)
# count 计数,返回某成员出现次数
my_hobby_list.append('apple')
my_hobby_list.append('apple')
print(my_hobby_list)
print(my_hobby_list.count('apple'))
# index 索引,返回某成员第一次出现的索引
print(my_hobby_list.index('apple'))
print(my_hobby_list.index('coding'))
# clear 清除列表数据
my_hobby_list.clear()
print(my_hobby_list)
# copy 复制列表
my_new_hobby_list = ['apple', 'banana', 'cherry', 'dog', 'english']
copy_list = my_new_hobby_list.copy()
print(copy_list)
print(id(copy_list))
print(id(my_new_hobby_list))
my_new_hobby_list[4] = 'Deutsch'
print(copy_list)
print(my_new_hobby_list)
print(id(my_hobby_list))
my_hobby_list = my_new_hobby_list.copy()
print(id(my_hobby_list))
# extend 把一个列表内容扩展到另一个列表的后面
my_new_hobby_list.extend(copy_list)
print(my_new_hobby_list)
# reverse 反转列表的内容
print(my_hobby_list)
my_hobby_list.reverse()
print(my_hobby_list)
# sort 排序,但数据内容必须是同一种类型
print(my_new_hobby_list)
my_new_hobby_list.sort(reverse=False)
print(my_new_hobby_list)
my_new_hobby_list.sort(reverse=True)
print(my_new_hobby_list)
元组的内置方法
hobby_tuple = ('apple', 'boy', 'girl', 'working')
# 索引取值
print(hobby_tuple[0])
print(hobby_tuple[-1])
# 切片(顾头不顾尾,步长)
print(hobby_tuple[1:])
print(hobby_tuple[::-1])
# 长度len
print(len(hobby_tuple))
# 成员运算in和not in
print('apple' in hobby_tuple)
print('banana' not in hobby_tuple)
# 循环
for i in hobby_tuple:
print(i)
# count
print(hobby_tuple.count('girl'))
# index
print(hobby_tuple.index('girl'))
字典类型的方法
优先需要掌握的方法
catching_dict = {'a': 'apple',
'b': 'butterfly',
'c': 'character',
'd': 'document',
'e': 'email',
'f': 'first'}
# 按key存取值:可存可取
print(catching_dict['c'])
catching_dict['c'] = 'car'
print(catching_dict['c'])
# 长度len
print(len(catching_dict))
# 成员运算in和not in
print('a' in catching_dict)
print('car' in catching_dict.values())
print('d' in catching_dict.values())
# 删除del & pop 补充 get popitem
del catching_dict['a']
print(catching_dict)
catching_dict.pop('f')
print(catching_dict)
print(catching_dict.get('b'))
print(catching_dict.get('a'))
print(catching_dict)
catching_dict.popitem()
print(catching_dict)
# 键keys()、值values()、键值对items()
catching_dict['a'] = 'apple'
catching_dict['e'] = 'english'
print(catching_dict)
print(catching_dict.keys())
print(catching_dict.values())
print(catching_dict.items())
# 循环
for k, v in catching_dict.items():
print(k, v)
需要掌握的其他方法
# get 通过KEY获取VALUE,如果KEY不存在,返回 None
print(catching_dict.get('a'))
print(catching_dict.get('b'))
print(catching_dict.get('c'))
print(catching_dict.get('z'))
# update 把一个字典的内容添加到另一个字典中
append_dict = {'z': 'zoo'}
catching_dict.update(append_dict)
print(catching_dict)
# fromkeys 用列表里的数据作为KEY,生成一字典
new_dict = dict.fromkeys(['apple', 'banana', 'cherry'], 'delicious')
print(new_dict)
# setdefault 加入一组键值对,若不存在,则添加,存在则不添加
new_dict.setdefault('apple', 'good')
new_dict.setdefault('zoo', 'tiger')
print(new_dict)
集合类型的内置方法
# 长度len
empty_set = set()
fruits_a_set = {'apple', 'banana', 'cherry', 'orange', 'lemon'}
fruits_b_set = {'cherry', 'orange', 'pear', 'watermelon'}
print(len(empty_set))
print(len(fruits_a_set))
print(len(fruits_b_set))
# 成员运算in和not in
print('cherry' in fruits_b_set)
print('apple' in fruits_b_set)
print('banana' not in fruits_a_set)
# |并集、union
print(fruits_a_set.union(fruits_b_set))
print(fruits_a_set | fruits_b_set)
# &交集、intersection
print(fruits_a_set.intersection(fruits_b_set))
print(fruits_a_set & fruits_b_set)
# -差集、difference
print(fruits_a_set - fruits_b_set)
print(fruits_b_set.difference(fruits_a_set))
# ^对称差集、symmetric_difference
print(fruits_a_set.symmetric_difference(fruits_b_set))
print(fruits_b_set ^ fruits_a_set)
# ==
print(fruits_a_set == fruits_b_set)
# 父集:>、>= 、issuperset
fruits_c_set = fruits_a_set - fruits_b_set
print(fruits_a_set >= fruits_c_set)
# 子集:<、<= 、issubset
print(fruits_c_set <= fruits_a_set)
# add
empty_set.add('peach')
print(empty_set)
# remove
empty_set.remove('peach')
print(empty_set)
# difference_update
print(fruits_a_set)
print(fruits_b_set)
fruits_a_set.difference_update(fruits_b_set)
print(fruits_a_set)
# discard
fruits_a_set.discard('zoo')
fruits_a_set.discard('apple')
print(fruits_a_set)
# isdisjoint
print(fruits_a_set)
print(fruits_b_set)
print(fruits_c_set)
print(fruits_a_set.isdisjoint(fruits_b_set))
print(fruits_a_set.isdisjoint(fruits_c_set))
数据类型总结
单值/多值 | 可变/不可变* | 有序/无序 | |
---|---|---|---|
数字 | 单值 | 不可变 | 无意义 |
字符串 | 单值 | 不可变 | 有序 |
列表 | 多值 | 可变 | 有序 |
元组 | 多值 | 不可变 | 有序 |
字典 | 多值 | 可变 | 无序 |
集合 | 多值 | 可变 | 无序 |