python - 如何输入多个键,输出应返回多个值。请检查脚本和我的评论
问题描述
nestdic = { 'modelname' : {
'mod-num-1221' : 'LENEVO' ,
'mod-num-1222' : 'ASUS' ,
'mod-num-1223' : 'APPLE' ,
'mod-num-1224' : 'SAMSUNG' ,
'mod-num-1225' : 'HP'
}, 'ostype' : {
'LENEVO' : 'Windows' ,
'ASUS' : 'Windows' ,
'APPLE' : 'IOS' ,
'SAMSUNG' : 'LINUX' ,
'HP' : 'Windows'
} }
option = input("Please select from the below options\n1. Vendor Name type\n2. OS type\n\n\nPlease make a selection or press Q to quit: ")
if option == '1':
for i in range(5):
modelnum = input("Please enter the model num: ")
model = list(nestdic['modelname'])
if modelnum not in model:
print("Model name not found")
continue
modelname = nestdic['modelname'][modelnum]
if modelnum in model:
print("model name = " + modelname)
elif option == '2':
for i in range(5):
compname = input("Please enter the vendors name: ")
os = list(nestdic['ostype'])
if compname not in os:
print("os name not found")
continue
compsname = nestdic['ostype'][compname]
if compname in os:
print("OS type = " + compsname)
当我运行这个脚本时,它会询问我选项。如果我选择型号,它会要求我输入型号...当我输入型号(键)时,它会给我型号名称(值)...目前,我可以提供一个输入并获得一个仅单输出。我想输入多个值并获得多个输出。输入 = mod-num-1221、mod-num-1222、mod-num-1225 输出 = 联想、华硕、惠普
解决方案
有很多东西可以在这里学习。我对您的代码进行了其他一些修改,以纠正一些低效率的问题;看看评论。
import re
nestdic = { 'modelname' : {
'mod-num-1221' : 'LENEVO' ,
'mod-num-1222' : 'ASUS' ,
'mod-num-1223' : 'APPLE' ,
'mod-num-1224' : 'SAMSUNG' ,
'mod-num-1225' : 'HP'
}, 'ostype' : {
'LENEVO' : 'Windows' ,
'ASUS' : 'Windows' ,
'APPLE' : 'IOS' ,
'SAMSUNG' : 'LINUX' ,
'HP' : 'Windows'
} }
option = input("Please select from the below options\n1. Vendor Name type\n2. OS type\n\n\nPlease make a selection or press Q to quit: ")
if option == '1':
models = nestdic['modelname'] # no reason for this to be in the loop and don't convert to list
while True: # loop while there is an error
error = False
modelnums_str = input("Please enter the model numbers separated by commas: ").strip()
if modelnums_str == '':
continue
modelnums = [modelnum for modelnum in re.split(r',\s*', modelnums_str)]
modelnames = []
for modelnum in modelnums:
if modelnum not in models:
print(f'Model number {modelnum} not found.')
error = True
else:
modelnames.append(models[modelnum])
if error:
continue
if modelnames:
print(f"Input = {', '.join(modelnums)}, Output = {', '.join(modelnames)}")
break
elif option == '2':
os = nestdic['ostype'] # no reason for this to be in the loop and don't convert to a list
for i in range(5):
compname = input("Please enter the vendors name: ")
if compname not in os:
print("os name not found")
continue
compsname = os[compname]
print("OS type = " + compsname)
尽量使用 4 个空格进行缩进;8有点多。请参阅PEP 8 -- Python 代码样式指南
推荐阅读
- java - java安全策略不按预期工作,总是给AccessControlException
- javascript - 无需重新加载即可更改页面的好习惯
- javascript - 使用 fetch-mock 测试 React Redux 异步操作 - “无法读取未定义的属性 'then'”
- css - 固定位置 div 中的垂直对齐
- git - 在 GitHub 上分叉现有 pod 并在 Swift 工作区中使用 fork - 步骤?
- batch-file - 将 CMD 日志保存到文件
- reactjs - 如何使用表单中的数据从 api 获取数据?
- mongodb - 如何从 MongoDB 的集合中提取选定的字段
- java - 使用 JSP 将 HashMap 发送到控制器
- dbeaver - 在 DBeaver 中,如何从两个不同的连接运行 SQL 联合查询..?