如何从 Python 3.x 中不同长度的多个项目列表中获取值的组合?
How to get a combination of values from multiple list of items with varying length in Python 3.x?
下面是我的列表 -
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
Required output ->
[('a', '10.0.0.0', "abc", "test1"), ('a', '10.0.0.0', "xyz", "test1"), ('a', '10.0.0.1', "def", "test2"), ('a', '10.0.0.2', "klm", "test1")] and so on for b, c and d values in hosts.
注意:ips & names 是等长的普通列表。路径可以是嵌套列表,但总长度与 ips 和名称相同。但是,ips、paths 和 names 的长度不需要与 hosts 匹配(这不是嵌套列表)。
除了重复编写 for 循环之外,有人可以建议我解决这个问题吗,而且由于列表长度不同,Zip 也不起作用。提前谢谢你。
你在找这个吗?
from itertools import product
def flatten(ips, paths, names):
result = []
for i, p, n in zip(ips, paths, names):
if isinstance(p, list):
result.extend(product([i], p, [n]))
else:
result.append((i, p, n))
return result
all_outputs = []
for entry in product(hosts, flatten(ips, paths, names)):
output = [entry[0]]
output.extend(*entry[1:])
all_outputs.append(output)
输出
[['a', '10.0.0.0', 'abc', 'test1'],
['a', '10.0.0.0', 'xyz', 'test1'],
['a', '10.0.0.1', 'def', 'test2'],
['a', '10.0.0.2', 'klm', 'test1'],
['b', '10.0.0.0', 'abc', 'test1'],
['b', '10.0.0.0', 'xyz', 'test1'],
['b', '10.0.0.1', 'def', 'test2'],
['b', '10.0.0.2', 'klm', 'test1'],
['c', '10.0.0.0', 'abc', 'test1'],
['c', '10.0.0.0', 'xyz', 'test1'],
['c', '10.0.0.1', 'def', 'test2'],
['c', '10.0.0.2', 'klm', 'test1'],
['d', '10.0.0.0', 'abc', 'test1'],
['d', '10.0.0.0', 'xyz', 'test1'],
['d', '10.0.0.1', 'def', 'test2'],
['d', '10.0.0.2', 'klm', 'test1']]
不确定 straight-forward 方式。使用循环:
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
final_list = []
for elem in hosts:
for i in range(len(ips)):
if isinstance(paths[i], list):
for j in range(len(paths[i])):
final_list.append((elem, ips[i], paths[i][j], names[i]))
elif isinstance(paths[i], str):
final_list.append((elem, ips[i], paths[i], names[i]))
print(final_list)
您可以对生成器使用递归:
def combos(d, c = []):
if not d:
yield tuple(c)
else:
for i in [j for k in d[0] for j in (k if isinstance(k, list) else [k])]:
yield from combos(d[1:], c+[i])
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
print(list(combos([hosts, ips, paths, names])))
输出:
[('a', '10.0.0.0', 'abc', 'test1'), ('a', '10.0.0.0', 'abc', 'test2'), ('a', '10.0.0.0', 'abc', 'test1'), ('a', '10.0.0.0', 'xyz', 'test1'), ('a', '10.0.0.0', 'xyz', 'test2'), ('a', '10.0.0.0', 'xyz', 'test1'), ('a', '10.0.0.0', 'def', 'test1'), ('a', '10.0.0.0', 'def', 'test2'), ('a', '10.0.0.0', 'def', 'test1'), ('a', '10.0.0.0', 'klm', 'test1'), ('a', '10.0.0.0', 'klm', 'test2'), ('a', '10.0.0.0', 'klm', 'test1'), ('a', '10.0.0.1', 'abc', 'test1'), ('a', '10.0.0.1', 'abc', 'test2'), ('a', '10.0.0.1', 'abc', 'test1'), ('a', '10.0.0.1', 'xyz', 'test1'), ('a', '10.0.0.1', 'xyz', 'test2'), ('a', '10.0.0.1', 'xyz', 'test1'), ('a', '10.0.0.1', 'def', 'test1'), ('a', '10.0.0.1', 'def', 'test2'), ('a', '10.0.0.1', 'def', 'test1'), ('a', '10.0.0.1', 'klm', 'test1'), ('a', '10.0.0.1', 'klm', 'test2'), ('a', '10.0.0.1', 'klm', 'test1'), ('a', '10.0.0.2', 'abc', 'test1'), ('a', '10.0.0.2', 'abc', 'test2'), ('a', '10.0.0.2', 'abc', 'test1'), ('a', '10.0.0.2', 'xyz', 'test1'), ('a', '10.0.0.2', 'xyz', 'test2'), ('a', '10.0.0.2', 'xyz', 'test1'), ('a', '10.0.0.2', 'def', 'test1'), ('a', '10.0.0.2', 'def', 'test2'), ('a', '10.0.0.2', 'def', 'test1'), ('a', '10.0.0.2', 'klm', 'test1'), ('a', '10.0.0.2', 'klm', 'test2'), ('a', '10.0.0.2', 'klm', 'test1'), ('b', '10.0.0.0', 'abc', 'test1'), ('b', '10.0.0.0', 'abc', 'test2'), ('b', '10.0.0.0', 'abc', 'test1'), ('b', '10.0.0.0', 'xyz', 'test1'), ('b', '10.0.0.0', 'xyz', 'test2'), ('b', '10.0.0.0', 'xyz', 'test1'), ('b', '10.0.0.0', 'def', 'test1'), ('b', '10.0.0.0', 'def', 'test2'), ('b', '10.0.0.0', 'def', 'test1'), ('b', '10.0.0.0', 'klm', 'test1'), ('b', '10.0.0.0', 'klm', 'test2'), ('b', '10.0.0.0', 'klm', 'test1'), ('b', '10.0.0.1', 'abc', 'test1'), ('b', '10.0.0.1', 'abc', 'test2'), ('b', '10.0.0.1', 'abc', 'test1'), ('b', '10.0.0.1', 'xyz', 'test1'), ('b', '10.0.0.1', 'xyz', 'test2'), ('b', '10.0.0.1', 'xyz', 'test1'), ('b', '10.0.0.1', 'def', 'test1'), ('b', '10.0.0.1', 'def', 'test2'), ('b', '10.0.0.1', 'def', 'test1'), ('b', '10.0.0.1', 'klm', 'test1'), ('b', '10.0.0.1', 'klm', 'test2'), ('b', '10.0.0.1', 'klm', 'test1'), ('b', '10.0.0.2', 'abc', 'test1'), ('b', '10.0.0.2', 'abc', 'test2'), ('b', '10.0.0.2', 'abc', 'test1'), ('b', '10.0.0.2', 'xyz', 'test1'), ('b', '10.0.0.2', 'xyz', 'test2'), ('b', '10.0.0.2', 'xyz', 'test1'), ('b', '10.0.0.2', 'def', 'test1'), ('b', '10.0.0.2', 'def', 'test2'), ('b', '10.0.0.2', 'def', 'test1'), ('b', '10.0.0.2', 'klm', 'test1'), ('b', '10.0.0.2', 'klm', 'test2'), ('b', '10.0.0.2', 'klm', 'test1'), ('c', '10.0.0.0', 'abc', 'test1'), ('c', '10.0.0.0', 'abc', 'test2'), ('c', '10.0.0.0', 'abc', 'test1'), ('c', '10.0.0.0', 'xyz', 'test1'), ('c', '10.0.0.0', 'xyz', 'test2'), ('c', '10.0.0.0', 'xyz', 'test1'), ('c', '10.0.0.0', 'def', 'test1'), ('c', '10.0.0.0', 'def', 'test2'), ('c', '10.0.0.0', 'def', 'test1'), ('c', '10.0.0.0', 'klm', 'test1'), ('c', '10.0.0.0', 'klm', 'test2'), ('c', '10.0.0.0', 'klm', 'test1'), ('c', '10.0.0.1', 'abc', 'test1'), ('c', '10.0.0.1', 'abc', 'test2'), ('c', '10.0.0.1', 'abc', 'test1'), ('c', '10.0.0.1', 'xyz', 'test1'), ('c', '10.0.0.1', 'xyz', 'test2'), ('c', '10.0.0.1', 'xyz', 'test1'), ('c', '10.0.0.1', 'def', 'test1'), ('c', '10.0.0.1', 'def', 'test2'), ('c', '10.0.0.1', 'def', 'test1'), ('c', '10.0.0.1', 'klm', 'test1'), ('c', '10.0.0.1', 'klm', 'test2'), ('c', '10.0.0.1', 'klm', 'test1'), ('c', '10.0.0.2', 'abc', 'test1'), ('c', '10.0.0.2', 'abc', 'test2'), ('c', '10.0.0.2', 'abc', 'test1'), ('c', '10.0.0.2', 'xyz', 'test1'), ('c', '10.0.0.2', 'xyz', 'test2'), ('c', '10.0.0.2', 'xyz', 'test1'), ('c', '10.0.0.2', 'def', 'test1'), ('c', '10.0.0.2', 'def', 'test2'), ('c', '10.0.0.2', 'def', 'test1'), ('c', '10.0.0.2', 'klm', 'test1'), ('c', '10.0.0.2', 'klm', 'test2'), ('c', '10.0.0.2', 'klm', 'test1'), ('d', '10.0.0.0', 'abc', 'test1'), ('d', '10.0.0.0', 'abc', 'test2'), ('d', '10.0.0.0', 'abc', 'test1'), ('d', '10.0.0.0', 'xyz', 'test1'), ('d', '10.0.0.0', 'xyz', 'test2'), ('d', '10.0.0.0', 'xyz', 'test1'), ('d', '10.0.0.0', 'def', 'test1'), ('d', '10.0.0.0', 'def', 'test2'), ('d', '10.0.0.0', 'def', 'test1'), ('d', '10.0.0.0', 'klm', 'test1'), ('d', '10.0.0.0', 'klm', 'test2'), ('d', '10.0.0.0', 'klm', 'test1'), ('d', '10.0.0.1', 'abc', 'test1'), ('d', '10.0.0.1', 'abc', 'test2'), ('d', '10.0.0.1', 'abc', 'test1'), ('d', '10.0.0.1', 'xyz', 'test1'), ('d', '10.0.0.1', 'xyz', 'test2'), ('d', '10.0.0.1', 'xyz', 'test1'), ('d', '10.0.0.1', 'def', 'test1'), ('d', '10.0.0.1', 'def', 'test2'), ('d', '10.0.0.1', 'def', 'test1'), ('d', '10.0.0.1', 'klm', 'test1'), ('d', '10.0.0.1', 'klm', 'test2'), ('d', '10.0.0.1', 'klm', 'test1'), ('d', '10.0.0.2', 'abc', 'test1'), ('d', '10.0.0.2', 'abc', 'test2'), ('d', '10.0.0.2', 'abc', 'test1'), ('d', '10.0.0.2', 'xyz', 'test1'), ('d', '10.0.0.2', 'xyz', 'test2'), ('d', '10.0.0.2', 'xyz', 'test1'), ('d', '10.0.0.2', 'def', 'test1'), ('d', '10.0.0.2', 'def', 'test2'), ('d', '10.0.0.2', 'def', 'test1'), ('d', '10.0.0.2', 'klm', 'test1'), ('d', '10.0.0.2', 'klm', 'test2'), ('d', '10.0.0.2', 'klm', 'test1')]
下面是我的列表 -
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
Required output ->
[('a', '10.0.0.0', "abc", "test1"), ('a', '10.0.0.0', "xyz", "test1"), ('a', '10.0.0.1', "def", "test2"), ('a', '10.0.0.2', "klm", "test1")] and so on for b, c and d values in hosts.
注意:ips & names 是等长的普通列表。路径可以是嵌套列表,但总长度与 ips 和名称相同。但是,ips、paths 和 names 的长度不需要与 hosts 匹配(这不是嵌套列表)。
除了重复编写 for 循环之外,有人可以建议我解决这个问题吗,而且由于列表长度不同,Zip 也不起作用。提前谢谢你。
你在找这个吗?
from itertools import product
def flatten(ips, paths, names):
result = []
for i, p, n in zip(ips, paths, names):
if isinstance(p, list):
result.extend(product([i], p, [n]))
else:
result.append((i, p, n))
return result
all_outputs = []
for entry in product(hosts, flatten(ips, paths, names)):
output = [entry[0]]
output.extend(*entry[1:])
all_outputs.append(output)
输出
[['a', '10.0.0.0', 'abc', 'test1'],
['a', '10.0.0.0', 'xyz', 'test1'],
['a', '10.0.0.1', 'def', 'test2'],
['a', '10.0.0.2', 'klm', 'test1'],
['b', '10.0.0.0', 'abc', 'test1'],
['b', '10.0.0.0', 'xyz', 'test1'],
['b', '10.0.0.1', 'def', 'test2'],
['b', '10.0.0.2', 'klm', 'test1'],
['c', '10.0.0.0', 'abc', 'test1'],
['c', '10.0.0.0', 'xyz', 'test1'],
['c', '10.0.0.1', 'def', 'test2'],
['c', '10.0.0.2', 'klm', 'test1'],
['d', '10.0.0.0', 'abc', 'test1'],
['d', '10.0.0.0', 'xyz', 'test1'],
['d', '10.0.0.1', 'def', 'test2'],
['d', '10.0.0.2', 'klm', 'test1']]
不确定 straight-forward 方式。使用循环:
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
final_list = []
for elem in hosts:
for i in range(len(ips)):
if isinstance(paths[i], list):
for j in range(len(paths[i])):
final_list.append((elem, ips[i], paths[i][j], names[i]))
elif isinstance(paths[i], str):
final_list.append((elem, ips[i], paths[i], names[i]))
print(final_list)
您可以对生成器使用递归:
def combos(d, c = []):
if not d:
yield tuple(c)
else:
for i in [j for k in d[0] for j in (k if isinstance(k, list) else [k])]:
yield from combos(d[1:], c+[i])
hosts = ['a', 'b', 'c', 'd']
ips = ["10.0.0.0", "10.0.0.1", "10.0.0.2"]
paths = [["abc", "xyz"], "def", "klm"]
names = ["test1", "test2", "test1"]
print(list(combos([hosts, ips, paths, names])))
输出:
[('a', '10.0.0.0', 'abc', 'test1'), ('a', '10.0.0.0', 'abc', 'test2'), ('a', '10.0.0.0', 'abc', 'test1'), ('a', '10.0.0.0', 'xyz', 'test1'), ('a', '10.0.0.0', 'xyz', 'test2'), ('a', '10.0.0.0', 'xyz', 'test1'), ('a', '10.0.0.0', 'def', 'test1'), ('a', '10.0.0.0', 'def', 'test2'), ('a', '10.0.0.0', 'def', 'test1'), ('a', '10.0.0.0', 'klm', 'test1'), ('a', '10.0.0.0', 'klm', 'test2'), ('a', '10.0.0.0', 'klm', 'test1'), ('a', '10.0.0.1', 'abc', 'test1'), ('a', '10.0.0.1', 'abc', 'test2'), ('a', '10.0.0.1', 'abc', 'test1'), ('a', '10.0.0.1', 'xyz', 'test1'), ('a', '10.0.0.1', 'xyz', 'test2'), ('a', '10.0.0.1', 'xyz', 'test1'), ('a', '10.0.0.1', 'def', 'test1'), ('a', '10.0.0.1', 'def', 'test2'), ('a', '10.0.0.1', 'def', 'test1'), ('a', '10.0.0.1', 'klm', 'test1'), ('a', '10.0.0.1', 'klm', 'test2'), ('a', '10.0.0.1', 'klm', 'test1'), ('a', '10.0.0.2', 'abc', 'test1'), ('a', '10.0.0.2', 'abc', 'test2'), ('a', '10.0.0.2', 'abc', 'test1'), ('a', '10.0.0.2', 'xyz', 'test1'), ('a', '10.0.0.2', 'xyz', 'test2'), ('a', '10.0.0.2', 'xyz', 'test1'), ('a', '10.0.0.2', 'def', 'test1'), ('a', '10.0.0.2', 'def', 'test2'), ('a', '10.0.0.2', 'def', 'test1'), ('a', '10.0.0.2', 'klm', 'test1'), ('a', '10.0.0.2', 'klm', 'test2'), ('a', '10.0.0.2', 'klm', 'test1'), ('b', '10.0.0.0', 'abc', 'test1'), ('b', '10.0.0.0', 'abc', 'test2'), ('b', '10.0.0.0', 'abc', 'test1'), ('b', '10.0.0.0', 'xyz', 'test1'), ('b', '10.0.0.0', 'xyz', 'test2'), ('b', '10.0.0.0', 'xyz', 'test1'), ('b', '10.0.0.0', 'def', 'test1'), ('b', '10.0.0.0', 'def', 'test2'), ('b', '10.0.0.0', 'def', 'test1'), ('b', '10.0.0.0', 'klm', 'test1'), ('b', '10.0.0.0', 'klm', 'test2'), ('b', '10.0.0.0', 'klm', 'test1'), ('b', '10.0.0.1', 'abc', 'test1'), ('b', '10.0.0.1', 'abc', 'test2'), ('b', '10.0.0.1', 'abc', 'test1'), ('b', '10.0.0.1', 'xyz', 'test1'), ('b', '10.0.0.1', 'xyz', 'test2'), ('b', '10.0.0.1', 'xyz', 'test1'), ('b', '10.0.0.1', 'def', 'test1'), ('b', '10.0.0.1', 'def', 'test2'), ('b', '10.0.0.1', 'def', 'test1'), ('b', '10.0.0.1', 'klm', 'test1'), ('b', '10.0.0.1', 'klm', 'test2'), ('b', '10.0.0.1', 'klm', 'test1'), ('b', '10.0.0.2', 'abc', 'test1'), ('b', '10.0.0.2', 'abc', 'test2'), ('b', '10.0.0.2', 'abc', 'test1'), ('b', '10.0.0.2', 'xyz', 'test1'), ('b', '10.0.0.2', 'xyz', 'test2'), ('b', '10.0.0.2', 'xyz', 'test1'), ('b', '10.0.0.2', 'def', 'test1'), ('b', '10.0.0.2', 'def', 'test2'), ('b', '10.0.0.2', 'def', 'test1'), ('b', '10.0.0.2', 'klm', 'test1'), ('b', '10.0.0.2', 'klm', 'test2'), ('b', '10.0.0.2', 'klm', 'test1'), ('c', '10.0.0.0', 'abc', 'test1'), ('c', '10.0.0.0', 'abc', 'test2'), ('c', '10.0.0.0', 'abc', 'test1'), ('c', '10.0.0.0', 'xyz', 'test1'), ('c', '10.0.0.0', 'xyz', 'test2'), ('c', '10.0.0.0', 'xyz', 'test1'), ('c', '10.0.0.0', 'def', 'test1'), ('c', '10.0.0.0', 'def', 'test2'), ('c', '10.0.0.0', 'def', 'test1'), ('c', '10.0.0.0', 'klm', 'test1'), ('c', '10.0.0.0', 'klm', 'test2'), ('c', '10.0.0.0', 'klm', 'test1'), ('c', '10.0.0.1', 'abc', 'test1'), ('c', '10.0.0.1', 'abc', 'test2'), ('c', '10.0.0.1', 'abc', 'test1'), ('c', '10.0.0.1', 'xyz', 'test1'), ('c', '10.0.0.1', 'xyz', 'test2'), ('c', '10.0.0.1', 'xyz', 'test1'), ('c', '10.0.0.1', 'def', 'test1'), ('c', '10.0.0.1', 'def', 'test2'), ('c', '10.0.0.1', 'def', 'test1'), ('c', '10.0.0.1', 'klm', 'test1'), ('c', '10.0.0.1', 'klm', 'test2'), ('c', '10.0.0.1', 'klm', 'test1'), ('c', '10.0.0.2', 'abc', 'test1'), ('c', '10.0.0.2', 'abc', 'test2'), ('c', '10.0.0.2', 'abc', 'test1'), ('c', '10.0.0.2', 'xyz', 'test1'), ('c', '10.0.0.2', 'xyz', 'test2'), ('c', '10.0.0.2', 'xyz', 'test1'), ('c', '10.0.0.2', 'def', 'test1'), ('c', '10.0.0.2', 'def', 'test2'), ('c', '10.0.0.2', 'def', 'test1'), ('c', '10.0.0.2', 'klm', 'test1'), ('c', '10.0.0.2', 'klm', 'test2'), ('c', '10.0.0.2', 'klm', 'test1'), ('d', '10.0.0.0', 'abc', 'test1'), ('d', '10.0.0.0', 'abc', 'test2'), ('d', '10.0.0.0', 'abc', 'test1'), ('d', '10.0.0.0', 'xyz', 'test1'), ('d', '10.0.0.0', 'xyz', 'test2'), ('d', '10.0.0.0', 'xyz', 'test1'), ('d', '10.0.0.0', 'def', 'test1'), ('d', '10.0.0.0', 'def', 'test2'), ('d', '10.0.0.0', 'def', 'test1'), ('d', '10.0.0.0', 'klm', 'test1'), ('d', '10.0.0.0', 'klm', 'test2'), ('d', '10.0.0.0', 'klm', 'test1'), ('d', '10.0.0.1', 'abc', 'test1'), ('d', '10.0.0.1', 'abc', 'test2'), ('d', '10.0.0.1', 'abc', 'test1'), ('d', '10.0.0.1', 'xyz', 'test1'), ('d', '10.0.0.1', 'xyz', 'test2'), ('d', '10.0.0.1', 'xyz', 'test1'), ('d', '10.0.0.1', 'def', 'test1'), ('d', '10.0.0.1', 'def', 'test2'), ('d', '10.0.0.1', 'def', 'test1'), ('d', '10.0.0.1', 'klm', 'test1'), ('d', '10.0.0.1', 'klm', 'test2'), ('d', '10.0.0.1', 'klm', 'test1'), ('d', '10.0.0.2', 'abc', 'test1'), ('d', '10.0.0.2', 'abc', 'test2'), ('d', '10.0.0.2', 'abc', 'test1'), ('d', '10.0.0.2', 'xyz', 'test1'), ('d', '10.0.0.2', 'xyz', 'test2'), ('d', '10.0.0.2', 'xyz', 'test1'), ('d', '10.0.0.2', 'def', 'test1'), ('d', '10.0.0.2', 'def', 'test2'), ('d', '10.0.0.2', 'def', 'test1'), ('d', '10.0.0.2', 'klm', 'test1'), ('d', '10.0.0.2', 'klm', 'test2'), ('d', '10.0.0.2', 'klm', 'test1')]