-1

Suppose I want to initialize 6 lists of 21 numbers each in a dynamic way, how do I do it?

Like,

temporal_id1 = [0, 1, 2, ...., 20]
temporal_id2 = [21, 22, 23, ...., 42]
.    . 
.    .
.    . 
temporal_id6 = [105, 106, ...., 125]

What I am doing is a static way. Something like this:

temporal_id1 = []
for i in range(0,21):
    temporal_id1.append(i)
temporal_id2 = []
for i in range(21, 42):
    temporal_id2.append(i)
temporal_id3 = []
for i in range(42, 63):
    temporal_id3.append(i)
temporal_id4 = []
for i in range(63, 84):
    temporal_id4.append(i)
temporal_id5 = []
for i in range(84, 105):
    temporal_id5.append(i)
temporal_id6 = []
for i in range(105, 126):
    temporal_id6.append(i)

But, this is very naive. And I'm dealing with big data where I have to initialize 107 arrays with 4117 items in each of them.

What I figured is something like this:

for i in range (0, 6):
    initialize list temporal_i with values (21 * i , 21 * (i+1)) ------ (?)

I just want to know what statement I should write in the (?) line.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Samarth Shah
  • 878
  • 8
  • 14
  • possible duplicate of [How do I do variable variables in Python?](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python) – jonrsharpe Jan 19 '15 at 11:06
  • @PadraicCunningham will a dictionary be able to hold up so much data? Like 106 keys and EACH key has 4117 numbers in it? Can you tell me how can I do it with lists? Since my entire code later on depends on them. I can't rewrite it now. I just subsampled the data but now when I realized I have to do it with Big data Im screwed. Yes, they are just incremental numbers: temp_id1 will have 0 to 4116 temp_id2 will have 4117 to 8233.. something like that.. – Samarth Shah Jan 19 '15 at 11:14
  • does each list hold 4117 values? – Padraic Cunningham Jan 19 '15 at 11:15
  • @jonrsharpe I don't get it...How am I using variable variables here? I am just making distinct lists with distinct values each – Samarth Shah Jan 19 '15 at 11:18
  • @SamarthShah I mean you should replace the pointless `temporal_idN`s with a single list or dictionary of lists. `106 * 4117` is really not that much. – jonrsharpe Jan 19 '15 at 11:21
  • Why do you _want_ distinct lists? A list of lists or a dict of lists is _much_ easier to work with. Actually, if the values in each list are fixed, there may be more efficient ways to write your code than to build all these lists. – PM 2Ring Jan 19 '15 at 11:21
  • @jonrsharpe Ahh..alrighty, the subsequent code was a lot dependent on those lists. Anyways, now that you have suggested me this, can you please tell me how do I initialize a dictionary of such kind? – Samarth Shah Jan 19 '15 at 11:23
  • @PM2Ring Well, I am working with a Facebook dataset where there are 4117 nodes and time span of 105 days. At day 0 each node will have ids 0 to 4116.. On day 1, each node will have ids 4117 to something... Get it? So that's why the first thing that came to my mind was lists. But , what's the most optimal way that comes to your mind? Please tell. I am not a Python developer but had no choice. – Samarth Shah Jan 19 '15 at 11:25
  • @SamarthShah this isn't a code-writing service. That being said, zoosuck has already answered showing how to do this. – jonrsharpe Jan 19 '15 at 11:28
  • Yep,I don't know what's temporal_id, his question is simple but not about temporal_id, why did not say that first... – lqhcpsgbl Jan 19 '15 at 11:30
  • @jonrsharpe Sure it isn't buddy. But I have seen SEVERAL posts where there are similar questions like - "How do I initialize this and that" and it has been answered all the time! My question was just that "How do I initialize something dynamically"? I'm not asking you to write the code for me. So please chill. I'm just asking you to help me with one line! – Samarth Shah Jan 19 '15 at 11:31
  • @zoosuck I am working with a Facebook dataset where there are 4117 nodes and time span of 105 days. At day 0 each node will have newly assigned temporal_ids 0 to 4116.. On day 1, each node will have temporal_ids 4117 to something... So that's why the first thing that came to my mind was lists... I have mentioned temporal_ids in the question itself. I just wanted 106 temporal_id lists - each containing 4117 number, is all! And I wanted my code to be used for other dataset not just the one Im working at! – Samarth Shah Jan 19 '15 at 11:32

1 Answers1

0

Try this way use list comprehension:

[range(21* i, 21 * (i+1)) for i in range(0,6)]

You just need to put the range(21* i, 21 * (i+1)) before the for keyword will works.

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
  • ``list(range(...))`` in Python 3.* – fjarri Jan 19 '15 at 11:07
  • I use Python2.7... PO didn't emphasize version. – lqhcpsgbl Jan 19 '15 at 11:08
  • I'm sorry @zoosuck , can you be more elaborative? But how do I initialize the list temporal_id i? And I use 2.7 only. – Samarth Shah Jan 19 '15 at 11:10
  • @SamarthShah: `a = [range(21* i, 21 * (i+1)) for i in range(0,6)]` creates a list of lists, with each list containing 21 numbers. Thus `a[0]` is a list of the numbers from 0 to 20, `a[1]` is a list of the numbers from 21 to 41, etc. – PM 2Ring Jan 19 '15 at 11:38
  • @PM2Ring Very helpful :) Thank you so much. And thanks zoosuck for the answer. – Samarth Shah Jan 19 '15 at 11:41
  • @SamarthShah: Or you can make a a dict using `a = dict([(i, range(21* i, 21 * (i+1))) for i in range(0,6)])`. Once again, a[0] is a list of the numbers from 0 to 20, etc. You can test these things in the Python interpreter... – PM 2Ring Jan 19 '15 at 11:41
  • @PM2Ring I just did buddy :) And yes got it. !! Thanks again. – Samarth Shah Jan 19 '15 at 11:42
  • See [the Python docs](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) for info on dictionaries in Python. Also, see https://docs.python.org/2/library/stdtypes.html#mapping-types-dict – PM 2Ring Jan 19 '15 at 11:44