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

来源:互联网 发布:linux 查看进度 编辑:程序博客网 时间:2024/05/16 23:33

如何使用Python容器

分子数据结构

列表(list)

# create a listempty_list = []another_empty_list = list()print empty_list,'\n',another_empty_listset_fruit = ['Apple', 'Banana', 'Lemon']noset_fruit = ['Apple', 'Apple', 'Banana']print set_fruit,'\n', noset_fruitprint "--- --- --- --- --- --- --- --- --- --- "# copy a listls_a = [11,22,33]ls_b = ls_aprint ls_a, ls_bls_a[0] = 0print ls_a, ls_bls_a = [11,22,33]ls_c= list(ls_a)ls_d= ls_a[:]# ls_e = ls_a.copy()print ls_a, ls_c, ls_dls_a[0] = 0print ls_a, ls_c, ls_dprint "--- --- --- --- --- --- --- --- --- --- "# convert to listprint list('kitty')print '2020/2/22'.split('/')print ', '.join(['Apple', 'Apple', 'Banana'])print "--- --- --- --- --- --- --- --- --- --- "# operate a listnoset_fruit = ['Apple', 'Apple', 'Banana']noset_fruit.append('Banana')print noset_fruitnoset_fruit.insert(3, 'Banana')print noset_fruitnoset_fruit = ['Apple', 'Apple', 'Banana']noset_fruit.extend(noset_fruit)print noset_fruitnoset_fruit = ['Apple', 'Apple', 'Banana']noset_fruit= noset_fruit+noset_fruitprint noset_fruitnoset_fruit = ['Apple', 'Apple', 'Banana']noset_fruit.remove('Apple')print noset_fruitnoset_fruit = ['Apple', 'Apple', 'Banana']del noset_fruit[0]print noset_fruitnoset_fruit.pop()print noset_fruitnoset_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 propertynoset_fruit = ['Apple', 'Apple', 'Banana']print len(noset_fruit), noset_fruit.count('Apple')print 'Apple' in noset_fruitprint noset_fruit.index('Apple')print noset_fruit.count('Apple')
() ('',) ('', '')('Apple', 'Banana', 'Lemon') ('Apple', 'Apple', 'Banana')--- --- --- --- --- --- --- --- --- --- 

元祖(tuple)

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

字典(dict)

# create a dictdict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}print dict_name_fonenumprint "--- --- --- --- --- --- --- --- --- --- --- "# convert to a dictprint dict([['AllenMoore','1234'],['AaronMose','4321']])print dict((['AllenMoore','1234'],['AaronMose','4321']))print "--- --- --- --- --- --- --- --- --- --- --- "# copy a dict# cp_dict=(['AllenMoore','1234'],['AaronMose','4321']).copy()# print cp_dictprint "--- --- --- --- --- --- --- --- --- --- --- "# operate a dictdict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}dict_name_fonenum['AllenMoore']='00001234'print dict_name_fonenumdict_other_fonenum = {'AlMo':'11223344','AaMo':'44332211'}dict_name_fonenum.update(dict_other_fonenum)print dict_name_fonenumdel dict_name_fonenum['AlMo']print dict_name_fonenumdict_name_fonenum.clear()print dict_name_fonenumdict_name_fonenum = {'AllenMoore':'1234','AaronMose':'4321'}print dict_name_fonenum['AllenMoore']print dict_name_fonenum.get('AaronMose')print "--- --- --- --- --- --- --- --- --- --- --- "print 'AllenMoore' in dict_name_fonenumprint 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'}{}12344321--- --- --- --- --- --- --- --- --- --- --- True['AaronMose', 'AllenMoore']['4321', '1234'][('AaronMose', '4321'), ('AllenMoore', '1234')]

集合

# create a setempty_set = set()print empty_setprint {1,2,3,4,5}print type({1,2,3,4,5})print "--- --- --- --- --- --- --- --- --- --- --- "# convert a setprint 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'])FalseFalseTrueTrue--- --- --- --- --- --- --- --- --- --- --- 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']
MAMAMA

分子组合

阅读全文
0 0
原创粉丝点击