python - Python:遍历字典中的项目列表并分配变量
问题描述
我正在尝试遍历字典并将变量分配给此python脚本中字典中的关键项,但是变量没有按预期分配,下面是我正在使用的python脚本,尝试了多种方法但无法破解这个,任何帮助将不胜感激:
interfaces = {
'switch1':['Gi0/0/0', 'Gi0/0/0.12', 'Gi0/0/0.14', 'Gi0/0/0.100', 'Gi0/0/0.101', 'Gi0/0/0.102', 'Gi0/0/0.105', 'Gi0/0/0.106', 'Gi0/0/1', 'Gi0/0/3', 'Gi0/0/5'],
'switch2':['Gi0/0/0', 'Gi0/0/0.34', 'Gi0/0/0.100', 'Gi0/0/0.101', 'Gi0/0/0.102', 'Gi0/0/0.103', 'Gi0/0/0.105', 'Gi0/0/1'],
'switch3':['Te0/1/0', 'Te0/1/0.3246', 'Te0/1/2', 'Te0/1/3', 'Te0/1/4'],
'switch4':['Te0/1/0.3246', 'Te0/1/3', 'Te0/1/4', 'Te0/1/0'],
'nexus1':['Eth1/1', 'Eth1/2', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/49', 'Eth1/50'],
'nexus2':['Eth1/1', 'Eth1/2', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/49', 'Eth1/50'],
'switch5':['Gi0/0/0', 'Gi0/0/1', 'Gi0/0/1.99', 'Gi0/0/1.200', 'Gi0/0/1.210', 'Gi0/0/2', 'Gi0'],
'switch6':['Gi0/0/0', 'Gi0/0/1', 'Gi0/0/1.103 ', 'Gi0/0/1.200', 'Gi0/0/1.210', 'Gi0/0/2 ', 'Gi0'],
'switch7':['Gi0/0/0', 'Gi0/0/0.100 ', 'Gi0/0/1 ', 'Gi0/0/3 ', 'Te0/1/0 ', 'Te0/1/0.100', 'Te0/1/1 ', 'Te0/1/1.100', 'Te0/1/1.101', 'Te0/1/1.110'],
'switch8':['Gi0/0/0', 'Gi0/0/0.100', 'Gi0/0/1', 'Gi0/0/2 ', 'Gi0/0/3', 'Te0/1/0 ', 'Te0/1/0.100', 'Te0/1/1', 'Te0/1/1.100 ', 'Te0/1/1.101', 'Te0/1/1.110'],
'nexus3':['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/27', 'Eth1/29', 'Eth1/41'],
'nexus4':['Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/25', 'Eth1/26', 'Eth1/27', 'Eth1/28', 'Eth1/29']
}
for key, value in interfaces.items():
if key == ('switch1' or 'switch2' or 'switch3' or 'switch4'or 'switch5' or 'switch6'):
username = 'username'
password = 'password'
platform = 'cisco_xe'
elif key == ('switch7' or 'switch8'):
username = 'username1'
password = 'password1'
platform = 'cisco_xe'
elif key == ('nexus1' or 'nexus2'):
username = 'username2'
password = 'password'
platform = 'cisco_nxos'
else:
username = 'username3'
password = 'password4'
platform = 'cisco_nxos'
print(key,username,password,platform)
电流输出:
switch1 username password cisco_xe
switch2 username3 password4 cisco_nxos
switch3 username3 password4 cisco_nxos
switch4 username3 password4 cisco_nxos
nexus1 username2 password cisco_nxos
nexus2 username3 password4 cisco_nxos
switch5 username3 password4 cisco_nxos
switch6 username3 password4 cisco_nxos
switch7 username1 password1 cisco_xe
switch8 username3 password4 cisco_nxos
nexus3 username3 password4 cisco_nxos
预期输出:
switch1 username password cisco_xe
switch2 username password cisco_xe
switch3 username password cisco_xe
switch4 username password cisco_xe
nexus1 username2 password cisco_nxos
nexus2 username3 password4 cisco_nxos
switch5 username password cisco_xe
switch6 username password cisco_xe
switch7 username1 password1 cisco_xe
switch8 username1 password1 cisco_xe
nexus3 username3 password4 cisco_nxos
nexus4 username3 password4 cisco_nxos
解决方案
尝试这个:
for key, value in interfaces.items():
if key in {'switch1', 'switch2', 'switch3', 'switch4', 'switch5', 'switch6'}:
username = 'username'
password = 'password'
platform = 'cisco_xe'
elif key in {'switch7', 'switch8'}:
username = 'username1'
password = 'password1'
platform = 'cisco_xe'
elif key in {'nexus1', 'nexus2'}:
username = 'username2'
password = 'password'
platform = 'cisco_nxos'
else:
username = 'username3'
password = 'password4'
platform = 'cisco_nxos'
print(key,username,password,platform)
推荐阅读
- php - PHP:简单路由类,问题如何添加多条路由
- javascript - 'JSON 输入意外结束'
- javascript - Google表格脚本日期比较未返回预期结果
- javascript - Youtube Data Api v3 - 未捕获的类型错误:无法读取未定义的属性“setApiKey”
- api - 如何从 instagram 图片 url 获取 media-id
- azure - Azure 服务计划 - Web 应用程序在时间更改后无法正常工作
- templates - 在我的 GitHub 中使用版权代码模板(CC BY NC SA 许可证)
- laravel - NPM:意外的标识符
- javascript - 在 jquery 中选择另一个文件中的元素
- python - Gradle 使用 Chaquopy 构建内存问题