【问题标题】:Finding the max and min of a variable and finding the corresponding value找到一个变量的最大值和最小值并找到对应的值
【发布时间】:2019-04-11 22:47:18
【问题描述】:

我正在寻找变量“Inc”的最大值和最小值,然后我需要找到“psavings”、“savings”和“Con”的最大值和最小值的对应值。

import csv
import numpy
psavings = []
savings = []
Con = []
Inc = []
Csv_file = open('/Users/charlesadams/Desktop/Lab.csv')
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader)
for row in csv_reader:
    consumption, income = row
    Con.append(float(consumption))
    Inc.append(float(income))
    savings.append(float(income)-float(consumption))
    psavings.append((float(income)-float(consumption))/ float(income) * 100)

【问题讨论】:

    标签: python


    【解决方案1】:
    min_inc = min(Inc)
    max_inc = max(Inc)
    # etc....
    

    【讨论】:

    • 我正在寻找 inc 的最大值和最小值,并从这些值中找到其他变量的对应项
    【解决方案2】:

    我使用这个之前的答案是一个参考:https://stackoverflow.com/a/48519235/3443106

    Numpy 有一个名为 where() 的函数,它返回一个值在 numpy 数组中的位置。

    所以你可以这样做:

    x = np.array([1,8,3,4,0,7,2,3,19,11])
    y = np.array([2,5,3,4,0,1,0,3,4,9])
    y[np.where(x == max(x))]
    

    输出将是array([4])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-21
      • 2012-02-10
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多