List 用Python连接列表
我想连接两个列表:List 用Python连接列表,list,concatenation,List,Concatenation,我想连接两个列表: list_a = ["hello", "world"] list_b = ["a", "b", "c", "d"] 并得到如下输出: list_c = ["hello a", "hello b", "hello c", "hello d", "world a", "world b", "world c", "world d"] 第二个列表基本上是从a到z,并创建与列表a的组合。您可以使用列表理解: from functools import reduce list_
list_a = ["hello", "world"]
list_b = ["a", "b", "c", "d"]
并得到如下输出:
list_c = ["hello a", "hello b", "hello c", "hello d", "world a", "world b", "world c", "world d"]
第二个列表基本上是从a到z,并创建与列表a的组合。您可以使用列表理解:
from functools import reduce
list_a = ["hello", "world"]
list_b = ["a", "b", "c", "d"]
def cross_join_lists(word):
return [f'{word} {letter}' for letter in list_b]
def concat_lists(final_list, a_list):
return final_list + a_list
joined_lists = list(map(cross_join_lists, list_a))
list_c = reduce(concat_lists, joined_lists)
print(list_c)
new_list = [f"{i} {j}" for i in list_a for j in list_b]
# output: ['hello a', 'hello b', 'hello c', 'hello d', 'world a', 'world b',
'world c', 'world d']
并非每个例程/函数/方法/运算符/关键字都有标记。请从列表中选择标签并阅读其Wiki。标记用于关系数据库操作符。