gpt4 book ai didi

python - 如何将众多类型的列表附加到单个字符串(python)

转载 作者:太空狗 更新时间:2023-10-30 00:53:14 25 4
gpt4 key购买 nike

如果我有这样的列表:

arr = [
["Hi ", "My ", "Name "],
["Is ", "Sally. ", "Born "],
[3, 13, 2010]
]

你如何在一个声明的字符串中得到它

H, My Name Is Sally. Born 3 13 2010

有没有比

更简单的方法
example = ""
for x in range(len(arr)):
for j in range(len(arr[x])):
example = example + str(arr[x][j])
print (example)

最佳答案

你可以使用' '.join:

myList = [
["Hi ", "My ", "Name "],
["Is ", "Sally. ", "Born "],
[3, 13, 2010]
]

sentence = ''.join(str(i)+[' ', ''][type(i) == str] for b in myList for i in b)[:-1]

输出:

'Hi My Name Is Sally. Born 3 13 2010'

关于python - 如何将众多类型的列表附加到单个字符串(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com