【发布时间】:2013-10-25 11:54:53
【问题描述】:
有人能帮我解决以下问题吗?我自己试过了,我也附上了解决方案。我使用了二维列表,但我想要一个没有二维列表的不同解决方案,它应该更 Pythonic。
请向我建议你们中的任何人有其他方法。
Q) 考虑在 CSV 文件中给出自 1990 年以来每个月的 N 家公司的股价。文件格式如下,以第一行为标题。
年,月,A公司,B公司,C公司,......公司N
1990 年 1 月 10 日、15 日、20 日、......、50
1990 年 2 月 10 日、15 日、20 日、......、50
.
.
.
.
2013 年 9 月 50 日、10 日、15 日…………500
解决方案应该采用这种格式。 a) 公司股价最高年份和月份的列表。
这是我使用二维列表的答案。
def generate_list(file_path):
'''
return list of list's containing file data.'''
data_list=None #local variable
try:
file_obj = open(file_path,'r')
try:
gen = (line.split(',') for line in file_obj) #generator, to generate one line each time until EOF (End of File)
for j,line in enumerate(gen):
if not data_list:
#if dl is None then create list containing n empty lists, where n will be number of columns.
data_list = [[] for i in range(len(line))]
if line[-1].find('\n'):
line[-1] = line[-1][:-1] #to remove last list element's '\n' character
#loop to convert numbers from string to float, and leave others as strings only
for i,l in enumerate(line):
if i >=2 and j >= 1:
data_list[i].append(float(l))
else:
data_list[i].append(l)
except IOError, io_except:
print io_except
finally:
file_obj.close()
except IOError, io_exception:
print io_exception
return data_list
def generate_result(file_path):
'''
return list of tuples containing (max price, year, month,
company name).
'''
data_list = generate_list(file_path)
re=[] #list to store results in tuple formet as follow [(max_price, year, month, company_name), ....]
if data_list:
for i,d in enumerate(data_list):
if i >= 2:
m = max(data_list[i][1:]) #max_price for the company
idx = data_list[i].index(m) #getting index of max_price in the list
yr = data_list[0][idx] #getting year by using index of max_price in list
mon = data_list[1][idx] #getting month by using index of max_price in list
com = data_list[i][0] #getting company_name
re.append((m,yr,mon,com))
return re
if __name__ == '__main__':
file_path = 'C:/Document and Settings/RajeshT/Desktop/nothing/imp/New Folder/tst.csv'
re = generate_result(file_path)
print 'result ', re
I have tried to solve it with generator also, but in that case it was giving result for only one company i.e. only one column.
p = 'filepath.csv'
f = open(p,'r')
head = f.readline()
gen = ((float(line.split(',')[n]), line.split(',',2)[0:2], head.split(',')[n]) for n in range(2,len(head.split(','))) for i,line in enumerate(f))
x = max((i for i in gen),key=lambda x:x[0])
print x
您可以获取以下提供的 csv 格式的输入数据..
year,month,company 1,company 2,company 3,company 4,company 5
1990,jan,201,245,243,179,133
1990,feb,228,123,124,121,180
1990,march,63,13,158,88,79
1990,april,234,68,187,67,135
1990,may,109,128,46,185,236
1990,june,53,36,202,73,210
1990,july,194,38,48,207,72
1990,august,147,116,149,93,114
1990,september,51,215,15,38,46
1990,october,16,200,115,205,118
1990,november,241,86,58,183,100
1990,december,175,97,143,77,84
1991,jan,190,68,236,202,19
1991,feb,39,209,133,221,161
1991,march,246,81,38,100,122
1991,april,37,137,106,138,26
1991,may,147,48,182,235,47
1991,june,57,20,156,38,245
1991,july,165,153,145,70,157
1991,august,154,16,162,32,21
1991,september,64,160,55,220,138
1991,october,162,72,162,222,179
1991,november,215,207,37,176,30
1991,december,106,153,31,247,69
预期输出如下。
[(246.0, '1991', 'march', 'company 1'),
(245.0, '1990', 'jan', 'company 2'),
(243.0, '1990', 'jan', 'company 3'),
(247.0, '1991', 'december', 'company 4'),
(245.0, '1991', 'june', 'company 5')]
提前谢谢...
【问题讨论】:
-
是 numpy 还是 pandas 一个选项?
-
任何你认为更 Pythonic 的东西,并且只能最大限度地使用标准库函数......请不要第三方......
-
好的,pandas 和 numpy 是你必须导入的库,所以我猜你会调用第三方,但它们非常适合这种应用程序。但是您也可以使用标准方法来做到这一点......
-
这是因为他们没有提供标准库。这就是为什么..如果你有不止一种方法来解决这个问题,欢迎你...... :)
-
你能发布一些实际的样本数据和预期的输出吗?
标签: python python-2.7 csv python-3.x generator