python数据结构

来源:互联网 发布:淘宝装修模板怎么使用 编辑:程序博客网 时间:2024/04/30 07:17

列表list用中括号[ ],元组用圆括号():

声明空元组 myempty=()

只含有一个元素的元组 :oneitem=(2, )   # 后面有逗号隔开,python才能区分逗号前的表达式

以上两者都用逗号隔开。列表是可变的,元组是不可变的。


字典的使用:

info={'chen':22, 'lin':21, 'future': 70}

for name,age in info.items():
print '%s is %d' % (name, age)


chen is 22
lin is 21
future is 70


>>>shoplist=['banana', 'pear', 'rice']

>>>mylist=shoplist   #这两个变量名指向同一个存储空间

>>>del shoplist[0]

>>>print mylist
['pear', 'rice']


>>>shoplist.append('apple')

>>>mylist=shoplist[:]   #取出shoplist中所有元素存入mylist中,则对两者的操作互不影响
>>> print mylist
['pear', 'rice', 'apple']

>>> del shoplist[1]
>>> print mylist
['pear', 'rice', 'apple']

0 0
原创粉丝点击