数据格式的文本列表,结构和秩序的元组
2010-04-16,9:15:00,3450,3488,3450,3470
分析文本,
Utuple = collections.namedtuple('Utuple', 'DT,OpenPrice,ClosePrice,HighPrice,LowPrice')
stats = collections.Counter()
for line in data.readlines():
cols = line.split(',')
Date = cols[0]
d = Date.split('-')
Time = cols[1]
t = Time.split(':')
DT = datetime(int(d[0]), int(d[1]), int(d[2]), int(t[0]), int(t[1]), int(t[2]))
DT = mdates.date2num(DT)
OpenPrice = float(cols[2])
HighPrice = float(cols[3])
LowPrice = float(cols[4])
ClosePrice = float(cols[5])
stats[DT] = Utuple(DT,OpenPrice,ClosePrice,HighPrice,LowPrice)
我想要得到一个元组的名单,以适应的candlesticks
在matplotlib格式.finance,这有望成为
D = [(datetime.datetime(2010, 4, 16, 9, 30), 311, 332, 344, 311),
(datetime.datetime(2010, 4, 16, 9, 31), 312, 332, 344, 311),
(datetime.datetime(2010, 4, 16, 9, 32), 323, 332, 344, 320),
(datetime.datetime(2010, 4, 16, 13, 0), 331, 332, 344, 330),
(datetime.datetime(2010, 4, 16, 13, 1), 335, 342, 348, 333)]
和我所做的:
formated_data = []
for time, index in stats.items():
formated_data.append(tuple(index))
我想保留此订单。但是,在formated_data
中,datetime.datetime的第四列中的13
的行结束于9
的前面。如何保持元组by the order that I save them
或the value of the number (9 < 13)
的顺序?
谢谢。为什么带'13'的线最终在带有'9'的线的前面?元组逐个比较元素。是因为'13'被认为是'string'而不是'int'? – juju 2012-07-12 11:30:39
'dict.keys()'方法的顺序基本上是随机的(它取决于密钥的哈希值) – 2012-07-12 11:34:58