python - 循环嵌套列表?
问题描述
所以我正在尝试构建一个验证脚本来访问主设备下的设备列表,并且每个设备都应该使用一组命令。
我想问一下我可以用来循环这个的最佳方法是什么,例如,如果主设备到互联网设备,然后检查互联网下的设备是否使用指定的命令。
List Main device:
1. Internet Device
a. 1.1.1.1
- List of command [show con, show 1.1.1.1]
b. 1.1.1.2
- list of command [show con, show 1.1.1.2]
2. Private Device
a. 1.1.1.3
- List of command [show con, show 1.1.1.3]
b. 1.1.1.4
- List of command [show con, show 1.1.1.4]
我应该为此创建一个txt文件还是像字典样式一样?喜欢主要设备、设备、命令的列表吗?
我应该使用嵌套列表以及嵌套循环来实现这一点吗?或创建多个功能?
我将命令列表分开以供其他用途。但是,例如,我确实有超过 5 个设备,我的代码是否适合此设置?
# sample code: #
internetdev = [1.1.1.1,1.1.1.2]
internetcommd = [[show con, show 1.1.1.1],[show con, show 1.1.1.2]]
privatedev = [1.1.1.1,1.1.1.2]
privcommd = [[show con, show 1.1.1.4]]
for intdev,intcmd in zip(internetdev,internetcommd[0:]):
print 'Connecting to ',intdev ,'\nSending command ',intcmd
child = begin_rmt(intdev,intcmd,uname,tpass)
for privdev,privcmd in zip(privatedev,privcommd[0:]):
print 'Connecting to ',privdev ,'\nSending command ',privcmd
child = begin_rmt(privdev,privcmd,uname,tpass)
有什么建议吗?如果您可以提供示例代码或参考,这样我也可以查看,这也更好。谢谢
解决方案
您可以使用嵌套字典。就像是,
devices = {
"public": [
{
"name": "1.1.1.1",
"command": ["show con", "show 1.1.1.1"]
},
{
"name": "1.1.1.2",
"command": ["show con", "show 1.1.1.2"]
}
],
"private": [
{
"name": "1.1.1.3",
"command": ["show con", "show 1.1.1.3"]
},
{
"name": "1.1.1.4",
"command": ["show con", "show 1.1.1.4"]
}
]
}
推荐阅读
- eclipse - GitHub 使用 Eclipse 推送和提交
- java - 创建媒体播放器时出错;Java、安卓工作室
- python - 如何让调试器单步执行整个程序?
- node.js - 在 mongoose 中保存和检索相关数据
- javascript - 将数据从firebase附加到表的问题
- ruby-on-rails - 如何使用带有参考的模型创建种子文件?我不确定我是否正确设置了模型
- bash - 将 If 条件写为函数
- woocommerce - 添加条件以显示 div
- haskell - Haskell 类型解释
- mysql - 如何保持wordpress、AWS lightsail的两个MySQL数据库同步?