Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 构建Python 3的目录_Python 3.x_Sorting_Dictionary - Fatal编程技术网

Python 3.x 构建Python 3的目录

Python 3.x 构建Python 3的目录,python-3.x,sorting,dictionary,Python 3.x,Sorting,Dictionary,我正在阅读一个csv文件,我必须建立一个列表的目录,其中的键是股票代码,值是该代码的收盘价列表。我很难按照每只股票的最高和最低价格来分类 预期输出为: >GOOG: 180.38 difference (672.93-492.55) >LNKD: 100.82 difference (270.76-169.94) >AAPL: 36.74 difference (133.0-96.26) >FB: 25.76 difference (98.39-72.63) &g

我正在阅读一个csv文件,我必须建立一个列表的目录,其中的键是股票代码,值是该代码的收盘价列表。我很难按照每只股票的最高和最低价格来分类

预期输出为:

>GOOG:  180.38 difference (672.93-492.55)
>LNKD:  100.82 difference (270.76-169.94)
>AAPL:  36.74 difference (133.0-96.26)
>FB:  25.76 difference (98.39-72.63)
>MSFT:  9.32 difference (49.61-40.29)
我的示例csv表文件:

>Ticker  Date       Open       High     Low     Close     Volume
>GOOG    25-Sep-15  629.77    629.77    611     611.97    2174009
>GOOG    24-Sep-15  616.64    627.32    612.4   625.8     2240098
我的代码没有产生预期的行为。我正在努力循环dict中的键,并按max-min值对它们进行排序:

file_data = open('../python_data/stock_prices.csv').readlines()[1:]
stock_dict = {}

def price_diff(key):
    change_price = max(stock_dict[key]) - min(stock_dict[key])
    return (change_price)


for line in file_data:
    line = line.split(',')
    ticker = line[0]
    if ticker not in stock_dict:
        stock_dict[ticker] = []
    stock_dict[ticker].append(float(line[5]))

sorted_keys = sorted(stock_dict, key=price_diff, reverse=True)
#print(sorted_keys)

for key in stock_dict:
    print(key, round(max(stock_dict[key]) - min(stock_dict[key]),2))

谢谢@zyzue的反馈。经过大量研究,我得出了这个结论,并得到了预期的输出

file_data = open('../python_data/stock_prices.csv').readlines()[1:]
stock_dict = {}

def price_diff(key):
    change_price = max(stock_dict[key]) - min(stock_dict[key])
    return (change_price)

for line in file_data:
    line = line.split(',')
    ticker = line[0]
    if ticker not in stock_dict:
        stock_dict[ticker] = []
    stock_dict[ticker].append(float(line[5]))

sorted_keys = sorted(stock_dict, key=price_diff, reverse=True)
for key in sorted_keys:
    min_val = min(stock_dict[key])
    max_val = max(stock_dict[key])
    change_val = round(max(stock_dict[key]) - min(stock_dict[key]),2)
    print("{}: {} difference ({} - {})".format (key, change_val, max_val, min_val))

你认识熊猫吗?你应该考虑使用熊猫,而不是解析CSV自己,除非你把它作为演习Zy雪,谢谢快速响应。我这样做是为了练习。你不应该对字典进行排序,而不是
排序(stock\u dict)
,尝试
排序(stock\u dict.items())
file_data = open('../python_data/stock_prices.csv').readlines()[1:]
stock_dict = {}

def price_diff(key):
    change_price = max(stock_dict[key]) - min(stock_dict[key])
    return (change_price)

for line in file_data:
    line = line.split(',')
    ticker = line[0]
    if ticker not in stock_dict:
        stock_dict[ticker] = []
    stock_dict[ticker].append(float(line[5]))

sorted_keys = sorted(stock_dict, key=price_diff, reverse=True)
for key in sorted_keys:
    min_val = min(stock_dict[key])
    max_val = max(stock_dict[key])
    change_val = round(max(stock_dict[key]) - min(stock_dict[key]),2)
    print("{}: {} difference ({} - {})".format (key, change_val, max_val, min_val))