Python Container Dictionary And List
来源:互联网 发布:js 导航栏 编辑:程序博客网 时间:2024/09/21 09:02
在我理解看来 dictinary 就是键值对 是可变的 可以增加减少键值对
>>> d={'one'=1,'two'=2}
File "<stdin>", line 1
d={'one'=1,'two'=2}
^
SyntaxError: invalid syntax
>>> d={'one'=1:'two'=2}
File "<stdin>", line 1
d={'one'=1:'two'=2}
^
SyntaxError: invalid syntax
>>> d={'one':1,'two':2}
>>> d
{'two': 2, 'one': 1}
>>> dd=dict('three'=3,'four'=4,**d)
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
>>> dd=dict('three'=3,'four'=4,**d)
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
>>> dd=dict(three=3,four=4,**d)
>>> dd
{'four': 4, 'one': 1, 'three': 3, 'two': 2}
dict的两种初始话方法
是否包含某个元素
>>> 'four' in dd
True
>>> 'five' in dd
False
遍历
>>> for k,v in dd.items():print(k,v)
...
four 4
one 1
three 3
two 2
get某个键值 可以如果没有的话可以返回空或者给定默认值
>>> dd.get('five',None)
>>> dd.get('five','5')
'5'
>>> dd.get('five',5)
而这样 没有该元素的话会报错
>>> dd['five']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'five'
>>> dd['one']
1
5
>>> dd.get('five')
获取某个值 并将其从dic中删除
>>> dd.pop('one')
1
>>> dd
{'four': 4, 'three': 3, 'two': 2}
若想直接删除
>>> del dd['two']
>>> dd
{'four': 4, 'three': 3}
>>>
list
>>> l=[1,2,3]
>>> l.extend(range(10)) #添加另外的list
>>> l
[1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l.insert(0,11) #指定下标添加元素
>>> l
[11, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l.pop(0) #得到下标元素并去除
11
>>> l.push(12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'push'
>>> l.append(12) # 队尾添加一个元素
>>> l
[1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
>>> l.remove(12) #去除值=12的第一个元素
>>> l
[1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l.remove(1)
>>> l
[2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- Python Container Dictionary And List
- Python Dictionary and List
- Python Container Tuple and List
- python dictionary list
- python下 list/dictionary
- (sequence container) vector and list
- python 中List 与dictionary
- Python List Comprehension, Dictionary Comprehension
- Python高级特性之:List Comprehensions、Generator、Dictionary and set comprehensions
- Python高级特性之:List Comprehensions、Generator、Dictionary and set comprehensions
- 第三章Python解析list,dictionary,set
- Python中的List,Tuple和Dictionary
- 【Python基础】之set, list, dictionary
- python内置类型(list,dictionary, tuple , string, )
- Python基础教程(六):list、tuple、dictionary
- python基础[list,tuple,dictionary,set]
- python中的list,tuple 和 dictionary
- Python list and tuple
- 1408181855-hd-A+B for Input-Output Practice (IV).cpp
- hdu1598 MST
- UVA 10048 - Audiophobia(flody算法应用)
- POJ 1459 Power Network(EK)
- Unable to create project from archetype Error merging velocity templates 解决办法
- Python Container Dictionary And List
- 解读Unity中的CG编写Shader系列1——初识CG
- linux文件系统基础知识
- cursor 游标
- 黑马程序员---Java高新技术反射知识点总结
- MyEclipse默认workplace的问题
- tftp服务器环境搭建
- 基础图论算法导引
- OC之继承、初始化方法