简体   繁体   English

在多个列表上嵌套 for 循环

[英]Nested for Loop on multiple lists

I'm building a script that takes a number of IP's from a user input.我正在构建一个脚本,该脚本从用户输入中获取多个 IP。 the output of this is 2 lists. output 是 2 个列表。

I now need to run every possible combination of the IP's in list A against the IP's in list B. So if a user entered: list A: 10.1.1.1 and List B: 192.168.1.1, 192.168.1.2我现在需要运行列表 A 中 IP 与列表 B 中 IP 的所有可能组合。因此,如果用户输入:列表 A:10.1.1.1 和列表 B:192.168.1.1、192.168.1.2

So i'd expect this output: 10.1.1.1 192.168.1.1 10.1.1.1 192.168.1.2所以我希望这个 output: 10.1.1.1 192.168.1.1 10.1.1.1 192.168.1.2

I've tried a few things, nested for loops, itertools etc. I can't seem to get the output I'm after.我已经尝试了一些事情,嵌套 for 循环、itertools 等。我似乎无法获得我想要的 output。

Please see my code below:请在下面查看我的代码:

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)

Here's what happens when i run it:这是我运行它时发生的情况:

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

My issue seems to be the nested for loop.. as when i print the two lists i get the expected output.我的问题似乎是嵌套的 for 循环。当我打印两个列表时,我得到了预期的 output。 So my output only presents the last entry rather than all of them - is there a better way to achieve this?所以我的 output 只显示最后一个条目而不是所有条目 - 有没有更好的方法来实现这一点?

You can try cartesian() module in from sklearn.utils.extmath import cartesian this takes an array of array as input and performs cross-product of those arrays.您可以尝试从 sklearn.utils.extmath import cartesian 中的 cartesian() 模块,这将数组数组作为输入并执行这些 arrays 的叉积。

try this:尝试这个:

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

I changed your code slightly:我稍微更改了您的代码:

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)

output: output:

['ip permit 10.1.1.1 0.0.0.0 192.168.1.1 0.0.0.0', 'ip permit 10.1.1.1 0.0.0.0 192.168.1.2 0.0.0.0']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM