如何从两个字符串列表的笛卡尔积创建列表
How to create lists from a cartesian product of two string lists
我有两个字符串列表,如下所示:
print(objects_list)
print(verb_list)
他们输出:
['parking_meter', 'sink', 'teddy_bear']
['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install',
'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze',
'stab', 'straddle', 'talk_on']
我想要这两个列表的产品,格式如下:
[['parking_meter','sail'],['parking_meter','fill'],['parking_meter','fly']......]
我试过这个代码
list3 = [[str(l),str(n)] for l in objects_list for n in verb_list]
print(list3)
但它输出:
[['[', '['], ['[', "'"], ['[', 's'], ['[', 'a'], ['[', 'i'], ['[',
'l'], ['[', "'"], ['[', ','], ['[', ' '], ['[', "'"], ['[', 'f'],
['[', 'i'], ['[', 'l'], ['[', 'l'], ['[', "'"], ['[', ','], ['[', '
'], ['[', "'"], ['[', 'f'], ['[', 'l'], ['[', 'y'], [.....
有什么解决办法吗?
试试这个,使用 itertools 库。有一个名为 product 的函数可以让你得到笛卡尔积。
import itertools
x = ['parking_meter', 'sink', 'teddy_bear']
y = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on']
然后你可以做这个 2-liner,(因为乘积函数 returns 一个元组但你需要一个列表)
z = list(itertools.product(x,y))
z = [list(z) for z in z]
或者这个 1-liner,
z = [list(tup) for tup in list(itertools.product(x, y))]
那我,
print (z)
这给了我
[['parking_meter', 'sail'],
['parking_meter', 'fill'],
['parking_meter', 'fly'],
['parking_meter', 'greet'],
['parking_meter', 'hit'],
['parking_meter', 'hose'],
['parking_meter', 'hunt'],
['parking_meter', 'install'],
['parking_meter', 'kick'],
['parking_meter', 'launch'],
['parking_meter', 'move'],
['parking_meter', 'pick'],
['parking_meter', 'repair'],
['parking_meter', 'sit_at'],
['parking_meter', 'squeeze'],
['parking_meter', 'stab'],
['parking_meter', 'straddle'],
['parking_meter', 'talk_on'],
['sink', 'sail'],
['sink', 'fill'],
['sink', 'fly'],......
您的代码应该可以正常工作。我怀疑您的实际代码中还有一些其他不必要的 str() 函数调用:
objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose',
'hunt', 'install', 'kick', 'launch', 'move',
'pick', 'repair', 'sit_at', 'squeeze', 'stab',
'straddle', 'talk_on']
list3 = [[o,v] for o in objects_list for v in verb_list]
[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly'], ['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]
从您得到的输出来看,您的输入列表似乎是字符串,而不是列表。
就好像 objects_list
是 print(objects_list)
的输出,而不是列表本身。
因此,当遍历 objects_list
时,您不是遍历列表项,而是遍历其字符串表示的字符:
"["
"'"
"p"
"a"
...
你的代码在其他方面是正确的,除了你不需要在列表理解中将元素转换为字符串。
list3 = [[l, n] for l in objects_list for n in verb_list]
你的代码工作得很好并且给了我预期的输出。我不确定是否还有其他问题。可能是您的列表没有像我一样声明...如果您是从其他来源创建此列表...您应该检查格式。
注意:我在 Python 3.9.5
上有 运行 这段代码
objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on']
list3 = [[str(l),str(n)] for l in objects_list for n in verb_list]
print(list3)
Output:
[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly']['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]
我有两个字符串列表,如下所示:
print(objects_list)
print(verb_list)
他们输出:
['parking_meter', 'sink', 'teddy_bear']
['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install',
'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze',
'stab', 'straddle', 'talk_on']
我想要这两个列表的产品,格式如下:
[['parking_meter','sail'],['parking_meter','fill'],['parking_meter','fly']......]
我试过这个代码
list3 = [[str(l),str(n)] for l in objects_list for n in verb_list]
print(list3)
但它输出:
[['[', '['], ['[', "'"], ['[', 's'], ['[', 'a'], ['[', 'i'], ['[',
'l'], ['[', "'"], ['[', ','], ['[', ' '], ['[', "'"], ['[', 'f'],
['[', 'i'], ['[', 'l'], ['[', 'l'], ['[', "'"], ['[', ','], ['[', '
'], ['[', "'"], ['[', 'f'], ['[', 'l'], ['[', 'y'], [.....
有什么解决办法吗?
试试这个,使用 itertools 库。有一个名为 product 的函数可以让你得到笛卡尔积。
import itertools
x = ['parking_meter', 'sink', 'teddy_bear']
y = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on']
然后你可以做这个 2-liner,(因为乘积函数 returns 一个元组但你需要一个列表)
z = list(itertools.product(x,y))
z = [list(z) for z in z]
或者这个 1-liner,
z = [list(tup) for tup in list(itertools.product(x, y))]
那我,
print (z)
这给了我
[['parking_meter', 'sail'],
['parking_meter', 'fill'],
['parking_meter', 'fly'],
['parking_meter', 'greet'],
['parking_meter', 'hit'],
['parking_meter', 'hose'],
['parking_meter', 'hunt'],
['parking_meter', 'install'],
['parking_meter', 'kick'],
['parking_meter', 'launch'],
['parking_meter', 'move'],
['parking_meter', 'pick'],
['parking_meter', 'repair'],
['parking_meter', 'sit_at'],
['parking_meter', 'squeeze'],
['parking_meter', 'stab'],
['parking_meter', 'straddle'],
['parking_meter', 'talk_on'],
['sink', 'sail'],
['sink', 'fill'],
['sink', 'fly'],......
您的代码应该可以正常工作。我怀疑您的实际代码中还有一些其他不必要的 str() 函数调用:
objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose',
'hunt', 'install', 'kick', 'launch', 'move',
'pick', 'repair', 'sit_at', 'squeeze', 'stab',
'straddle', 'talk_on']
list3 = [[o,v] for o in objects_list for v in verb_list]
[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly'], ['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]
从您得到的输出来看,您的输入列表似乎是字符串,而不是列表。
就好像 objects_list
是 print(objects_list)
的输出,而不是列表本身。
因此,当遍历 objects_list
时,您不是遍历列表项,而是遍历其字符串表示的字符:
"["
"'"
"p"
"a"
...
你的代码在其他方面是正确的,除了你不需要在列表理解中将元素转换为字符串。
list3 = [[l, n] for l in objects_list for n in verb_list]
你的代码工作得很好并且给了我预期的输出。我不确定是否还有其他问题。可能是您的列表没有像我一样声明...如果您是从其他来源创建此列表...您应该检查格式。
注意:我在 Python 3.9.5
上有 运行 这段代码objects_list = ['parking_meter', 'sink', 'teddy_bear']
verb_list = ['sail', 'fill', 'fly', 'greet', 'hit', 'hose', 'hunt', 'install', 'kick', 'launch', 'move', 'pick', 'repair', 'sit_at', 'squeeze', 'stab', 'straddle', 'talk_on']
list3 = [[str(l),str(n)] for l in objects_list for n in verb_list]
print(list3)
Output:
[['parking_meter', 'sail'], ['parking_meter', 'fill'], ['parking_meter', 'fly']['parking_meter', 'greet'], ['parking_meter', 'hit'], ['parking_meter', 'hose'], ['parking_meter', 'hunt'], ['parking_meter', 'install'], ['parking_meter', 'kick'], ['parking_meter', 'launch'], ['parking_meter', 'move'], ['parking_meter', 'pick'], ['parking_meter', 'repair'], ['parking_meter', 'sit_at'], ['parking_meter', 'squeeze'], ['parking_meter', 'stab'], ['parking_meter', 'straddle'], ['parking_meter', 'talk_on'], ['sink', 'sail'], ['sink', 'fill'], ['sink', 'fly'], ['sink', 'greet'], ['sink', 'hit'], ['sink', 'hose'], ['sink', 'hunt'], ['sink', 'install'], ['sink', 'kick'], ['sink', 'launch'], ['sink', 'move'], ['sink', 'pick'], ['sink', 'repair'], ['sink', 'sit_at'], ['sink', 'squeeze'], ['sink', 'stab'], ['sink', 'straddle'], ['sink', 'talk_on'], ['teddy_bear', 'sail'], ['teddy_bear', 'fill'], ['teddy_bear', 'fly'], ['teddy_bear', 'greet'], ['teddy_bear', 'hit'], ['teddy_bear', 'hose'], ['teddy_bear', 'hunt'], ['teddy_bear', 'install'], ['teddy_bear', 'kick'], ['teddy_bear', 'launch'], ['teddy_bear', 'move'], ['teddy_bear', 'pick'], ['teddy_bear', 'repair'], ['teddy_bear', 'sit_at'], ['teddy_bear', 'squeeze'], ['teddy_bear', 'stab'], ['teddy_bear', 'straddle'], ['teddy_bear', 'talk_on']]