我有一個列表,它由不同的字符串(句子,單詞......)組成,我想將它們連接在一起成爲一個字符串。 我想:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
''.join(kafka) #but strings stay the same as before
我有一個列表,它由不同的字符串(句子,單詞......)組成,我想將它們連接在一起成爲一個字符串。 我想:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
''.join(kafka) #but strings stay the same as before
你需要做的:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
s = ''.join(kafka)
s
現在包含您的連接字符串。
基本上你是在正確的軌道上,但你需要將連接操作分配給一個新的變量或直接使用它。原始列表不會受到影響。
將它們連接起來有或沒有任何空格之間:
no_spaces = ''.join(kafka)
with_spaces = ' '.join(kafka)
print no_spaces
print with_spaces
的原因是
''.join(kafka)
返回,而無需修改卡夫卡新的字符串。嘗試:
my_string = ''.join(kafka)
如果你想句子之間的空間,然後使用:
my_string = ' '.join(kafka)
''.join(kafka)
返回一個字符串。爲了使用它,它存儲到一個變量:
joined = ''.join(kafka)
print joined
注意:您可能想用空格代替' '
加盟。
可以使用加入,
假設你的列表是
['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!']
>>> kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!']
>>>' '.join(kafka)
輸出:
'Das ist ein sch\xf6ner Tag. >>I would like some ice cream and a big cold orange juice!'
並沒有分配的' ''。加入結果(卡夫卡)'的任何東西?如果它本身就在一條線上,它不會有任何影響。 – Kevin