如何找到字典的最大值

时间:2017-09-24 02:55:13

标签: python python-3.x dictionary

假设我在这里有一本字典:

stock_price = { 'AAPL' : [100,200,100.3,100.55,200.33],
                'GOOGL': [100.03,200.11,230.33,100.20],
                'SSNLF': [100.22,150.22,300,200,100.23],
                'MSFT' : [100.89,200,100,500,200.11,600]}

列表中的每个值都来自特定的时间段。 (即AAPL股票为100,GOOGL股票为100.03,AAPL股票为100.3,SSNLF股票为150.22,为期2),等等。

所以我在这里创建一个功能,帮助我找到特定时期的最高股价。

def maximum(periods):
    """
    Determine the max stock price at a time of the day  

    Parameters
    ----------
    times: a list of the times of the day we need to calculate max stock for 

    Returns
    ----------
    A list

result = []

#code here

return result

我的目标是输入期间,使函数看起来最大([期间]),以便找到该时间段的最大股票价格。

预期结果示例应如下所示:

  
    
      

最大值([0,1])

             

[100.89,200.11]

    
  

这表明100.89是所有股票中第1期的最高价格,而200.11是第2期的最高价格。

2 个答案:

答案 0 :(得分:1)

我相信你正在寻找这样的东西:

[100.89, 200.11]

输出:

*args

通过使用print(list(maximum(0, 1, 2, 3))) ,您可以根据需要指定任意数量的列:

[100.89, 200.11, 300, 500]

输出:

// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher) {
    // TODO: Fetch URLs in parallel.
    // TODO: Don't fetch the same URL twice.
    // This implementation doesn't do either:
    if depth <= 0 {
        fmt.Printf("depth <= 0 return")
        return
    }
    body, urls, err := fetcher.Fetch(url)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("found: %s %q\n", url, body)
    crawled.mux.Lock()
    crawled.c[url]++
    crawled.mux.Unlock()
    for _, u := range urls {
        //crawled.mux.Lock()
        if cnt, ok := crawled.c[u]; ok {
            cnt++
        } else {
            fmt.Println("go ...", u)
            go Crawl(u, depth-1, fetcher)
        }
        //crawled.mux.Unlock()
        //Crawl(u, depth-1, fetcher)
    }
    return
}


type crawledUrl struct {
    c   map[string]int
    mux sync.Mutex
}

var crawled = crawledUrl{c: make(map[string]int)}

答案 1 :(得分:0)

您可以使用dict.values来迭代字典值。使用list comprehension / generator表达式获取超出值的周期值;使用max获取最大值:

# 1st period (0) prices
>>> [prices[0] for prices in stock_price.values()]
[100, 100.03, 100.89, 100.22]

# To get multiple periods prices
>>> [[prices[0] for prices in stock_price.values()],
     [prices[1] for prices in stock_price.values()]]
[[100, 100.03, 100.89, 100.22], [200, 200.11, 200, 150.22]]
>>> [[prices[0] for prices in stock_price.values()] for period in [0, 1]]
[[100, 100.03, 100.89, 100.22], [100, 100.03, 100.89, 100.22]]

>>> max([100, 100.03, 100.22, 100.89])
100.89
>>> stock_price = { 'AAPL' : [100,200,100.3,100.55,200.33],
...                 'GOOGL': [100.03,200.11,230.33,100.20],
...                 'SSNLF': [100.22,150.22,300,200,100.23],
...                 'MSFT' : [100.89,200,100,500,200.11,600]}
>>> 
>>> def maximum(periods):
...     return [max(prices[period] for prices in stock_price.values())
...             for period in periods]
... 
>>> maximum([0, 1])
[100.89, 200.11]