构造并订购元组列表

时间:2012-07-12 11:07:08

标签: python matplotlib tuples

文字中的数据格式,

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)

我想得到一个元组列表,以符合matplotlib.finance中candlesticks的格式,预计将是

  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)的顺序?

3 个答案:

答案 0 :(得分:2)

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

或者,您可以通过

迭代键
for time in sorted(stats.keys()):
    formatted_data.append(tuple(stats[time]))

答案 1 :(得分: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

如果您认为我错误地解释了原始数据,则根据需要重新排序前两个代码行中的第一个。我承认我对你正在做什么样的应用程序知之甚少,因此不知道不同的变量意味着什么。

哦,如果你想知道我是如何做到这一点的,那就很简单了。逗号是python中的元组解包操作符。

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

依此类推:))

答案 2 :(得分:0)

collections.Counter基于字典,不保留顺序(“A Counter是dict子类”)

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()

相关问题