我试图获取一个字符串,并将其附加到列表中包含的每个字符串,然后有一个包含已完成字符串的新列表.例:
list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
Run Code Online (Sandbox Code Playgroud)
我尝试了循环,尝试列表理解,但它是垃圾.一如往常,任何帮助,非常感谢.
gah*_*ooa 268
最简单的方法是使用列表理解:
[s + mystring for s in mylist]
Run Code Online (Sandbox Code Playgroud)
请注意,我避免使用内置名称,list因为阴影或隐藏内置名称,这是非常不好的.
此外,如果您实际上不需要列表,但只需要一个迭代器,则生成器表达式可以更高效(尽管它在短列表中可能不重要):
(s + mystring for s in mylist)
Run Code Online (Sandbox Code Playgroud)
这些功能非常强大,灵活且简洁.每个优秀的python程序员都应该学会使用它们.
Ten*_*she 23
my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
my_new_list = [x + string for x in my_list]
print my_new_list
Run Code Online (Sandbox Code Playgroud)
这将打印:
['foobar', 'fobbar', 'fazbar', 'funkbar']
Run Code Online (Sandbox Code Playgroud)
这是一个使用的简单答案pandas。
import pandas as pd
list1 = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
list2 = (pd.Series(list1) + string).tolist()
list2
# ['foobar', 'fobbar', 'fazbar', 'funkbar']
Run Code Online (Sandbox Code Playgroud)
map 对我来说似乎是这项工作的正确工具。
my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
list2 = list(map(lambda orig_string: orig_string + string, my_list))
Run Code Online (Sandbox Code Playgroud)
请参见本节的更多的例子在函数式编程工具map。
更新更多选项
以下是我遵循的一些方法,我相信还可以有更多。
方法一:
list1 = ['foo', 'fob', 'faz', 'funk']
list2 = [ls+"bar" for ls in list1] # using list comprehension
print(list2)
Run Code Online (Sandbox Code Playgroud)
方法二:
list1 = ['foo', 'fob', 'faz', 'funk']
list2 = list(map(lambda ls: ls+"bar", list1))
print(list2)
Run Code Online (Sandbox Code Playgroud)
方法三:
list1 = ['foo', 'fob', 'faz', 'funk']
addstring = 'bar'
for index, value in enumerate(list1):
list1[index] = addstring + value #this will prepend the string
#list1[index] = value + addstring #this will append the string
Run Code Online (Sandbox Code Playgroud)
方法四:
list1 = ['foo', 'fob', 'faz', 'funk']
addstring = 'bar'
list2 = []
for value in list1:
list2.append(str(value) + "bar")
print(list2)
Run Code Online (Sandbox Code Playgroud)
方法五:
list1 = ['foo', 'fob', 'faz', 'funk']
list2 = list(map(''.join, zip(list1, ["bar"]*len(list1))))
print(list2)
Run Code Online (Sandbox Code Playgroud)
避免使用关键字作为变量,如“list”,将“list”重命名为“list1”
以pythonic方式运行以下实验:
[s + mystring for s in mylist]
Run Code Online (Sandbox Code Playgroud)
似乎比明显使用这样的 for 循环快约 35%:
i = 0
for s in mylist:
mylist[i] = s+mystring
i = i + 1
Run Code Online (Sandbox Code Playgroud)
实验
import random
import string
import time
mystring = '/test/'
l = []
ref_list = []
for i in xrange( 10**6 ):
ref_list.append( ''.join(random.choice(string.ascii_lowercase) for i in range(10)) )
for numOfElements in [5, 10, 15 ]:
l = ref_list*numOfElements
print 'Number of elements:', len(l)
l1 = list( l )
l2 = list( l )
# Method A
start_time = time.time()
l2 = [s + mystring for s in l2]
stop_time = time.time()
dt1 = stop_time - start_time
del l2
#~ print "Method A: %s seconds" % (dt1)
# Method B
start_time = time.time()
i = 0
for s in l1:
l1[i] = s+mystring
i = i + 1
stop_time = time.time()
dt0 = stop_time - start_time
del l1
del l
#~ print "Method B: %s seconds" % (dt0)
print 'Method A is %.1f%% faster than Method B' % ((1 - dt1/dt0)*100)
Run Code Online (Sandbox Code Playgroud)
结果
Number of elements: 5000000
Method A is 38.4% faster than Method B
Number of elements: 10000000
Method A is 33.8% faster than Method B
Number of elements: 15000000
Method A is 35.5% faster than Method B
Run Code Online (Sandbox Code Playgroud)
结合map和format:
>>> list(map('{}bar'.format, ['foo', 'fob', 'faz', 'funk']))
['foobar', 'fobbar', 'fazbar', 'funkbar']
Run Code Online (Sandbox Code Playgroud)
因此,不存在循环变量。
它适用于 Python 2 和 3。(在 Python 3 中可以编写[*map(...)],而在 Python 2 中只能编写map(...).
如果更喜欢模数表达式
>>> list(map('%sbar'.__mod__, ['foo', 'fob', 'faz', 'funk']))
['foobar', 'fobbar', 'fazbar', 'funkbar']
Run Code Online (Sandbox Code Playgroud)
要添加一个可以使用__add__方法
>>> list(map('bar'.__add__, ['foo', 'fob', 'faz', 'funk']))
['barfoo', 'barfob', 'barfaz', 'barfunk']
Run Code Online (Sandbox Code Playgroud)