如何通过计算dict中的值来获取密钥?

2024-09-24 22:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找一家制造了史上最差汽车数量最多的公司。你知道吗

输出如下所示:

 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函数。你知道吗


Tags: testdefcardictprintfordmanufacturerworst
3条回答

可以使用maxkey作为参数,如下例:

def get_worst(a):
    # For: car_dict2:
    # max(a.items(), key=lambda x: len(x[1]))
    # Will return:
    # ('Ford', ['1909', '1958', '1971', '1995', '2000'])
    # So, we take only the first element
    return max(a.items(), key=lambda x: len(x[1]))[0]

car_dict1 = {'Amphicar': ['1961'], 'Corvair': ['1961'], 'Horsey':    ['1899'], 'Overland': ['1911'], 'Ford': ['1909', '1958']}
car_dict2 = {'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']}
car_dict3 = {'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("car_dict1: Worst manufacturer is:", get_worst(car_dict1))
print("car_dict2: Worst manufacturer is:", get_worst(car_dict2))
print("car_dict3: Worst manufacturer is:", get_worst(car_dict3))

输出:

car_dict1: Worst manufacturer is: Ford
car_dict2: Worst manufacturer is: Ford
car_dict3: Worst manufacturer is: Triumph
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']}

# Transform the dict into a list 

k = car_dict.keys ()
v = car_dict.values ()
car_list = zip (k,v)

# Sort the List: We take the second element of the List items (which is a list of the years) and use its length as the sorting criteria 

car_list.sort (key = lambda x: len (x [1]))

# the last Element in the list is the one with the most years:

print car_list [-1] [0]

list.count返回参数在列表中出现的次数,但您正在搜索列表中的空字符串,该字符串从不出现:

In [1]: ["a", "b"].count("a")
Out[1]: 1

In [2]: ["a", "b"].count("")
Out[2]: 0

看起来您需要列表的长度(即计算制造商拥有“最差汽车”的年限);请尝试len,它返回列表中元素的数量:

In [3]: len(["a", "b"])
Out[3]: 2

相关问题 更多 >