我试图将2D数组中的每个元素转换为字符串。所以例如我想把这样的[['a','b','c'],['x','y','z']]这样的东西转换成这样的东西['abc',' XYZ']。我很确定这是可能的,但我似乎无法理解如何。请告诉我一些事情。谢谢
我已经尝试加入数组并将其转换为字符串和东西,但我似乎无法得到它。我想我可能没有足够的语法知识,这就是我被困的原因。
def combs(a):
if len(a) == 0:
return [[]]
cs = []
for x in combs(a[1:]):
cs = cs + [x, x+[a[0]]]
return cs
a = ["J","a","m","e","s"]
c = combs(a[::-1])
arr = []
for element in c:
arr.append(element)
print(arr)
#I would like to do this to the 'arr' array please.
#Everything else works how I would like it to.
我只想将1d数组中的所有可能组合作为字符串元素。
arr = [ ['a' , 'b' , 'c'] , ['x' , 'y' , 'z'] ]
for index, i in enumerate(arr):
arr[index] = ''.join(i)
print(arr)
缩短版:
arr = [''.join(i) for i in arr]
print(arr)