我想找一家制造了史上最差汽车数量最多的公司。你知道吗
输出如下所示:
Worst manufacturer: Ford
Worst manufacturer: Ford
Worst manufacturer: Triumph
我试着这样做:
def print_worst_manufacturer(car_dict):
for k,v in car_dict.items():
if len(v) > 2:
print("Worst manufacturer:",k)
def test_print_worst_manufacturer():
car_dict = {'Amphicar': ['1961'], 'Corvair': ['1961'], 'Horsey': ['1899'], 'Overland': ['1911'], 'Ford': ['1909', '1958']}
print_worst_manufacturer(car_dict)
car_dict = {'MGA': ['1958'], 'Amphicar': ['1961'], 'Aston': ['1976'], 'Horsey': ['1899'], 'Bricklin': ['1975'], 'Zunndapp': ['1958'], 'Pontiac': ['2001'], 'Chevy': ['1976', '2004'], 'Mosler': ['1985'], 'Lamborghini': ['1986'], 'Peel': ['1966'], 'Ferrari': ['1980'], 'AMC': ['1970', '1978'], 'Yugo': ['1985'], 'BMW': ['2002'], 'Ford': ['1909', '1958', '1971', '1995', '2000'], 'Crosley': ['1949'], 'GM': ['1997'], 'Fuller': ['1933'], 'Chrysler': ['1971'], 'Chrysler/Desoto': ['1934'], 'Corvette': ['1980'], 'Lotus': ['1958'], 'Waterman': ['1957'], 'Overland': ['1911'], 'Triumph': ['1970', '1975'], 'Scripps-Booth': ['1913'], 'King': ['1957'], 'Camaro': ['1982'], 'De': ['1981'], 'Briggs': ['1920'], 'Fiat': ['1998'], 'Corvair': ['1961'], 'Trabant': ['1975'], 'Plymouth': ['1997'], 'Jaguar': ['1974', '2001'], 'Maserati': ['1984'], 'Morgan': ['1975'], 'Hummer': ['2003'], 'Cadillac': ['1981', '1982'], 'Renault': ['1956']}
print_worst_manufacturer(car_dict)
car_dict ={'MGA': ['1958'], 'Amphicar': ['1961'], 'Aston': ['1976'], 'Horsey': ['1899'], 'Bricklin': ['1975'], 'Zunndapp': ['1958'], 'Pontiac': ['2001'], 'Chevy': ['1976', '2004'], 'Mosler': ['1985'], 'Lamborghini': ['1986'], 'Peel': ['1966'], 'Ferrari': ['1980'], 'AMC': ['1970', '1978'], 'Yugo': ['1985'], 'BMW': ['2002'], 'Crosley': ['1949'], 'GM': ['1997'], 'Fuller': ['1933'], 'Chrysler': ['1971'], 'Chrysler/Desoto': ['1934'], 'Corvette': ['1980'], 'Lotus': ['1958'], 'Waterman': ['1957'], 'Overland': ['1911'], 'Triumph': ['1970', '1975', 1958], 'Scripps-Booth': ['1913'], 'King': ['1957'], 'Camaro': ['1982'], 'De': ['1981'], 'Briggs': ['1920'], 'Fiat': ['1998'], 'Corvair': ['1961'], 'Trabant': ['1975'], 'Plymouth': ['1997'], 'Jaguar': ['1974', '2001'], 'Maserati': ['1984'], 'Morgan': ['1975'], 'Hummer': ['2003'], 'Cadillac': ['1981', '1982'], 'Renault': ['1956']}
print_worst_manufacturer(car_dict)
test_print_worst_manufacturer()
输出:
Worst manufacturer: Ford
Worst manufacturer: Triumph
我不知道如何修复我的first def函数。你知道吗
可以使用
max
和key
作为参数,如下例:输出:
list.count
返回参数在列表中出现的次数,但您正在搜索列表中的空字符串,该字符串从不出现:看起来您需要列表的长度(即计算制造商拥有“最差汽车”的年限);请尝试
len
,它返回列表中元素的数量:相关问题 更多 >
编程相关推荐