【脚本语言系列】关于Python容器,你需要知道的事

时间:2023-01-25 22:59:22

如何使用Python容器

分子数据结构

列表(list)

# create a list
empty_list = []
another_empty_list = list()
print empty_list,'\n',another_empty_list
set_fruit = ['Apple', 'Banana', 'Lemon']
noset_fruit = ['Apple', 'Apple', 'Banana']
print set_fruit,'\n', noset_fruit
print "--- --- --- --- --- --- --- --- --- --- "
# copy a list
ls_a = [11,22,33]
ls_b = ls_a
print ls_a, ls_b
ls_a[0] = 0
print ls_a, ls_b
ls_a = [11,22,33]
ls_c= list(ls_a)
ls_d= ls_a[:]
# ls_e = ls_a.copy()
print ls_a, ls_c, ls_d
ls_a[0] = 0
print ls_a, ls_c, ls_d
print "--- --- --- --- --- --- --- --- --- --- "
# convert to list
print list('kitty')
print '2020/2/22'.split('/')
print ', '.join(['Apple', 'Apple', 'Banana'])
print "--- --- --- --- --- --- --- --- --- --- "
# operate a list
noset_fruit = ['Apple', 'Apple', 'Banana']
noset_fruit.append('Banana')
print noset_fruit
noset_fruit.insert(3, 'Banana')
print noset_fruit
noset_fruit = ['Apple', 'Apple', 'Banana']
noset_fruit.extend(noset_fruit)
print noset_fruit
noset_fruit = ['Apple', 'Apple', 'Banana']
noset_fruit= noset_fruit+noset_fruit
print noset_fruit

noset_fruit = ['Apple', 'Apple', 'Banana']
noset_fruit.remove('Apple')
print noset_fruit
noset_fruit = ['Apple', 'Apple', 'Banana']
del noset_fruit[0]
print noset_fruit
noset_fruit.pop()
print noset_fruit

noset_fruit = ['Apple', 'Apple', 'Banana']
print noset_fruit[-1::]
print noset_fruit[0::]
print noset_fruit[::1]
print noset_fruit[::-1]

print ['Apple', 'Apple', 'Banana'].sort(reverse=True)
print sorted(['Apple', 'Apple', 'Banana'])
print "--- --- --- --- --- --- --- --- --- --- "
# show the property
noset_fruit = ['Apple', 'Apple', 'Banana']
print len(noset_fruit), noset_fruit.count('Apple')
print 'Apple' in noset_fruit
print noset_fruit.index('Apple')
print noset_fruit.count('Apple')
() 
('',) 
('', '')
('Apple', 'Banana', 'Lemon') 
('Apple', 'Apple', 'Banana')
--- --- --- --- --- --- --- --- --- --- 

元祖(tuple)

# create a tuple
empty_tuple = ()
another_tuple = '',
theother_tuple = '','',
print empty_tuple,'\n',another_tuple,'\n',theother_tuple
set_fruit = ('Apple', 'Banana', 'Lemon')
one_fruit, two_fruit, three_fruit = set_fruit
print one_fruit, two_fruit, three_fruit
() 
('',) 
('', '')
Apple Banana Lemon

字典(dict)

# create a dict
dict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}
print dict_name_fonenum
print "--- --- --- --- --- --- --- --- --- --- --- "
# convert to a dict
print dict([['AllenMoore','1234'],['AaronMose','4321']])
print dict((['AllenMoore','1234'],['AaronMose','4321']))
print "--- --- --- --- --- --- --- --- --- --- --- "
# copy a dict
# cp_dict=(['AllenMoore','1234'],['AaronMose','4321']).copy()
# print cp_dict
print "--- --- --- --- --- --- --- --- --- --- --- "
# operate a dict
dict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}
dict_name_fonenum['AllenMoore']='00001234'
print dict_name_fonenum
dict_other_fonenum = {'AlMo':'11223344','AaMo':'44332211'}
dict_name_fonenum.update(dict_other_fonenum)
print dict_name_fonenum
del dict_name_fonenum['AlMo']
print dict_name_fonenum
dict_name_fonenum.clear()
print dict_name_fonenum

