【问题标题】:How to get a combination of values from multiple list of items with varying length in Python 3.x?如何从 Python 3.x 中长度不同的多个项目列表中获取值的组合?
【发布时间】:2020-11-27 19:47:19
【问题描述】:

以下是我的清单 -

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 也不起作用。提前谢谢你。

【问题讨论】:

  • 假设paths 是一维数组是否安全?
  • 不。路径可以为单个 ip 携带多个值。假设 10.0.0.0 可以有“abc”和“xyz”,10.0.0.1 可以只有一个“def”,而 10.0.0.2 可以有两个以上的值。

标签: python python-3.x list nested-lists


【解决方案1】:

你在找这个吗?

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']]

【讨论】:

  • 非常感谢。它与上述解决方案一样有用,尽管效率略高。
【解决方案2】:

不确定直接的方式。使用循环:

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)

【讨论】:

  • 谢谢!这非常有用。
【解决方案3】:

您可以将递归与生成器一起使用:

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')]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多