查找字典中最大的区域
Finding largest areas in dictionary
我正在编写一个函数,用于查找字典。字典包含艺术家作为键,他们的画作作为值。我需要在字典中找到面积最大的这幅画,如果有两幅面积相等,则应将它们 return 编辑为元组列表。
示例词典:
{
'A, Jr.':[("One",1400,10,20.5,"oil paint","Austria"),("Three",1400,100.0,100.0,"oil paint","France"),("Twenty",1410,50.0,200.0,"oil paint","France")],
'X':[("Eight",1460, 100.0, 20.0, "oil paint","France"),("Six",1465,10.0, 23.0, "oil paint", "France"),("Ten",1465,12.0,15.0,"oil paint","Austria"),("Thirty",1466,30.0,30.0,"watercolor","Germany")],
'M':[("One, Two", 1500, 10.0, 10.0, "panel","Germany")]
}
基本上,四位数字是绘画或艺术品的创作年份,接下来的两位数字是长度和宽度。我需要 return 乘以长度和宽度时面积最大的值。所以对于上面的字典,函数 find_largest 应该 return
find_largest(dictionary2())
[('A, Jr.', 'Three'), ('A, Jr.', 'Twenty')]
因为 "Three" 绘画是 100 * 100 = 10,000 而 "Twenty" 绘画是 50 * 200 = 10,000 它们都被 return 编辑为列表中的元组。
有人对如何执行此操作有建议吗?我已经在下面开始编写代码,但我认为这不是正确的方法。
def find_largest(dictionary):
matches = {}
for key, the_list in db.items():
for record in the_list:
value = record[4]
if dictionary in record:
if key in matches:
max(the_list)
max(lst, key=lambda tupl: tupl[2]*tupl[3])
matches[key].append(record)
else:
matches[key] = [record]
return matches
这基本上是我对较早函数的代码进行了一些重大更改。这个基本框架已经实现了我的一些目标。我添加了 max(matches) 但我意识到这并没有多大作用,除非该函数将长度和宽度相乘,然后寻找最大值。如果有人有建议会有所帮助
只记录您当前的最大值可能会更容易
data = {
'A, Jr.':[("One",1400,10,20.5,"oil paint","Austria"),("Three",1400,100.0,100.0,"oil paint","France"),("Twenty",1410,50.0,200.0,"oil paint","France")],
'X':[("Eight",1460, 100.0, 20.0, "oil paint","France"),("Six",1465,10.0, 23.0, "oil paint", "France"),("Ten",1465,12.0,15.0,"oil paint","Austria"),("Thirty",1466,30.0,30.0,"watercolor","Germany")],
'M':[("One, Two", 1500, 10.0, 10.0, "panel","Germany")]
}
def find_largest(d):
matches = []
max_value = 0
for key in d:
for record in d[key]:
value = record[2] * record[3]
if value > max_value:
matches = [(key, record[0])]
max_value = value
elif value == max_value:
matches.append((key, record[0]))
return matches
# Output
>>> find_largest(data)
[('A, Jr.', 'Three'), ('A, Jr.', 'Twenty')]
我正在编写一个函数,用于查找字典。字典包含艺术家作为键,他们的画作作为值。我需要在字典中找到面积最大的这幅画,如果有两幅面积相等,则应将它们 return 编辑为元组列表。
示例词典:
{
'A, Jr.':[("One",1400,10,20.5,"oil paint","Austria"),("Three",1400,100.0,100.0,"oil paint","France"),("Twenty",1410,50.0,200.0,"oil paint","France")],
'X':[("Eight",1460, 100.0, 20.0, "oil paint","France"),("Six",1465,10.0, 23.0, "oil paint", "France"),("Ten",1465,12.0,15.0,"oil paint","Austria"),("Thirty",1466,30.0,30.0,"watercolor","Germany")],
'M':[("One, Two", 1500, 10.0, 10.0, "panel","Germany")]
}
基本上,四位数字是绘画或艺术品的创作年份,接下来的两位数字是长度和宽度。我需要 return 乘以长度和宽度时面积最大的值。所以对于上面的字典,函数 find_largest 应该 return
find_largest(dictionary2())
[('A, Jr.', 'Three'), ('A, Jr.', 'Twenty')]
因为 "Three" 绘画是 100 * 100 = 10,000 而 "Twenty" 绘画是 50 * 200 = 10,000 它们都被 return 编辑为列表中的元组。
有人对如何执行此操作有建议吗?我已经在下面开始编写代码,但我认为这不是正确的方法。
def find_largest(dictionary):
matches = {}
for key, the_list in db.items():
for record in the_list:
value = record[4]
if dictionary in record:
if key in matches:
max(the_list)
max(lst, key=lambda tupl: tupl[2]*tupl[3])
matches[key].append(record)
else:
matches[key] = [record]
return matches
这基本上是我对较早函数的代码进行了一些重大更改。这个基本框架已经实现了我的一些目标。我添加了 max(matches) 但我意识到这并没有多大作用,除非该函数将长度和宽度相乘,然后寻找最大值。如果有人有建议会有所帮助
只记录您当前的最大值可能会更容易
data = {
'A, Jr.':[("One",1400,10,20.5,"oil paint","Austria"),("Three",1400,100.0,100.0,"oil paint","France"),("Twenty",1410,50.0,200.0,"oil paint","France")],
'X':[("Eight",1460, 100.0, 20.0, "oil paint","France"),("Six",1465,10.0, 23.0, "oil paint", "France"),("Ten",1465,12.0,15.0,"oil paint","Austria"),("Thirty",1466,30.0,30.0,"watercolor","Germany")],
'M':[("One, Two", 1500, 10.0, 10.0, "panel","Germany")]
}
def find_largest(d):
matches = []
max_value = 0
for key in d:
for record in d[key]:
value = record[2] * record[3]
if value > max_value:
matches = [(key, record[0])]
max_value = value
elif value == max_value:
matches.append((key, record[0]))
return matches
# Output
>>> find_largest(data)
[('A, Jr.', 'Three'), ('A, Jr.', 'Twenty')]