简体   繁体   中英

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.

I have this variable called months_dict which works very well. 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
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.

What are these 'L's? How can I remove them so that this code will work the same as with months_dict?

L means long integer ( Why do integers in database row tuple have an 'L' suffix? ). 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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