在多个列表上嵌套 for 循环

问题描述

我正在构建一个脚本,该脚本从用户输入中获取多个 IP。这个的输出是 2 个列表。

我现在需要针对列表 B 中的 IP 运行列表 A 中 IP 的所有可能组合。 因此,如果用户输入:列表 A:10.1.1.1 和列表 B:192.168.1.1,192.168.1.2

所以我希望这个输出: 10.1.1.1 192.168.1.1 10.1.1.1 192.168.1.2

我尝试了一些方法,嵌套 for 循环、itertools 等。我似乎无法获得我想要的输出

请看下面我的代码

import ipaddress
#from itertools import combinations
local_traffic_list = []
# nget the number of subnets as input
l_subnets = int(input("Enter number of Local Encryption Domain subnets: "))
# get the user to enter the subnets,line by line and append to list
print("NOTE: Enter subnet with mask,then press enter and repeat!")
for i in range(0,l_subnets):
    local_traffic_subnets = input("Whats the local encryption domain? ")
    local_traffic_list.append(local_traffic_subnets)

# ensure this is a valid IP and convert to wildcard and make list
for item in local_traffic_list:
    host_wc_acl = ipaddress.ip_network(item).with_hostmask.replace("/"," ").splitlines()
    print(host_wc_acl)

remote_traffic_list = []
r_subnets = int(input("Enter number of Remote Encryption Domain subnets: "))
print("NOTE: Enter subnet with mask,then press enter and repeat!")
for x in range(0,r_subnets):
    remote_traffic_subnets = input("Whats the remote encryption domain? ")
    remote_traffic_list.append(remote_traffic_subnets)

# ensure this is a valid IP and convert to wildcard  and make list
for item in remote_traffic_list:
    dest_wc_acl = ipaddress.ip_network(item).with_hostmask.replace("/"," ").splitlines()
    print(dest_wc_acl)

#joing source and dest's together for every possible solution

for host in host_wc_acl:
    for dest in dest_wc_acl:
        print(host+ " " + dest)

这是我运行它时发生的情况:

Enter number of Local Encryption Domain subnets: 1
NOTE: Enter subnet with mask,then press enter and repeat!
Whats the local encryption domain? 10.1.1.1
['10.1.1.1 0.0.0.0']
Enter number of Remote Encryption Domain subnets: 2
NOTE: Enter subnet with mask,then press enter and repeat!
Whats the remote encryption domain? 192.168.1.1
Whats the remote encryption domain? 192.168.1.2
['192.168.1.1 0.0.0.0']
['192.168.1.2 0.0.0.0']
10.1.1.1 0.0.0.0 192.168.1.2 0.0.0.0

我的问题似乎是嵌套的 for 循环......因为当我打印这两个列表时,我得到了预期的输出。 所以我的输出显示最后一个条目而不是所有条目 - 有没有更好的方法来实现这一点?

解决方法

您可以在 from sklearn.utils.extmath import cartesian 中尝试 cartesian() 模块,这将数组数组作为输入并执行这些数组的叉积。

试试这个:

from sklearn.utils.extmath import cartesian
cartesian((local_traffic_list,remote_traffic_list))
,

我稍微更改了您的代码:

row_number() between ... and ...

输出:

import ipaddress
import itertools

local_traffic_list = []
remote_traffic_list = []

# get the number of subnets as input
l_subnets = int(input("Enter number of Local Encryption Domain Subnets: "))

# get the user to enter the subnets,line by line and append to list
print("NOTE: Enter subnet with mask,then press enter and repeat!")
for i in range(0,l_subnets):
    local_traffic_list.append(input("Whats the local encryption domain? "))

# ensure this is a valid IP and convert to wildcard and make list
host_wc_acl = [ipaddress.ip_network(item).with_hostmask.replace("/"," ").splitlines() for item in local_traffic_list]

# get the number of subnets as input
r_subnets = int(input("Enter number of Remote Encryption Domain Subnets: "))


print("NOTE: Enter subnet with mask,then press enter and repeat!")
for x in range(0,r_subnets):
    remote_traffic_list.append(input("Whats the remote encryption domain? "))

# ensure this is a valid IP and convert to wildcard  and make list
dest_wc_acl=[ipaddress.ip_network(item).with_hostmask.replace("/"," ").splitlines() for item in remote_traffic_list]

#joing source and dest's together for every possible solution
combinations = [" ".join(["ip permit"] + list(itertools.chain.from_iterable(a))) for a in itertools.product(host_wc_acl,dest_wc_acl)]

print(combinations)