[英]multiply columns in the list using core python, without any import libraries
If I have the data like this如果我有这样的数据
[['6', '0.24','B','C'], ['6', '0.22','A','C'], ['4', '0.20', 'A','X']]
and now I want to find out max of (6/.24, 6/0.22, 4/0.20)
AND finally, which ever has max value, show col values at index 2 and 3. Output I am expecting should look something like this: 27.27273, A, C
(A, C are the corresponding values in the list where we found max value) Thanks again.现在我想找出
(6/.24, 6/0.22, 4/0.20)
的最大值,最后,它曾经有最大值,在索引 2 和 3 处显示 col 值。我期望的 Output 应该看起来像这样: 27.27273, A, C
(A, C 是我们找到最大值的列表中的对应值) 再次感谢。
Sort the list by the difference between first 2 elements按前 2 个元素之间的差异对列表进行排序
lst = [['6', '0.24','B','C'], ['6', '0.22','A','C'], ['4', '0.20', 'A','X']]
sorted_lst = sorted(lst, key=lambda x: float(x[0])/float(x[1]), reverse=True)
x = sorted_lst[0]
result = [float(x[0])/float(x[1]), x[2], x[3]]
print(result)
Output: Output:
[27.272727272727273, 'A', 'C']
Build a list of the (6/.24, 6/0.22, 4/0.20)
values and then take the max.建立一个
(6/.24, 6/0.22, 4/0.20)
值的列表,然后取最大值。
a = [['6', '0.24','B','C'], ['6', '0.22','A','C'], ['4', '0.20', 'A','X']]
b = max([float(x[0])/float(x[1]) for x in a])
print(b)
def get_maxrow(data):
x = [[float(i[0])/float(i[1]), *i[2:]] for i in data]
return max(x)
x = [['6', '0.24','B','C'], ['6', '0.22','A','C'], ['4', '0.20', 'A','X']]
y = get_maxrow(x)
print(y) # [27.272727272727273, 'A', 'C']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.