将串联连接到列表的好方法?

Sup*_*zGi 5 python string list concatenation

有没有办法有效地连接str和列表?

inside = [] #a list of Items

class Backpack:
    def add(toadd):
        inside += toadd

print "Your backpack contains: " #now what do I do here?
Run Code Online (Sandbox Code Playgroud)

aba*_*ert 9

听起来你只是想尝试将字符串添加到字符串列表中.那只是append:

>>> inside = ['thing', 'other thing']
>>> inside.append('another thing')
>>> inside
['thing', 'other thing', 'another thing']
Run Code Online (Sandbox Code Playgroud)

这里没有具体的字符串; 同样的事情适用于Item实例列表,或字符串列表列表列表,或37种不同类型的37种不同事物的列表.

通常,append将单个内容连接到列表末尾是最有效的方法.如果你想连接一堆东西,并且你已经将它们放在一个列表(或迭代器或其他序列)中,而不是extend一次只进行一次,那么可以一次性完成它们,或者只是+=改为(这意味着与extend列表相同):

>>> inside = ['thing', 'other thing']
>>> in_hand = ['sword', 'lamp']
>>> inside += in_hand
>>> inside
['thing', 'other thing', 'sword', 'lamp']
Run Code Online (Sandbox Code Playgroud)

如果你想稍后将该字符串列表连接成一个字符串,那就是join方法,正如RocketDonkey解释的那样:

>>> ', '.join(inside)
'thing, other thing, another thing'
Run Code Online (Sandbox Code Playgroud)

我猜你想得到一个有点发烧友,并在最后的东西之间放一个"和",如果少于三个就跳过逗号,等等.但如果你知道如何切片列表以及如何使用join,我认为这可以留给读者练习.

如果您试图反过来并将列表连接到字符串,则需要以某种方式将该列表转换为字符串.你可以使用str,但往往不会给你你想要的东西,你会想要像join上面的例子.

无论如何,一旦你有了字符串,你可以将它添加到另一个字符串:

>>> 'Inside = ' + str(inside)
"Inside = ['thing', 'other thing', 'sword', 'lamp']"
>>> 'Inside = ' + ', '.join(inside)
'Inside = thing, other thing, another thing'
Run Code Online (Sandbox Code Playgroud)

如果你有一个不是字符串的东西列表并希望将它们添加到字符串中,你必须决定这些东西的相应字符串表示(除非你满意repr):

>>> class Item(object):
...   def __init__(self, desc):
...     self.desc = desc
...   def __repr__(self):
...     return 'Item(' + repr(self.desc) + ')'
...   def __repr__(self):
...     return self.desc
...
>>> inside = [Item('thing'), Item('other thing')]
>>> 'Inside = ' + repr(inside)
... "Inside = [Item('thing'), Item('other thing')]"
>>> 'Inside = ' + str(inside)
... "Inside = [Item('thing'), Item('other thing')]"
>>> 'Inside = ' + ', '.join(str(i) for i in inside)
... 'Inside = thing, other thing'
Run Code Online (Sandbox Code Playgroud)

请注意,只需要调用各个项目strItems调用列表repr; 如果你想打电话str给他们,你必须明确地做; 这就是这个str(i) for i in inside部分的用途.

把它们放在一起:

class Backpack:
    def __init__(self):
        self.inside = []
    def add(self, toadd):
        self.inside.append(toadd)
    def addmany(self, listtoadd):
        self.inside += listtoadd
    def __str__(self):
        return ', '.join(str(i) for i in self.inside)

pack = Backpack()
pack.add('thing')
pack.add('other thing')
pack.add('another thing')
print 'Your backpack contains:', pack
Run Code Online (Sandbox Code Playgroud)

当你运行它时,它将打印:

Your backpack contains: thing, other thing, another thing
Run Code Online (Sandbox Code Playgroud)


Roc*_*key 5

你可以试试这个:

In [4]: s = 'Your backpack contains '

In [5]: l = ['item1', 'item2', 'item3']

In [6]: print s + ', '.join(l)
Your backpack contains item1, item2, item3
Run Code Online (Sandbox Code Playgroud)

join与其设置中的其他Python方法相比,该方法有点奇怪,但在这种情况下,它意味着"获取此列表并将其转换为字符串,将元素与逗号和空格连接在一起".这有点奇怪,因为你指定了首先加入的字符串,这有点不同寻常,但很快成为第二天性:)请参阅此处讨论.

如果您要将项目添加到inside(列表),将项目添加到列表的主要方法是使用该append方法.然后join,您可以将所有项目组合在一起作为字符串:

In [11]: inside = []

In [12]: inside.append('item1')

In [13]: inside.append('item2')

In [14]: inside.append('item3')

In [15]: print 'Your backpack contains ' + ', '.join(inside)
Your backpack contains item1, item2, item3
Run Code Online (Sandbox Code Playgroud)