2012-07-12 62 views
2

数据格式的文本列表,结构和秩序的元组

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 themthe value of the number (9 < 13)的顺序?

回答

2

您必须对结果列表进行排序。迭代器stats.items()不保证项目顺序。

另外,可以通过

for time in sorted(stats.keys()): 
    formatted_data.append(tuple(stats[time])) 
+0

谢谢。为什么带'13'的线最终在带有'9'的线的前面?元组逐个比较元素。是因为'13'被认为是'string'而不是'int'? – juju 2012-07-12 11:30:39

+0

'dict.keys()'方法的顺序基本上是随机的(它取决于密钥的哈希值) – 2012-07-12 11:34:58

2

首先遍历键的另一种方法来解析文本

2010-04-16,9:15:00,3450,3488,3450,3470 

基本上是

date,time,openprice,closeprice,highprice,lowprice 

和进一步细分

YYYY-MM-DD,HH:MM:SS,openprice,closeprice,highprice,lowprice 

这转化为正则表达式:

r='(\d+)-(\d+)-(\d+),(\d+):(\d+):(\d+),(\d+),(\d+),(\d+),(\d+) 

可以用来生成一个元组

tuple = re.search(r, my_date_string).groups() 

你的问题:为什么项目按照一定的顺序出来

当您像这样将其插入集合中时,将不再对其进行排序。想想这是将糖果装入糖果袋。袋子有黑色的外观。

迭代器的功能是每次取出一颗糖果。你可能拥有的任何优待(如味道,气味,大小)都不重要。唯一能做的,就是迭代器首先想要输出的内容。

回复:您的评论

你的意思是你读的数据,是不是你希望它是什么样不同的格式,因此,你要重新排序的元组以反映您发现任何顺序明智?

如果是这种情况,正则表达式将保持不变:) 但是,您只需将其他索引分配给您的变量。

这可以在Python非常优雅进行(准备谈恋爱):

date,time,openprice,highprice,lowprice,closeprice = tuple #temporarily store them 
tuple = date,time, openprice,closeprice,highprice,lowprice #reorder the tuple 

如果你认为我已经解释的原始数据错误,然后重新排序第一前两次codelines如需要。我承认我对你正在制作的应用程序没有太多的知识,因此不知道不同的变量是什么意思。

哦,如果你想知道我是如何做到这一点的,那很简单。逗号是Python中的元组解包运算符。

>>>tuple = ('a', 'b' , 'c') 
>>>first,second,third = tuple 
>>>first 
    'a' 

等等:)

+0

这很可爱。唯一的数据是不正确的顺序。 ClosePrice在原始数据“openprice,highprice,lowprice,closeprice”中落后于LowPrice。有什么办法可以解决这个问题吗?我希望这个元组是“openprice,closeprice,highprice,lowprice”。 – juju 2012-07-13 01:33:35

0

collections.Counter基于字典,不维持秩序(“A计数器是一个字典子”)

an example in the collections docs它展示了如何结合它应该做的collections.OrderedDictcollections.Counter你想要什么:

from collections import Counter, OrderedDict 


class OrderedCounter(Counter, OrderedDict): 
    'Counter that remembers the order elements are first encountered' 

    def __repr__(self): 
     return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) 

    def __reduce__(self): 
     return self.__class__, (OrderedDict(self),) 

然后,只需改变stats = collections.Counter()stats = OrderedCounter()