Python Basis (3) Container: list,tuple,dict,set. Study Note
本文是我在学习期间的笔记,看的书是《python语言及其应用》。
在编程中,最常见的工作就是将数据进行拆分和合并,将其加工为特定的形式, 而数据结构就是用以切分数据的钢锯以及合并数据的粘和合枪。
- 如果经常需要判断一个值是否存在一个列表中,但并不关心列表中元素之间的顺序,
使用Python的集合进行存储和查找会是更好的选择
list
- 列表非常适合利用顺序和位置定位某一元素,尤其时当元素的顺序或者内容经常发生改变时
- 相同的元素允许出现多次
- list() 可以创建一个空列表
- [] 可以创建一个空列表
empty_list = []
weekendys = ['Monday','Tuesday','Wednesday','Thursday','Friday']
big_birds = ['emu','ostrich','cassowary']
first_names = ['Graham','Jhon','Terry','Terry','Michael']
another_empty_list = list()
print(empty_list,weekendys,another_empty_list)
list() 将其他类型转换成列表
print(list('cat'))
a_tuple = ('ready','fire','aim')
print(list(a_tuple))
birthday = '1/6/1952'
print(birthday.split('/'))
splitme = 'a/b//c/d///e'
print(splitme.split('/'))
print(splitme.split('//'))
[offset] 获取元素
- 当指定的偏移量小于起始位置或者大于末尾位置时会产生异常
marxes = ['Grouco','Chico','Harpo']
print(marxes[0],marxes[1],marxes[2],marxes[-1],marxes[-2],marxes[-3])
print(marxes[5])
包含列表的列表
small_birds = ['hummingbird','finch']
extinct_birds = ['dodo','passenger pigeon','Norwegian Blue']
carol_birds = [3,'French hens',2,'turtledoves']
all_birds = [small_birds, extinct_birds, carol_birds]
print('all_birds',all_birds)
print('all_birds[0]',all_birds[0])
print('all_birds[1]',all_birds[1])
print('all_birds[1][0]',all_birds[1][0])
使用 offset 修改元素
marexs = ['Groucho','Chico','Harpo']
marexs[2] = 'Wanda'
print('marexs',marexs)
指定范围内使用切片提取元素
marexs = ['Groucho','Chico','Harpo']
print('marexs[0:2]',marexs[0:2])
print('marexs[::2]',marexs[::2])
print('marexs[::-2]',marexs[::-2])
print('marexs[::-1]',marexs[::-1])
append() 添加元素至尾部
marexs.append('Zeppo')
print(marexs)
extend() 或 += 合并列表
marexs = ['Groucho','CHico','Harpo','Zeppo']
others = ['Gummo','Karl']
marexs.extend(others)
print('marexs',marexs)
marexs += others
print('marexs',marexs)
marexs = ['Groucho','CHico','Harpo','Zeppo']
others = ['Gummo','Karl']
marexs.append(others)
print('marexs',marexs)
insert() 在指定位置插入元素
- 偏移量为0可以插入列表头部
- 偏移量超过尾部,则会插入到列表最后
marxes = ['Groucho','Chico','Harpo','Zeppo']
print(marxes)
marxes.insert(3,'Karl')
print(marxes)
del 删除指定位置的元素
- del 是Python 语句
print('marxes',marxes)
del marxes[-1]
print('marxes',marxes)
remove() 删除具有指定值的元素
print('marxes',marxes)
marxes.remove('Chico')
print('marxes',marxes)
pop() 获取并删除指定位置的元素
- pop(-1) 默认删除列表的尾元素
print('marxes',marxes)
marxes.pop()
print('marxes',marxes)
marxes.pop(0)
print('marxes',marxes)
index() 查询具有特定值的元素的位置
marxes = ['Groucho','Chico',"Harpo",'Zeppo']
print(marxes.index('Chico'))
in 判断值是否存在
print('marxes',marxes)
print("'Groucho' in marxes",'Groucho' in marxes)
words = ['a','deer','a','famale','deer']
print("'deer' in words",'deer' in words)
count() 记录特定的值出现的次数
print('marxes',marxes)
print(marxes.count('Harpo'))
print(marxes.count('Bob'))
snl_skit = ['cheeseburger','cheeseburger','cheeseburger']
print(snl_skit.count('cheeseburger'))
join() 转换为字符串
- join() 实际上是一个字符串方法
friends = ['Harry','Hermione','Ron']
separator = ' * '
joined = separator.join(friends)
print('joined',joined)
separated = joined.split(separator)
print('separated',separated)
print('separated == friends',separated == friends)
sort() 重新排列元素
- 列表方法 sort() 会对原列表进行排序,改变原列表内容
- 通用函数 sorted() 会返回排好的列表副本,原列表内容不变
- 数字,默认从小到大顺序
- 字符串,默认字母表顺序
- reverse=True 参数可以改为降序排列
marxes = ['Groucho','Chico','Harpo']
sorted_marxes = sorted(marxes)
print('sorted_marxes',sorted_marxes)
print('marxes',marxes)
marxes.sort()
print('marxes',marxes)
numbers = [2,1,3,2,9.0,3.4]
numbers.sort()
print('numbers',numbers)
numbers.sort(reverse=True)
print('numbers',numbers)
len() 获取长度
print('marxes',marxes)
print('len(marxes)',len(marxes))
= 赋值, copy() 复制
a = [1,2,3]
b = a
print('a',a,'b',b)
a[0] = 'surprise'
print('a',a,'b',b)
b[0] = 'I hate surprise'
print('a',a,'b',b)
- copy()
- list() 转换函数
- 列表分片[:]
a = [1,2,3]
b = a.copy()
c = list(a)
d = a[:]
a[0] = 'integer lists are boring'
print('a',a,'b',b,'c',c,'d',d)
tuple
- 相当于常量列表
- 函数的参数是以元组形式传递的
- 命名元组可以当作对象的替代
- 元组占用的空间比列表小
使用 () 创建元组
- 每一个元素后面都要跟着一个逗号
- 定义元组真正靠的是每个元素的后缀逗号
- 元组解包, 一次将元组赋值给多个变量
- tuple() 将其他类型转换成元组
empty_tuple = ()
print('empty_tuple',empty_tuple)
one_marx = 'Groucho',
print(one_marx)
marx_tuple = 'Groucho','Chico','Harpo'
print(marx_tuple)
a, b, c = marx_tuple
print('a',a,'b',b,'c',c)
passwd = 'swoedfish'
icecream = 'tuttifrtti'
passwd, icecream = icecream, passwd
print('passwd',passwd,'icecream',icecream)
marx_list = ['Groucho','Chico','Harpo']
print('tuple(marx_list)',tuple(marx_list))
dictionary
- 可变的,可以增加,删除,修改其中的键值对
- Python 允许在列表,字典,或者元组的最后一个元素后面添加逗号
使用 {} 创建字典
empty_dict = {}
print('empty_dict',empty_dict)
bierve = {
'day':'A period f twenty-four hours, mostly misspent',
'positive':"Mistaken at the top of one's voice",
'misfortune':'The kind of fortune that never misses'}
print('bierve',bierve)
dict() 转换为字典
lol = [['a','b'],['c','d'],['e','f']]
print('lol',dict(lol))
lot = [('a','b'),('c','d'),('e','f')]
tol = (['a','b'],['c','d'],['e','f'])
los = ['ab','cd','ef']
tos = ('ab','cd','ef')
print(dict(lot),dict(tol),dict(los),dict(tos))
[key] 添加或者修改元素
pythons = {
'Chapman':'Graham',
'Cleese':'Jhon',
"Idle":'Eric',
"Jones":'Terry',
'Palin':'Michael'}
print('pythons',pythons)
pythons['Gilliam'] = 'Gerry'
print('pythons',pythons)
pythons['Gilliam'] = 'Terry'
print('pythons',pythons)
update() 合并字典
print('pythons',pythons)
others = {'Marx':'Groucho','Howard':'Moe'}
pythons.update(others)
print('pythons',pythons)
first = {'a':1,'b':2}
second = {'b':'platypus'}
first.update(second)
print('first',first)
del 删除具有指定键的元素
print('pythons',pythons)
del pythons['Marx']
del pythons['Howard']
print('pythons',pythons)
clear() 删除所有的元素
print('pythons',pythons)
pythons.clear()
print('pythons',pythons)
in 判断键是否存在
pythons = {'Chapman':'Graham','Cleese':'Jhon','Jones':'Terry','Palin':'Michael'}
print("'Chapman' in pythons", 'Chapman' in pythons)
print("'Jhon' in pythons", 'Jhon' in pythons)
[key] 获取元素
- 直接获取不包含会报错
- get() 获取可以指定默认值
- get() 获取不到会返回 None
pythons = {'Chapman':'Graham','Cleese':'Jhon','Jones':'Terry','Palin':'Michael'}
print("'Palin' in pythons", 'Palin' in pythons)
print('pythons.get("Marx")',pythons.get('Marx'))
print('pythons.get("Marx")',pythons.get('Marx','Not a Python'))
print('pythons.get("Cleese")',pythons.get('Cleese'))
keys() 获取所有的键, values() 获取所有的值
print('pythons',pythons)
print('pythons.keys()',pythons.keys())
print('pythons.values()',pythons.values())
print('list(pythons.values())',list(pythons.values()))
items() 获取所有的键值对
print('pythons',pythons)
print('pythons.items()',pythons.items())
print('list(pythons.items())',list(pythons.items()))
= 赋值, copy() 复制
signals = {'green':'go','yellow':'go faster','red':'smile for the camera'}
save_signals = signals
signals['blue'] = 'confuse everyone'
print('signals',signals)
print('save_signals',save_signals)
original_signals = signals.copy()
signals['black'] = 'like dark'
print('signals',signals)
print('save_signals',original_signals)
set
- 键与键之间也不允许重复
set() 创建集合
empty_set = set()
print('empty_set',empty_set)
even_numbers = {0,2,4,6,8}
odd_numbers = {1,3,5,7,9}
print('even_numbers',even_numbers)
print('odd_numbers',odd_numbers)
set() 将其他类型转换成集合
- 字典作为参数传入 set() 时,只有键会被使用
s = 'letters'
print('set',set(s))
print(set(['Dasher','Dancer','Prance','Mason-Dixon']))
print(set(('Ummagumma','Echoes','Atom Heart Mother')))
print(set({'apple':'red','orange':'orange','cherry':'red'}))
in 测试值是否存在
drinks = {
'martini':{'vodka','vermouth'},
'black russian':{'vodka','kahlua'},
'white russian':{'cream','kahlua','vodka'},
'manhattan':{'rye','vermouth','bitters'},
'screwdriver':{'orange juice','vodka'}
}
for name, contents in drinks.items():
if 'vodka' in contents:
print('name',name)
print('other choose')
for name, contents in drinks.items():
if 'vodka' in contents and not ('vermouth' in contents or 'cream' in contents):
print('name',name)
合并及运算符
- & 交集运算符
for name, contents in drinks.items():
print(name, contents)
if contents & {'vermouth', 'orange juice'}:
print(name)
for name, contents in drinks.items():
if 'vodka' in contents and not contents & {'vermouth','cream'}:
print('name',name)
- union()
- difference() 差集,出现在第一个集合,但不出现在第二个集合
- intersection()
bruss = drinks['black russian']
wruss = drinks['white russian']
print('bruss',bruss)
print('wruss',wruss)
a = {1,2}
b = {2,3}
print('a & b', a & b)
print('a.intersection(b)',a.intersection(b))
print('bruss & wruss', bruss & wruss)
print('a | b', a | b)
print('a.union(b)',a.union(b))
print('bruss | wruss', bruss | wruss)
print('a - b', a - b )
print('a.difference(b)',a.difference(b))
print('bruss - wruss', bruss - wruss)
print('wruss - bruss', wruss - bruss)
其他一些运算符
print('a',a)
print('b',b)
print('bruss',bruss)
print('wruss',wruss)
print('a ^ b', a ^ b)
print('a.symmetric_difference(b)',a.symmetric_difference(b))
print('bruss ^ wruss', bruss ^ wruss)
print('a <= b', a <= b)
print('a.issubset(b)', a.issubset(b))
print('bruss <= wruss',bruss <= wruss)
print('a < b', a < b)
print('a < a', a < a)
print('bruss < wruss',bruss < wruss)
print('a >= b', a >= b)
print('a.issuperset(b)',a.issuperset(b))
print('wruss >= bruss', wruss >= bruss)
print('a >= a', a >= a)
print('a > b', a > b )
print('wruss > bruss', wruss > bruss)
比较几种数据结构
marx_list = ['Groucho','Chico','Harpo']
marx_tuple = 'Groucho','Chico','Harpo'
marx_dict = {'Groucho':'banjo','Chico':'piano','Harpo':'harp'}
print(marx_dict['Harpo'])
print(marx_list[2])
print(marx_tuple[2])
建立大型数据结构
- 列表, 字典, 集合不能作为字典的键
marexs = ['Groucho','Chico','Harp']
pythons = ['Chapamn','Cleese','Gilliam','Jones','Palin']
stooges = ['Moe','Curly','Larry']
tuple_of_lists = marexs,pythons,stooges
print('tuple_of_lists',tuple_of_lists)
list_of_lists = [marexs,pythons,stooges]
print('list_of_lists',list_of_lists)
dict_of_lists = {'Marexs':marexs,'pythons':pythons,'stooges':stooges}
print('dict_of_lists',dict_of_lists)
- 元组可以作为字典的键
houses = {
(44.79,-99.33, 238):'My house',
(23.22,-23.23,233):'The White House'}
print('house',houses)
练习
year_list = [1980,1981,1982,1983,1984,1985]
print('year_list[3]',year_list[3])
print('oldest year ', year_list.index(max(year_list)))
things = ['mozzarella','cinderella','salmonella']
# for name in things:
# name.capitalize()
# print('things',things)
# for name in things:
# name.replace('rella','RELLA')
# print('things',things)
# for name in things:
# name.upper()
# print('things',things)
things = ['mozzarella','cinderella','salmonella']
things = [ name.capitalize() for name in things ]
print('things',things)
things = ['mozzarella','cinderella','salmonella']
things = [ name.upper() for name in things ]
print('things',things)
things = ['mozzarella','cinderella','salmonella']
things = [ name.replace('rella','RELLA') for name in things ]
print('things',things)
things = ['mozzarella','cinderella','salmonella']
things.remove('cinderella')
print('things',things)
- 列表操作
surprise = ['Groucho','Chico','Harpo']
surprise[-1] = surprise[-1][::-1].capitalize()
print('surprise',surprise)
e2f = {'dog':'chien','cat':'chat','walrus':'morse'}
print('e2f',e2f)
print('e2f["walrus"]',e2f["walrus"])
print('e2f.get("walrus")',e2f.get('walrus','Not found'))
f2e = {}
for key, value in e2f.items():
f2e[value] = key
print('f2e',f2e)
print('f2e.get("chien")',f2e.get("chien"))
print('word set:', set(f2e.keys()))
- 多级字典
life = {}
life['animals'] = {'cats':['Henri','Grumpy','Lucy']}
life['plants'] = {}
life['others'] = {}
print('life',life)
print('life fist level key: ',list(life.keys()))
print('life["animals"] keys: ',list(life["animals"].keys()))
print('life["animals"]["cats"]: ',life["animals"]['cats'])