[英]Can I use .pop() and .append() on the same item at the same time in Python?
[英]How can i append an item more than once at the same time in Python?
我想多次追加一个项目。 例如
listA = ['AS','23','45']
listB = ['TH','67','78']
listB.append(listA.pop()*3)
print(listA)
# ['AS', '23']
print(listB)
#['TH', '67', '78', '454545']
在打印listB时,它当前给了我以上列表,但我希望它给我# ['TH', '67', '78', '45','45','45']
我怎样才能做到这一点。
尝试使用list.extend()
而不是重复pop()
返回的字符串,而是重复一个单元素列表:
>>> listA = ['AS','23','45']
>>> listB = ['TH','67','78']
>>> listB.extend([listA.pop()]*3)
>>> listB
['TH', '67', '78', '45', '45', '45']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.