码迷,mamicode.com
首页 > 编程语言 > 详细

python Memo

时间:2015-12-24 13:21:19      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

list&tuple 运算

乘以constant

技术分享
>>> x = ((1,2),)
>>> x*2
((1, 2), (1, 2))
>>> x = ((1,2))
>>> x*2
(1, 2, 1, 2)
>>> 
View Code

从上面可以看出,tuple或者list和一个常数相乘,会复制元素得到一个新的tuple或list,需要注意的是有无逗号,这将决定是复制元素还是 "子tuple"。

tuple或list相加

技术分享
>>> a = [1,2,3,4,5]
>>> a + [6]
[1, 2, 3, 4, 5, 6]
>>> a = (1,2,3,4,5)
>>> a +(6)

Traceback (most recent call last):
  File "<pyshell#189>", line 1, in <module>
    a +(6)
TypeError: can only concatenate tuple (not "int") to tuple
>>> a + (6,)
(1, 2, 3, 4, 5, 6)
>>> type((6))
<type int>
>>> type([6])
<type list>
>>> 
View Code

  tuple和list在此处略有区别

 

python Memo

标签:

原文地址:http://www.cnblogs.com/vin-yuan/p/5072661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!