简体   繁体   English

使用核心 python 将列表中的列相乘,无需任何导入库

[英]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.

相关问题 在不使用 python 中的库的情况下,将两个不同大小的元素列表相乘 - Multiply two list of different sizes element wise without using libraries in python 如何在不使用任何库(如 pandas)的情况下拆分 Python 中的列表? - How to split a list in Python without using any libraries like pandas? Python:如何在不使用任何预建函数的情况下将列表中的所有项目相乘? - Python: How may I multiply all items in a list without using any prebuilt function? 迭代列表的开头,而无需在Python中进行任何导入 - Iterate the start of a list without any import in Python 如何在不使用任何库(如 Itertools)的情况下在 Python 中查找字符串或列表的所有组合? - How To Find All Combinations Of String OR List In Python Without Using Any Libraries Like Itertools? 不使用库或导入的dict数据处理列表 - Data processing List of dict without using libraries or import 列表列表上的Python点乘列表,不使用numpy - Python dot-multiply lists on list of lists, without using numpy 仅使用python检查和处理没有任何包或库的平面文件 - Using only python to examine and process a flatfile without any packages or libraries 不使用任何库或模块的 Python 随机函数 - Python Random Function without using ANY libraries or modules 将自动密钥库导入python脚本的任何方法 - Any way to import autokey libraries into python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM