中国科技会堂,会声会影12安装教程,妾不如妻 王野
本文实例讲述了Python中列表与元组的乘法操作。分享给大家供大家参考,具体如下:
直接上code吧,还可以这么玩儿
列表乘法:
li=[1,] li=li*3 print(li)
out:
[1, 1, 1]
元组乘法:
>>> t=(1,2) >>> t*3 (1, 2, 1, 2, 1, 2)
但字典,集合不能这么玩
例如:
>>> dict1={'k1':1,'k2':2} >>> dict1*2 #报错 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> dict1*2 TypeError: unsupported operand type(s) for *: 'dict' and 'int' >>>
另外,字符串也可以使用乘法,例如:
>>> str1='hello python!' >>> str1*3 'hello python!hello python!hello python!' >>>
注意:在元组的定义中:(1,)
代表着元组,(1,)*3
可得到(1, 1, 1)
而(1)则只会被视为一个整数,
(1)*3
会得到3
更多Python相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家Python程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论