Dictionary
Dictionary, 字典,是Python的内置数据类型之一,定义了键值之间一一对应的关系。
Python中的dictionary就像Perl中的Hash数组。Python中的dictionary就像Java中的Hashtable实例。Dictionary不能有重复的key,任何时候都可以加入新的key-value对Dictionary的value可以是任意类型Dictionary的key一旦定义之后不可变,故其类型可以为字符串、数字常量、tuple,但不能为list。Dictionary的删除:
In:a = {
'1':
'a',
'2':
'b'}
In:
del a[
'1']
Out:删除某一键值对
In: a.clear()
Out:清空所有元素
Out:{}
List
List相当于Java的ArrayList类
In: li = [
'a',
'b',
'c']
In: li.append([
'a',
'b',
'c'])
Out:[
'a',
'b',
'c',[
'a',
'b',
'c']]
In:li.extend([
'd',
'e',
'f'])
Out:[
'a',
'b',
'c',[
'a',
'b',
'c'],
'd',
'e',
'f']
In:li.index(
'a')
Out:
0
列表的删除
In:li.remove(
'a')
Out:[
'b',
'c',[
'a',
'b',
'c'],
'd',
'e',
'f']
In:li.pop()
Out:
'f'
列表的运算符
In:li = li+=[
'd',
'e']
Out:[
'b',
'c',[
'a',
'b',
'c'],
'd',
'e',
'd',
'e']
'''作用和list等同,不过+=相当于拷贝后用新值返回,extend只修改存在的list,所以对大型list来说,extend更快'''
In:a = [
1]*
3
Out:[
1,
1,
1]
Tuple
Tuple, 元组,没有方法Tuple比list操作速度快,如果您定义了一个值的常量集,并且不需修改,请使用Tuple写保护Tuple冻结一个list,list解冻一个Tuple
格式化字符串
In:age =
27
In:hobby =
'playing football'
In:
print "Luhan is %d years old and he likes %s" % (age,hobby)
Out:
"Luhan is 27 years old and he likes playing football"
其它
join 与 split方法列表式
[do(f)
for f
in list]