2012-07-12 63 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()