我正在尝试确定搜索字典键值的最佳方法。
我似乎无法找到一个允许我在列表列表中搜索特定字符串的函数。我正在尝试让程序显示某个类型的学期。
即。输入“ENGR”并返回它显示在“fall14”,“fall15”和“spring16”中。
有人能指出我从列表中的列表中提取数据的正确方向吗?我想我可能需要重新调整我定义键的方式。
Classes = dict()
Classes["FALL14"] = ("ENGR","156","01",'Engineering 3'),("ENGR","156","01",'Engineering 4'),("SCI","156","01",'Chemistry 1'),("ENGL","156","01",'Writing 1')
Classes['SPRING15'] =("ENGL","156","01",'Writing 2'),("MATH","156","01",'Calculus 1'),("MATH","156","01",'Calculus 2'),("MATH","156","01",'Calculus 3')
Classes['FALL15'] =("MATH","156","01",'Differential Equations'),("ENGR","156","01",'Solid Works'),("ENGR","156","01",'MATLAB'),("ENGR","156","01",'Dynamics')
Classes['SPRING16'] = ("ENGR","156","01",'Statics'),("ART","156","01",'Drawing 1'),("ENGR","156","01",'Fluid Dynamics'),("ENGR","156","01",'Thermodynamics 1')
for semester, information in Classes.items(): #Prints semesters that a class type was taken.
if information == "ENGR":
print(semester)
答案 0 :(得分:0)
假设您要比较的字符串始终位于嵌套元组的第0个索引处,您可以使用any()
匹配内容,如:
my_str = "ENGR"
# v as per PEP naming convention, it should
# v start with lower-case
for semester, informations in Classes.items():
if any(item[0]==my_str for item in informations):
# ^ check element at 0th index of each sub-tuple
# for the match with `my_str`
print(semester) # OR, print(semester.lower()) to print the lower cased
# string as mentioned in desired result
将打印:
FALL14
FALL15
SPRING16
答案 1 :(得分:0)
你需要再降低一级。你的循环给你一组集合。然后,您需要迭代这些集
set pri=20
set sec=20
set proddataset=IPP.PROD
set dsntype=cylinders
set recfm=fb
set lrecl=27998
set blksize=27998
set volume=PPINS2
echo quote site pri=%pri% sec=%sec% %dsntype% recfm=%recfm% lrecl=%lrecl% blksize=%blksize% volume=%volume%
答案 2 :(得分:0)
试一试。您需要继续深入研究数据结构。
Classes = dict()
Classes["FALL14"] = ("ENGR","156","01",'Engineering 3'),("ENGR","156","01",'Engineering 4'),("SCI","156","01",'Chemistry 1'),("ENGL","156","01",'Writing 1')
Classes['SPRING15'] =("ENGL","156","01",'Writing 2'),("MATH","156","01",'Calculus 1'),("MATH","156","01",'Calculus 2'),("MATH","156","01",'Calculus 3')
Classes['FALL15'] =("MATH","156","01",'Differential Equations'),("ENGR","156","01",'Solid Works'),("ENGR","156","01",'MATLAB'),("ENGR","156","01",'Dynamics')
Classes['SPRING16'] = ("ENGR","156","01",'Statics'),("ART","156","01",'Drawing 1'),("ENGR","156","01",'Fluid Dynamics'),("ENGR","156","01",'Thermodynamics 1')
for semester, information in Classes.items(): #Prints semesters that a class type was taken.
for course_info in Classes[semester]:
if course_info[0] == "ENGR":
print(semester)