简体   繁体   English

python在列表列表中加入字符串

[英]python join strings in list of lists

I have a list of lists of individual litters, however II would like a list of lists of a string.我有一个单独垃圾列表的列表,但是我想要一个字符串列表的列表。

What I have:我有的:

[
  ['a', 'b', 'c', 'd', 'e'],
  ['b', 'c', 'd', 'e', 'a'],
  ['c', 'd', 'e', 'a', 'b'],
  ['d', 'e', 'a', 'b', 'c'],
  ['e', 'a', 'b', 'c', 'd']
]

What I need:我需要的:

[
  ['a b c d e'],
  ['b c d e a'],
  ['c d e a b'],
  ['d e a b c'],
  ['e a b c d']
]

I guess below will work as you wish.我想下面会如你所愿。

list = [
  ['a', 'b', 'c', 'd', 'e'],
  ['b', 'c', 'd', 'e', 'a'],
  ['c', 'd', 'e', 'a', 'b'],
  ['d', 'e', 'a', 'b', 'c'],
  ['e', 'a', 'b', 'c', 'd']
]

newList = [[' '.join(elem)] for elem in list]
print(newList)

列表理解:

result = [[' '.join(inner)] for inner in outer_list]

You can also do it with this你也可以这样做

array = [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j']] 

string = [' '.join(i).split(',') for i in array] 
print(string)

你也可以用map

list(map(lambda x:[' '.join(x)], lst))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM