python 元组 列表、列表问题

51CTO博客_系统升级中
停服升级通知
为了给您全新的博客体验,本站将于本周三(11月22日)22:00至次日7:00进行系统升级,升级期间网站功能暂不可用。
对此给大家带来的不便,敬请谅解!感谢大家对升级工作的支持和理解,谢谢![转载]python&中列表元组字典的区别
python中,有3种内建的数据结构:列表、元组和字典。
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中,这样python就知道你是在指明一个列表。一旦你创建了一个列表,你就可以添加,删除,或者是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。
列表是可以嵌套的。
#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
更多来源:
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the
for item in shoplist:
print 'nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist
print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist
元祖和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。
元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。
#!/usr/bin/python
# Filename: using_tuple.py
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf',
'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant',
'penguin')
Last animal brought from old zoo is penguin
元组与打印语句:
元组最通常的用法是用在打印语句中。
#!/usr/bin/python
# Filename: print_tuple.py
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制。
字典类似于你通过联系人名称查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2
}。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。另外,记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
'Swaroop'&& :
&&&&&&&&&&&&
'Larry'&&&&
: 'larry@wall.org',
&&&&&&&&&&&&
'Matsumoto' : 'matz@ruby-lang.org',
&&&&&&&&&&&&
'Spammer'&& :
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print 'nThere are %d contacts in the address-bookn' %
for name, address in ab.items():
'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
"nGuido's address is %s" % ab['Guido']
列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
使用序列:
#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考
那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。
一般说来,你不需要担心这个,只是在参考上有些细微的效果需要你注意。
#!/usr/bin/python
# Filename: reference.py
print 'Simple Assignment'
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the
same object!
del shoplist[0]
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list
# the 'apple' confirming that they point to the same object
print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
更多字符串的内容:
我们已经在前面详细讨论了字符串。我们还需要知道什么呢?那么,你是否知道字符串也是对象,同样具有方法。这些方法可以完成包括检验一部分字符串和去除空格在内的各种工作。
你在程序中使用的字符串都是str类的对象。这个类的一些有用的方法会在下面这个例子中说明。如果要了解这些方法的完整列表,请参见help(str)。
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。向python列表添加元素的问题
大家好,我是一个python新手,想向各位请教个问题。我建立了一个列表 a,试着用extend向 a 添加元素‘gg',但不知道为什么得到的结果是‘gg'被拆分成了 'g' ,& 'g'两个元素?请高手赐教,非常感谢!
&&& a=['gag','gagh','ggw']
&&& a.append(('aa'))
['gag', 'gagh', 'ggw', 'aa']
&&& a.extend(('gg'))
['gag', 'gagh', 'ggw', 'aa', 'g', 'g']
append()&方法向列表的尾部添加一个新的元素。只接受一个参数。
extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
用extend输入非列表,会自动转为列表,字符就会拆成列表。
--- 共有 4 条评论 ---
: 明白了,非常感谢!
: a.extend(('cd',))这样写才是元组。a.extend(('cd'))这样写你传入的是字符串。
: j.extend(('cd'))这样写是传入字符,不是元组,字符当然会拆开成列表,j.extend(('gd','hsd'))传入是元组,就不会拆开处理了。
建立j=['kioew','oik']
那使用extend向列表j添加元素,j.extend(('cd'))结果是cd被拆分,而j.extend(('gd','hsd'))的gd和hsd为什么没有按字母拆分呢,只是因为有逗号差异这么简单?唉,不懂。
引用来自“雪梨苹果”的评论 append()&方法向列表的尾部添加一个新的元素。只接受一个参数。
extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
用extend输入非列表,会自动转为列表,字符就会拆成列表。 正解}

我要回帖

更多关于 python元组转换为列表 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信