2

I would like to initialize a multidimensional list capable of storing an object for every minute since year 1950-2050.

Something like:

minute = [None]*61
hour = [minute]*25
day = [hour]
month = [day]
data = [month]*100

So you can do:

data[89][1][29][23][55] = 'It was a good minute the one from January 29th in 1989 when it was 23:55'

How would be such a multidimensional list be initialized in Python? Would it be an actual different object than the one created with the above code?

Initially the multidimensional list would contain objects None.

Python 2.7

Following answer I tried:

# Data structure
minute = 60
hour = 24
day = 31
month = 12
year = 100
test = [[[[[None for _minute in range(minute)] for _hour in range(hour)] for _day in range(day)] for _month in range(month)] for _year in range(year)]

But it seems too much for multidimensional lists, as I get "Killed" when trying to execute this.

8
  • 1
    Why do you need to build such a massive dictionary? Is the objective to return a string given the integer inputs, in which case would a function not work? Commented Mar 11, 2018 at 3:57
  • It needs to store data for any minute from 1950 to 2050. I have edited the question so it uses word "store" instead of "return". Commented Mar 11, 2018 at 3:59
  • 1
    Yes, but why? What is your end use case? I'm guessing it's not going to be printed out, this will cost a lot of ink! Commented Mar 11, 2018 at 4:01
  • That's about 52 million minutes. Are you actually going to store an entry for every minute? Why do you need this data structure? Commented Mar 11, 2018 at 4:02
  • Fast in-memory store and easy retrieval of the information as show in the example: data[89][1][29][23][55] Commented Mar 11, 2018 at 4:02

2 Answers 2

2

I also don't recommend this, but you could use a numpy.chararray for this:

import numpy as np
arr = np.chararray((100, 12, 31, 24, 60, 60), itemsize=100)

arr[52, 7, 12, 12, 44, 54] = 'year 1950+52, 7th month, 12th day, 12th hour, 44th minute, 54th second'

I'm not exactly sure what your desired structure is, but the string I inserted into the array should explain the structure I proposed, and you can change it however you need. Note that itemsize limits how many characters you can put in at any index.

Again, with a caveat that this is not necessarily the most efficient thing in the world to do, but if you wish to store lists of ints and/or floats in that array (as per your comment), one way to do it would be to convert that list to strings, and then when retrieving it, re-transform back to a list:

data_to_insert = [1,2,3,4.5]
# store as string
arr[52, 7, 12, 12, 44, 54] = ','.join(map(str, data_to_insert))
# retrieve
arr[52, 7, 12, 12, 44, 54].decode('utf-8').split(',')

This should be pretty fast

Sign up to request clarification or add additional context in comments.

3 Comments

The object to be stored is another list with 4 floats and 1 integer. I never used numpy so I am not sure if it can handle it better than a multidimensional list. It must be fast to retrieve any given minute.
There could be also None instead of the described list.
I am exploring this which might fit better stackoverflow.com/questions/30740490/… Not sure if dtype will allow to fullfil this particular case
2

Though i won't recommend it, A multi dimentional empty list can be created by using list comprehension:

> >>> a = 4 #Width of elements
> >>> b = 6 #Width of main list container
>>>>> c = 4
>>>>> d = 3
> >>> l = [[[[0 for k in range(d) ] for z in range(c)] for x in range(a)] for y in range(b)]
> >>> [[[[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], [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]], [[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], [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]], [[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], [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]], [[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], [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]]], [[[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], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]]

Keep replacing 0 with list comprehensions to add more dimentions.

7 Comments

It would eat your memory and since linked list takes linear time, it'll take too much computation as well. Though it is what you asked for!
I thought it would be extremely fast, as you are indexing everything. Anyway python can not generate such big multidimensional list -see my edited question-
Did you get any errors while generating that list? If yes, try the other answer's solution. Use numpy arrays instead of normal lists. They are fast and well designed for this kind of tasks.
Can a numpy array store lists of 4 floats and 1 float? Objects to be stored have this form: [67.4, 65.3, 68.8, 69.5, 10000] or None object
Easily, It's used in machine learning and they use like crazy complex arrays.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.