怎样在python中向list同时添加多个元素,不用循环

挑葱 发布于 2016/05/04 21:04
阅读 2K+
收藏 0

L=[]

例如添加类似:

fa,guo,mei,shi

L=["fa","guo","mei","shi"]

加载中
1
agateriver
agateriver

L=[]
s = "fa,guo,mei,shi"
L = s.split(",")
print L
#Out: ['fa','guo','mei','shi']



1
502BadGateway
502BadGateway

条件只是同时添加多个元素?

L = ["fa","guo","mei","shi"]

L.extend(["tong","shi","tian","jia"])

print L

Out: ['fa', 'guo', 'mei', 'shi', 'tong', 'shi', 'tian', 'jia']



1
fromdtor
fromdtor
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = ["fa","guo","mei","shi"]
>>> L = []
>>> L.extend(x)
>>> L
['fa', 'guo', 'mei', 'shi']
>>>




返回顶部
顶部