将列表(对象)列表转换为字符串列表
Convert a list of lists (objects) into a list of strings
有什么方法可以转换它:
listofadds = [(1, 'fakestreet', 'city', 'ab12 1ab'), (2, 'sillystreet', 'othercity', 'dc12 4ef')]
进入这个:
['1 fakestreet city ab12 1ab', '2 sillystreet othercity dc12 4ef']
到目前为止,我已经能够转换其中之一,方法是将整数转换为字符串然后连接:
f = listofadds[0]
h = [str(i) for i in f]
g = ' '
s = g.join((h))
>>> '1 fakestreet city ab12 1ab'
但我不确定如何使用 for 循环成功地为下一个以及出现在原始列表中的任何其他人重复此操作!
如有任何帮助,我们将不胜感激。
listofadds = [(1, 'fakestreet', 'city', 'ab12 1ab'), (2, 'sillystreet', 'othercity', 'dc12 4ef')]
new_list = [' '.join([str(i) for i in curr]) for curr in listofadds]
print(new_list)
这是另一个例子:
new_list = []
for object in listofadds:
element_string = " ".join([str(element) for element in object])
new_list.append(element_string)
有什么方法可以转换它:
listofadds = [(1, 'fakestreet', 'city', 'ab12 1ab'), (2, 'sillystreet', 'othercity', 'dc12 4ef')]
进入这个:
['1 fakestreet city ab12 1ab', '2 sillystreet othercity dc12 4ef']
到目前为止,我已经能够转换其中之一,方法是将整数转换为字符串然后连接:
f = listofadds[0]
h = [str(i) for i in f]
g = ' '
s = g.join((h))
>>> '1 fakestreet city ab12 1ab'
但我不确定如何使用 for 循环成功地为下一个以及出现在原始列表中的任何其他人重复此操作!
如有任何帮助,我们将不胜感激。
listofadds = [(1, 'fakestreet', 'city', 'ab12 1ab'), (2, 'sillystreet', 'othercity', 'dc12 4ef')]
new_list = [' '.join([str(i) for i in curr]) for curr in listofadds]
print(new_list)
这是另一个例子:
new_list = []
for object in listofadds:
element_string = " ".join([str(element) for element in object])
new_list.append(element_string)