字典列表中的最小值和最大值输出不正确

时间:2020-09-09 12:58:18

标签: python-3.x

我有像这样的词典列表

string field1 = null, field2 = "abc", field3 = "def", field4 = null;
string[] input = { field1, field2, field3, field4};
string result = input.SkipWhile(x => x != "abc").Skip(1).FirstOrDefault(x => x != null);

并使用以下方法计算最高费用

t = [{'Sr. No.': '983', 'Hotel Code': 'TN05553556', 'State': 'Tamilnadu', 'Cost': '1110', 'Ratings': '5.3'}, {'Sr. No.': '984', 'Hotel Code': 'KA86403535', 'State': 'Karnataka', 'Cost': '1010', 'Ratings': '6.5'}, {'Sr. No.': '985', 'Hotel Code': 'TN23687028', 'State': 'Tamilnadu', 'Cost': '1790', 'Ratings': '5.5'}, {'Sr. No.': '986', 'Hotel Code': 'KA36970858', 'State': 'Karnataka', 'Cost': '1300', 'Ratings': '8.6'}, {'Sr. No.': '987', 'Hotel Code': 'MH38461439', 'State': 'Maharashtra', 'Cost': '2200', 'Ratings': '3'}, {'Sr. No.': '988', 'Hotel Code': 'KA20076818', 'State': 'Karnataka', 'Cost': '1340', 'Ratings': '7.1'}, {'Sr. No.': '989', 'Hotel Code': 'TN13118419', 'State': 'Tamilnadu', 'Cost': '910', 'Ratings': '6.2'}, {'Sr. No.': '990', 'Hotel Code': 'KA14225819', 'State': 'Karnataka', 'Cost': '470', 'Ratings': '2.6'}, {'Sr. No.': '991', 'Hotel Code': 'MH29527861', 'State': 'Maharashtra', 'Cost': '910', 'Ratings': '2.2'}, {'Sr. No.': '992', 'Hotel Code': 'MH29382347', 'State': 'Maharashtra', 'Cost': '2160', 'Ratings': '8.2'}, {'Sr. No.': '993', 'Hotel Code': 'MH11989011', 'State': 'Maharashtra', 'Cost': '2110', 'Ratings': '2'}, {'Sr. No.': '994', 'Hotel Code': 'KA25450392', 'State': 'Karnataka', 'Cost': '1450', 'Ratings': '9'}, {'Sr. No.': '995', 'Hotel Code': 'KA66177616', 'State': 'Karnataka', 'Cost': '2030', 'Ratings': '2.3'}, {'Sr. No.': '996', 'Hotel Code': 'MH45682094', 'State': 'Maharashtra', 'Cost': '1340', 'Ratings': '2.4'}, {'Sr. No.': '997', 'Hotel Code': 'TN79155458', 'State': 'Tamilnadu', 'Cost': '1620', 'Ratings': '4.6'}, {'Sr. No.': '998', 'Hotel Code': 'KA26726731', 'State': 'Karnataka', 'Cost': '850', 'Ratings': '8'}, {'Sr. No.': '999', 'Hotel Code': 'TN09700536', 'State': 'Tamilnadu', 'Cost': '1550', 'Ratings': '5.3'}, {'Sr. No.': '1000', 'Hotel Code': 'MH38559747', 'State': 'Maharashtra', 'Cost': '600', 'Ratings': '4'}]

并且理想情况下应该给出正确的最大值,但输出为

maxPricedItem = max(t, key=lambda x:x["Cost"])

这显然是错误的。我无法确定为什么会这样。当词典列表的长度相对较小时,它可以工作。请提供一些指导。

2 个答案:

答案 0 :(得分:2)

您正在比较字符串,因此请在关键函数中将Cost转换为整数:

Grid

打印:

g.RenderSize.Width;
g.RenderSize.Height;
g.ColumnDefinitions[0].Width
g.ColumnDefinitions[0].ActualWidth
g.ColumnDefinitions[0].MinWidth

答案 1 :(得分:0)

如果打算仅获取最大值,请使用list-comprehension

print(max([int(x['Cost']) for x in t]))  # 2200