简体   繁体   English

使OrderedDict脱离列表

[英]Making OrderedDict out of Lists

I am trying to modify my code so that it will work over different time spans. 我正在尝试修改我的代码,以便它可以在不同的时间范围内工作。 I want my variable days_dict to look like months_dict. 我希望我的变量days_dict看起来像months_dict。

I have this variable called months_dict which works very well. 我有一个名为months_dict的变量,它工作得很好。 I made it using these lines of code 我使用这些代码行

graphmonths = [pivot_table[(m)].astype(float).values for m in range(1, 13)]
names = ["Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"]
months_dict = OrderedDict(list(zip(names, graphmonths)))

It looks like this 看起来像这样

months_dict
Out[86]: 
OrderedDict([('Jan', array([ 17.,   4.,  11.,  12.,  13.,   6.,   9.,   7.,   8.,   7.,   4.,
         3.,   5.,   3.,   4.,   4.,   5.,   6.,   3.,   4.,  10.,   5.,
         8.,   5.,   3.,   4.,   9.,   4.,   2.,   2.])), ('Feb', array([ 30.,  11.,   8.,  11.,  10.,  10.,   1.,   8.,   3.,   5.,   5.,
        11.,   6.,   8.,   3.,   5.,   2.,   5.,  15.,   2.,   3.,   8.,
         6.,   6.,   5.,   4.,   9.,   6.,  11.,   1.])), ('Mar', array([ 29.,  13.,  25.,   6.,   7.,  11.,   2.,   9.,   9.,   5.,   4.,
         7.,  12.,   5.,   5.,   8.,   8.,   5.,  13.,   6.,   8.,   3.,
         7.,   5.,  10.,   5.,   6.,   4.,   3.,   4.])), ('Apr', array([ 39.,  22.,  24.,  23.,  14.,   8.,  20.,   7.,   8.,   3.,   6.,
         7.,   6.,   6.,   6.,   3.,  15.,   8.,   4.,   1.,   5.,   2.,
         4.,   7.,   2.,   4.,   6.,   3.,   5.,   0.])), ('May', array([ 15.,  34.,   7.,  11.,   6.,   3.,   6.,  11.,   9.,   3.,   5.,
         5.,  10.,   1.,   5.,   4.,   2.,   4.,   5.,   5.,   2.,   3.,
        13.,   9.,   4.,   7.,   5.,   3.,   5.,   0.])), ('Jun', array([ 27.,  27.,  13.,  11.,   8.,   4.,   7.,   4.,   7.,   7.,   4.,
         9.,   6.,   4.,   6.,   4.,   7.,   9.,   1.,   3.,   2.,  11.,
         8.,   1.,   4.,   4.,   5.,   1.,   3.,  10.])), ('Jul', array([ 22.,  30.,  24.,   9.,   5.,  10.,   6.,   3.,   5.,   9.,  12.,
         6.,   4.,   6.,   5.,  10.,   6.,   7.,   1.,   9.,   2.,   6.,
         0.,   8.,   6.,   2.,   3.,   6.,   5.,   9.])), ('Aug', array([ 12.,  18.,   0.,   4.,  10.,   8.,   4.,   3.,   7.,   7.,  14.,
         3.,   5.,  10.,   5.,   7.,   6.,   2.,   0.,   8.,  20.,  10.,
         1.,   5.,   7.,   8.,   3.,   0.,   5.,  12.])), ('Sep', array([ 36.,  29.,  21.,   6.,  13.,  11.,   6.,   6.,   6.,  11.,   5.,
         6.,   3.,   6.,   4.,   5.,   6.,   5.,   7.,   8.,   2.,   3.,
         1.,   4.,   5.,   6.,   3.,   3.,  10.,   7.])), ('Oct', array([ 21.,  31.,  12.,  11.,   8.,  11.,   6.,   5.,   9.,   6.,   8.,
         5.,   4.,   4.,   7.,   4.,   1.,   6.,   9.,   3.,   5.,   7.,
         6.,   7.,   6.,   6.,   3.,   4.,   4.,   8.])), ('Nov', array([ 18.,  17.,  12.,   5.,  12.,  18.,  12.,   8.,   3.,  10.,   2.,
         3.,   9.,   4.,  12.,   6.,   5.,   4.,   2.,   8.,   4.,   5.,
         4.,   3.,   2.,   3.,   4.,  21.,   3.,   3.])), ('Dec', array([ 15.,  14.,  17.,  10.,  11.,  14.,   7.,  11.,   5.,   3.,   6.,
         9.,   3.,  15.,   9.,  11.,   5.,   7.,   5.,   7.,   1.,   1.,
         4.,   1.,   7.,   7.,   3.,   4.,   2.,   2.]))])

