索引和遍历字典时出现问题
Problem when indexing and iterating through dictionary
我正在为 python class 构建 Vin 解码器,进展顺利,但是我在遍历字典时遇到问题。以下是我目前为该项目编写的所有代码:
class vinfo():
def __init__(self, vin = input("Enter Vin (Without Dashes):")):
""" Data """
model_codes = {
}
year_codes = {
"2021":"M","2020":"L","2019":"K",
"2018":"J","2017":"H","2016":"G",
"2015":"F","2014":"E","2013":"D",
"2012":"C","2011":"B","2010":"A",
"2009":"9","2008":"8","2007":"7",
"2006":"6","2005":"5","2004":"4",
"2003":"3","2002":"2","2001":"1",
"2000":"Y","1999":"X","1998":"W",
"1997":"V","1996":"T","1995":"S",
"1994":"R","1993":"P","1992":"N",
"1991":"M","1990":"L","1989":"K",
"1988":"J","1987":"H","1986":"G",
"1985":"F","1984":"E","1983":"D",
"1982":"C","1981":"B","1980":"A"
}
country_codes = {
"USA":"1", "USA":"4", "USA":"5",
"USA":"7F","Mexico":"3X", "Mexico":"37",
"Canada":"3A", "Canada":"3W",
"Germany":"W", "United Kingdom":"SA",
"United Kingdom":"SM", "Japan":"J",
"Korea":"KL", "Korea":"KR"
}
engine_codes = {
}
if "-" in vin:
vin = vin.replace("-", "")
else:
print("Thanks for entering the Vin without dashes as prompted.")
if len(vin) == 17:
self.country_filt = vin[0]
self.engine_filt = vin[7]
self.year_filt = vin[9]
self.manufact_filt = vin[1:3]
self.serial = vin[-6:]
self.plant_filt = vin[10]
else:
print("You're dumb. Enter a real vin (must be 17 characters).")
self.year = self.find(self.year_filt, year_codes)
self.country = self.find(self.country_filt, country_codes)
self.manufact = self.find(self.manufact_filt, model_codes)
self.engine = self.find(self.engine_filt, engine_codes)
print(f"You entered: {vin}")
print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")
def find(self, filt, dict_of_type_of_codes):
try:
key = (list(dict_of_type_of_codes.keys())[list(dict_of_type_of_codes.values()).index(filt)])
except:
key = "Not in dict"
return key
#%% Testing
# test vin: 48123658489411439
vinfo("48123658489411439")
理想情况下,它会 return 包含汽车制造年份和制造国家/地区的打印声明。年份有效,但找不到国家/地区。 4 是本示例中的国家/地区代码,显然是美国。找不到原因。忽略空字典,一旦我知道我的过程有效,我会填写它们。
提前致谢。
首先,您在创建字典时有重复键。字典是哈希映射,用于搜索唯一键的对应值。
{"USA": "1", "USA": "4", "USA": "5"}
只会让你只剩下一个{"USA": "5"}
其次,你在查字典上做的太多了,违背了使用它的目的。例如。如果您正在根据代码搜索国家/地区,则您的字典应该是相反的,您可以从那里简单地检索每个代码的相应值(国家/地区):
class vinfo:
def __init__(self, vin = input("Enter Vin (Without Dashes):")):
""" Data """
model_codes = {
}
year_codes = {
"M":"2021","L":"2020","K":"2019",
"J":"2018","H":"2017","G":"2016",
"F":"2015","E":"2014","D":"2013",
"C":"2012","B":"2011","A":"2010",
"9":"2009","8":"2008","7":"2007",
"6":"2006","5":"2005","4":"2004",
"3":"2003","2":"2002","1":"2001",
"Y":"2000","X":"1999","W":"1998",
"V":"1997","T":"1996","S":"1995",
"R":"1994","P":"1993","N":"1992",
"M":"1991","L":"1990","K":"1989",
"J":"1988","H":"1987","G":"1986",
"F":"1985","E":"1984","D":"1983",
"C":"1982","B":"1981","A":"1980"
}
country_codes = {
"1": "USA",
"4": "USA",
"5": "USA",
"7F": "USA",
"3X": "Mexico",
"37": "Mexico",
"3A": "Canada",
"3W": "Canada",
"W": "Germany",
"SA": "United Kingdom",
"SM": "United Kingdom",
"J": "Japan",
"KL": "Korea",
"KR": "Korea"
}
engine_codes = {
}
if "-" in vin:
vin = vin.replace("-", "")
else:
print("Thanks for entering the Vin without dashes as prompted.")
if len(vin) == 17:
self.country_filt = vin[0]
self.engine_filt = vin[7]
self.year_filt = vin[9]
self.manufact_filt = vin[1:3]
self.serial = vin[-6:]
self.plant_filt = vin[10]
else:
print("You're dumb. Enter a real vin (must be 17 characters).")
self.year = self.find(self.year_filt, year_codes)
self.country = self.find(self.country_filt, country_codes)
self.manufact = self.find(self.manufact_filt, model_codes)
self.engine = self.find(self.engine_filt, engine_codes)
print(f"You entered: {vin}")
print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")
def find(self, filt, dict_of_type_of_codes):
return dict_of_type_of_codes.get(filt, 'Not in dict')
#%% Testing
# test vin: 48123658489411439
vinfo("48123658489411439")
我正在为 python class 构建 Vin 解码器,进展顺利,但是我在遍历字典时遇到问题。以下是我目前为该项目编写的所有代码:
class vinfo():
def __init__(self, vin = input("Enter Vin (Without Dashes):")):
""" Data """
model_codes = {
}
year_codes = {
"2021":"M","2020":"L","2019":"K",
"2018":"J","2017":"H","2016":"G",
"2015":"F","2014":"E","2013":"D",
"2012":"C","2011":"B","2010":"A",
"2009":"9","2008":"8","2007":"7",
"2006":"6","2005":"5","2004":"4",
"2003":"3","2002":"2","2001":"1",
"2000":"Y","1999":"X","1998":"W",
"1997":"V","1996":"T","1995":"S",
"1994":"R","1993":"P","1992":"N",
"1991":"M","1990":"L","1989":"K",
"1988":"J","1987":"H","1986":"G",
"1985":"F","1984":"E","1983":"D",
"1982":"C","1981":"B","1980":"A"
}
country_codes = {
"USA":"1", "USA":"4", "USA":"5",
"USA":"7F","Mexico":"3X", "Mexico":"37",
"Canada":"3A", "Canada":"3W",
"Germany":"W", "United Kingdom":"SA",
"United Kingdom":"SM", "Japan":"J",
"Korea":"KL", "Korea":"KR"
}
engine_codes = {
}
if "-" in vin:
vin = vin.replace("-", "")
else:
print("Thanks for entering the Vin without dashes as prompted.")
if len(vin) == 17:
self.country_filt = vin[0]
self.engine_filt = vin[7]
self.year_filt = vin[9]
self.manufact_filt = vin[1:3]
self.serial = vin[-6:]
self.plant_filt = vin[10]
else:
print("You're dumb. Enter a real vin (must be 17 characters).")
self.year = self.find(self.year_filt, year_codes)
self.country = self.find(self.country_filt, country_codes)
self.manufact = self.find(self.manufact_filt, model_codes)
self.engine = self.find(self.engine_filt, engine_codes)
print(f"You entered: {vin}")
print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")
def find(self, filt, dict_of_type_of_codes):
try:
key = (list(dict_of_type_of_codes.keys())[list(dict_of_type_of_codes.values()).index(filt)])
except:
key = "Not in dict"
return key
#%% Testing
# test vin: 48123658489411439
vinfo("48123658489411439")
理想情况下,它会 return 包含汽车制造年份和制造国家/地区的打印声明。年份有效,但找不到国家/地区。 4 是本示例中的国家/地区代码,显然是美国。找不到原因。忽略空字典,一旦我知道我的过程有效,我会填写它们。
提前致谢。
首先,您在创建字典时有重复键。字典是哈希映射,用于搜索唯一键的对应值。
{"USA": "1", "USA": "4", "USA": "5"}
只会让你只剩下一个{"USA": "5"}
其次,你在查字典上做的太多了,违背了使用它的目的。例如。如果您正在根据代码搜索国家/地区,则您的字典应该是相反的,您可以从那里简单地检索每个代码的相应值(国家/地区):
class vinfo:
def __init__(self, vin = input("Enter Vin (Without Dashes):")):
""" Data """
model_codes = {
}
year_codes = {
"M":"2021","L":"2020","K":"2019",
"J":"2018","H":"2017","G":"2016",
"F":"2015","E":"2014","D":"2013",
"C":"2012","B":"2011","A":"2010",
"9":"2009","8":"2008","7":"2007",
"6":"2006","5":"2005","4":"2004",
"3":"2003","2":"2002","1":"2001",
"Y":"2000","X":"1999","W":"1998",
"V":"1997","T":"1996","S":"1995",
"R":"1994","P":"1993","N":"1992",
"M":"1991","L":"1990","K":"1989",
"J":"1988","H":"1987","G":"1986",
"F":"1985","E":"1984","D":"1983",
"C":"1982","B":"1981","A":"1980"
}
country_codes = {
"1": "USA",
"4": "USA",
"5": "USA",
"7F": "USA",
"3X": "Mexico",
"37": "Mexico",
"3A": "Canada",
"3W": "Canada",
"W": "Germany",
"SA": "United Kingdom",
"SM": "United Kingdom",
"J": "Japan",
"KL": "Korea",
"KR": "Korea"
}
engine_codes = {
}
if "-" in vin:
vin = vin.replace("-", "")
else:
print("Thanks for entering the Vin without dashes as prompted.")
if len(vin) == 17:
self.country_filt = vin[0]
self.engine_filt = vin[7]
self.year_filt = vin[9]
self.manufact_filt = vin[1:3]
self.serial = vin[-6:]
self.plant_filt = vin[10]
else:
print("You're dumb. Enter a real vin (must be 17 characters).")
self.year = self.find(self.year_filt, year_codes)
self.country = self.find(self.country_filt, country_codes)
self.manufact = self.find(self.manufact_filt, model_codes)
self.engine = self.find(self.engine_filt, engine_codes)
print(f"You entered: {vin}")
print(f"Year: {self.year}\nManufacturer: {self.manufact}\nCountry: {self.country}\nEngine: {self.engine}")
def find(self, filt, dict_of_type_of_codes):
return dict_of_type_of_codes.get(filt, 'Not in dict')
#%% Testing
# test vin: 48123658489411439
vinfo("48123658489411439")