[英]How to merge 2d array into string in python
I have tried this solution but the problem I am facing is that when I want to convert我已经尝试过这个解决方案,但我面临的问题是当我想转换
[[65, 'D', 'M', 'A', 'H'], [65, 'S', 'I', 'N']]
to string using the method above, what i get is用上面的方法串起来,我得到的是
'65DMAH65SIN'
but what I want is但我想要的是
'65DMAH 65SIN'
ie after every array ends, it gives me space.即在每个数组结束后,它给了我空间。 I am thinking of logics for this but not working.我正在考虑这方面的逻辑,但不起作用。
I would use following comprehension for that task:我会对该任务使用以下理解:
a = [[65, 'D', 'M', 'A', 'H'], [65, 'S', 'I', 'N']]
s = ' '.join(''.join(map(str,i)) for i in a)
print(s)
Output: Output:
65DMAH 65SIN
For every sublist I map it, so every element of sublist become str, then I join elements of every sublist without separator (empty str), which then I join using space.对于每个子列表我 map 它,所以子列表的每个元素都变成 str,然后我加入每个没有分隔符的子列表的元素(空 str),然后我使用空格加入。
Your problem answer is:你的问题答案是:
def solution(List):
result = ''
for i in range(len(string)):
for j in string[i]:
result+=str(j)
result+=' '
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.