Now I am trying to do the same but for a month. 现在,我试图做同样的事情,但是持续了一个月。 So I have 所以我有

Days = pivot_table.columns[1:].tolist()

this gives 这给

Days
Out[80]: 
[1L,
 2L,
 3L,
 4L,
 5L,
 6L,
 7L,
 8L,
 9L,
 10L,
 11L,
 12L,
 13L,
 14L,
 15L,
 16L,
 17L,
 18L,
 19L,
 20L,
 21L,
 22L,
 23L,
 24L,
 25L,
 26L,
 27L,
 28L,
 29L,
 30L]

Then I have 那我有

graphdays = [pivot_table[(m)].astype(float).values for m in range(1, len(Days)+1)]
days_dict = OrderedDict(list(zip(Days, graphdays)

but days_dict looks like this: 但是days_dict看起来像这样:

days_dict
Out[73]: 
OrderedDict([(1L, array([ 3.,  0.,  0.,  0.,  0.,  2.,  0.,  1.,  0.,  0.,  2.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  1.,
        0.,  0.,  0.,  0.])), (2L, array([ 1.,  1.,  0.,  0.,  3.,  1.,  0.,  1.,  1.,  1.,  0.,  1.,  0.,
        0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.,  0.])), (3L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (4L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,
        0.,  0.,  0.,  0.])), (5L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (6L, array([ 1.,  3.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,
        0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  1.,  0.])), (7L, array([ 2.,  2.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  4.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  0.,
        0.,  0.,  2.,  0.])), (8L, array([ 9.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  1.,  1.,  0.,  0.,  1.,  0.,  0.,  1.,  2.,  0.,  0.,
        0.,  0.,  0.,  0.])), (9L, array([ 1.,  0.,  4.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,
        0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  3.,  2.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (10L, array([ 2.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (11L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (12L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (13L, array([ 1.,  0.,  0.,  2.,  1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  0.,  0.,  0.])), (14L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (15L, array([  0.,   2.,  10.,   1.,   0.,   0.,   1.,   1.,   0.,   1.,   1.,
         3.,   0.,   2.,   0.,   1.,   0.,   2.,   0.,   0.,   0.,   0.,
         0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.])), (16L, array([ 0.,  4.,  3.,  0.,  0.,  0.,  5.,  2.,  1.,  0.,  0.,  0.,  3.,
        1.,  0.,  0.,  1.,  4.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (17L, array([ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  2.,  1.,  0.,  1.,
        0.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  0.,  0.,  0.])), (18L, array([ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,
        0.,  0.,  0.,  0.])), (19L, array([ 4.,  1.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (20L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (21L, array([ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (22L, array([ 2.,  1.,  0.,  5.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  3.,  0.,  0.,  0.,  0.,  2.,
        0.,  1.,  0.,  0.])), (23L, array([ 2.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,
        0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  3.,  0.,  1.])), (24L, array([ 1.,  0.,  0.,  3.,  1.,  0.,  0.,  0.,  0.,  1.,  1.,  0.,  1.,
        0.,  1.,  0.,  0.,  0.,  0.,  0.,  3.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  1.,  2.])), (25L, array([ 1.,  4.,  0.,  1.,  0.,  2.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.,  1.,  0.,  6.,  0.,  0.,  0.,  0.,  0.,  6.,  0.,
        2.,  0.,  0.,  2.])), (26L, array([ 0.,  1.,  0.,  2.,  1.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  1.,
        1.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (27L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (28L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])), (29L, array([ 2.,  1.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,
        0.,  0.,  1.,  0.])), (30L, array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  1.,  0.,  0.]))])

With 'L's in the code. 在代码中带有“ L”。

What are these 'L's? 这些'L'是什么? How can I remove them so that this code will work the same as with months_dict? 如何删除它们,以便此代码与months_dict一样工作?

L means long integer ( Why do integers in database row tuple have an 'L' suffix? ). L表示长整数( 为什么数据库行元组中的整数后缀为“ L”? )。 Long integers and regular integers were merged in Python 3, so you can get rid of them by switching to Python 3. I do not think you have to worry about them, however. 长整数和正整数在Python 3中已合并,因此您可以通过切换到Python 3摆脱它们。但是,我认为您不必担心它们。

Alright I figured out how to make it work. 好吧,我想出了使它工作的方法。 First I had to make a list of Days. 首先,我必须列出天数。 I took this from my dataframe. 我从数据框中获取了此信息。 Then I used 然后我用

graphdays = [pivot_table[(m)].astype(float).values for m in range(1, len(Days)+1)]
days_dict = OrderedDict(list(zip(Days, graphdays)))

and it worked! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM