str と list を効率的に連結する方法はありますか?
inside = [] #a list of Items
class Backpack:
def add(toadd):
inside += toadd
print "Your backpack contains: " #now what do I do here?
str と list を効率的に連結する方法はありますか?
inside = [] #a list of Items
class Backpack:
def add(toadd):
inside += toadd
print "Your backpack contains: " #now what do I do here?
文字列のリストに文字列を追加しようとしているようです。それはただappend
です:
>>> inside = ['thing', 'other thing']
>>> inside.append('another thing')
>>> inside
['thing', 'other thing', 'another thing']
ここでは文字列に固有のものは何もありません。Item
インスタンスのリスト、文字列のリストのリストのリスト、または 37 の異なるタイプの 37 の異なるもののリストに対しても同じことが機能します。
一般にappend
、リストの最後に単一のものを連結する最も効率的な方法です。たくさんのものを連結したい場合、すでにそれらをリスト (またはイテレータやその他のシーケンス) に持っている場合は、それらを一度に 1 つずつ行うのではなく、 を使用extend
してすべてを一度に行うか、+=
代わりに (つまり、extend
リストの場合と同じです):
>>> inside = ['thing', 'other thing']
>>> in_hand = ['sword', 'lamp']
>>> inside += in_hand
>>> inside
['thing', 'other thing', 'sword', 'lamp']
後でその文字列のリストを単一の文字列に連結したい場合はjoin
、RocketDonkey が説明しているように、それがメソッドです。
>>> ', '.join(inside)
'thing, other thing, another thing'
少し手の込んだものにして、最後のものの間に「and」を入れたり、3つ未満の場合はコンマをスキップしたりしたいと思います.しかし、リストをスライスする方法と使用方法を知っている場合はjoin
、読者の演習として残しておくことができると思います。
逆にリストを文字列に連結しようとしている場合は、何らかの方法でそのリストを文字列に変換する必要があります。をそのまま使用することもできますが、多くの場合、それでは目的が得られず、上記の例のようstr
なものが必要になります。join
とにかく、文字列を取得したら、それを他の文字列に追加するだけです。
>>> 'Inside = ' + str(inside)
"Inside = ['thing', 'other thing', 'sword', 'lamp']"
>>> 'Inside = ' + ', '.join(inside)
'Inside = thing, other thing, another thing'
文字列ではないもののリストがあり、それらを文字列に追加したい場合は、それらのものの適切な文字列表現を決定する必要があります (に満足しない限り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'
s のstr
リストを呼び出すだけで、個々のアイテムが呼び出されることに注意してください。それらを呼び出したい場合は、明示的に行う必要があります。そのためのパーツです。Item
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
これを実行すると、次のように出力されます。
Your backpack contains: thing, other thing, another thing
You could try this:
In [4]: s = 'Your backpack contains '
In [5]: l = ['item1', 'item2', 'item3']
In [6]: print s + ', '.join(l)
Your backpack contains item1, item2, item3
The join
method is a bit odd compared to other Python methods in its setup, but in this case it means 'Take this list and convert it to a string, joining the elements together with a comma and a space'. It is a bit odd because you specify the string with which to join first, which is a little outside of the ordinary but becomes second nature soon :) See here for a discussion.
If you are looking to add items to inside
(a list), the primary way to add items to a list is to use the append
method. You then use join
to bring all the items together as a string:
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