微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在多个列表上嵌套 for 循环

如何解决在多个列表上嵌套 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。