dict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}
print dict_name_fonenum['AllenMoore']
print dict_name_fonenum.get('AaronMose')
print "--- --- --- --- --- --- --- --- --- --- --- "
print 'AllenMoore' in dict_name_fonenum
print dict_name_fonenum.keys()
print dict_name_fonenum.values()
print dict_name_fonenum.items()
{'AaronMose': '4321', 'AllenMoore': '1234'}
--- --- --- --- --- --- --- --- --- --- --- 
{'AaronMose': '4321', 'AllenMoore': '1234'}
{'AaronMose': '4321', 'AllenMoore': '1234'}
--- --- --- --- --- --- --- --- --- --- --- 
--- --- --- --- --- --- --- --- --- --- --- 
{'AaronMose': '4321', 'AllenMoore': '00001234'}
{'AaMo': '44332211', 'AlMo': '11223344', 'AaronMose': '4321', 'AllenMoore': '00001234'}
{'AaMo': '44332211', 'AaronMose': '4321', 'AllenMoore': '00001234'}
{}
1234
4321
--- --- --- --- --- --- --- --- --- --- --- 
True
['AaronMose', 'AllenMoore']
['4321', '1234']
[('AaronMose', '4321'), ('AllenMoore', '1234')]

集合

# create a set
empty_set = set()
print empty_set
print {1,2,3,4,5}
print type({1,2,3,4,5})
print "--- --- --- --- --- --- --- --- --- --- --- "
# convert a set
print set('allenmoore')
print set(['Allen','Moore','Aaron','Mose'])
print set(('Allen','Moore','Aaron','Mose'))
print "--- --- --- --- --- --- --- --- --- --- --- "
print {'Allen','Moore','Alan'} | {'Aaron','Moore','Ellen'}
print {'Allen','Moore','Alan'}.union({'Aaron','Moore','Ellen'})
print {'Allen','Moore','Alan'} & {'Aaron','Moore','Ellen'}
print {'Allen','Moore','Alan'}.intersection({'Aaron','Moore','Ellen'})
print {'Allen','Moore','Alan'} - {'Aaron','Moore','Ellen'}
print {'Allen','Moore','Alan'}.difference({'Aaron','Moore','Ellen'})
print {'Allen','Moore','Alan'} ^ {'Aaron','Moore','Ellen'}
print {'Allen','Moore','Alan'}.symmetric_difference({'Aaron','Moore','Ellen'})
print {'Allen','Moore'} > {'Allen','Moore','Aaron','Mose'}
print {'Allen','Moore'}.issuperset({'Allen','Moore','Aaron','Mose'})
print {'Allen','Moore'} <= {'Allen','Moore','Aaron','Mose'}
print {'Allen','Moore'}.issubset({'Allen','Moore','Aaron','Mose'})
print "--- --- --- --- --- --- --- --- --- --- --- "
print 'AllenMoore' in {'Allen','Moore','Aaron','Mose'}
set([])
set([1, 2, 3, 4, 5])
<type 'set'>
--- --- --- --- --- --- --- --- --- --- --- 
set(['a', 'e', 'm', 'l', 'o', 'n', 'r'])
set(['Mose', 'Allen', 'Aaron', 'Moore'])
set(['Mose', 'Allen', 'Aaron', 'Moore'])
--- --- --- --- --- --- --- --- --- --- --- 
set(['Aaron', 'Moore', 'Allen', 'Alan', 'Ellen'])
set(['Aaron', 'Moore', 'Allen', 'Alan', 'Ellen'])
set(['Moore'])
set(['Moore'])
set(['Allen', 'Alan'])
set(['Allen', 'Alan'])
set(['Allen', 'Aaron', 'Alan', 'Ellen'])
set(['Allen', 'Aaron', 'Alan', 'Ellen'])
False
False
True
True
--- --- --- --- --- --- --- --- --- --- --- 
False

相互比较

true_list = ['AM','MA']
true_tuple = ('AM','MA')
true_dict = {'AM':'MA'}
print true_list[1]
print true_tuple[1]
print true_dict['AM']
MA
MA
MA

分子组合