【脚本语言系列】关于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
- 【脚本语言系列】关于Python容器,你需要知道的事
- 【脚本语言系列】关于Python基础知识容器,你需要知道的事
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python调试报错,你需要知道的事
- 【脚本语言系列】关于Python开发环境搭建,你需要知道的事
- 【脚本语言系列】关于Python数值计算NumPy, 你需要知道的事
- 【脚本语言系列】关于Python测试工具lettuce, 你需要知道的事
- 【脚本语言系列】关于Python测试工具mock, 你需要知道的事
- 【脚本语言系列】关于Python测试工具unittest, 你需要知道的事
- 【脚本语言系列】关于Python测试工具docstrings, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pylint, 你需要知道的事
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事
- 【脚本语言系列】关于Python代码分析工具line_profiler,你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 【脚本语言系列】关于Python机器学习scikits-learn,你需要知道的事
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事
- 【脚本语言系列】关于Python统计分析statsmodel,你需要知道的事
- Codeforces Round #420 (Div.2)
- 判断单链表是否形成环形结构,快慢指针
- weight和weightSum
- java线程
- Lintcode78 Longest Common On Prefix solution 题解
- 【脚本语言系列】关于Python容器,你需要知道的事
- 11. fae来协助调试_办法
- Web图书管理系统---分页、上传、下载、增删改查
- html事件(onclick、onchange等)传对象作为参数
- iOS_UIDevice
- [week 18][Leetcode] Clone Graph
- 应用系统之间数据传输的几种方式
- UML中活动图、状态图、流程图的区别
- 在linux中输入locate出现locate: can not stat ()的错误