如何通过搜索关联名称在列表中找到数值

  • 本文关键字:列表 搜索 何通过 关联 python
  • 更新时间 :
  • 英文 :


不打印"发现";,我想打印相关的数值

def lookupHostGroup(group):
hostgroups = [
['Maas GCP Pod M005', '134'],
['Maas GCP Pod M006', '136'],
['Maas GCP Pod M007', '135'],
['Maas GCP Pod M009', '156'],
['Maas GCP Pod M001', '168'],
['Maas GCP Pod M0011', '166']
]

for hg in hostgroups:
if hg[0] == group:
print ("found")
return hg[0]

print(lookupHostGroup("Maas GCP Pod M007"))

让Python为您完成工作。

hostgroups = {
'Maas GCP Pod M005': '134',
'Maas GCP Pod M006': '136',
'Maas GCP Pod M007': '135',
'Maas GCP Pod M009': '156',
'Maas GCP Pod M001': '168',
'Maas GCP Pod M0011': '166'
}
def lookupHostGroup(group):
return hostgroups[group]

print(lookupHostGroup("Maas GCP Pod M007"))
def lookupHostGroup(group):
hostgroups = [
['Maas GCP Pod M005', '134'],
['Maas GCP Pod M006', '136'],
['Maas GCP Pod M007', '135'],
['Maas GCP Pod M009', '156'],
['Maas GCP Pod M001', '168'],
['Maas GCP Pod M0011', '166']
]
for hg in hostgroups:
if hg[0] == group: 
return int(hg[1])

print(lookupHostGroup("Maas GCP Pod M007"))

这应该行得通

最新更新