0

(Update) I need to find stationary distribution of a Markov Chain with 4.5 million states. It means I need to solve a linear system with 4.5 million equations. Each state is a vector of size 6. I am trying to store each state in a list. The following in part of my effort in creating all admissible states.

I am trying to loop through a big set of numbers and create a set of vectors. Here is a simplified version of my code:

mylist=[]
for i in range(1,4):
    for j in range(1,4-i):
        for k in range(0,5-i-j):
            Temp=[i,j,k]
            mylist.extend(Temp)
            print(mylist)
            mylist=[]
            Temp=[]

which will give me:

[1, 1, 0]
[1, 1, 1]
[1, 1, 2]
[1, 2, 0]
[1, 2, 1]
[2, 1, 0]
[2, 1, 1]

my question is: is there a neater, nicer, more efficient way of doing this in Python?

Thank you

7
  • I'm voting to close this question as off-topic because it belongs on codereview.stackexchange.com Commented Jun 22, 2015 at 2:40
  • 1
    @CoreyOgburn if it were actual code from a real project, perhaps. As it stands it's hypothetical/example code, which is explicitly off-topic on Code Review. Also, being a better fit for CR isn't a close reason; to help migrate a question you need to custom-flag for moderator attention. Commented Jun 22, 2015 at 2:43
  • 1
    Do it once then serialize it so the next time you need it you can just read it in. Commented Jun 22, 2015 at 4:16
  • @wwii: thank you; can you please explain a bit what is serialization? I am dealing with huge amount of data and can definitely use any hint for saving time ( and specially memory). Commented Jun 22, 2015 at 4:26
  • 1
    You may need to expand on your question and describe what type of efficiency you are trying to achieve. I took it to mean time so my suggestion is to keep your code as is, create the data once (while having a cup of tea) and write it to disc to be read back in when needed (serialization). It could be easily written as a text file or pickled. You should also define huge amount of data in the question. Commented Jun 22, 2015 at 4:38

3 Answers 3

3

If you are looking for a one line code that would create the same vector , you can use list comprehension in python.

Example -

myList = [[i,j,k] for i in range(1,4) for j in range(1,4-i) for k in range(0,5-i-j)]
myList
>> [[1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [2, 1, 0], [2, 1, 1]]

Though I do not think this is in anyway neater or more efficient.

Though after some testing using timeit , we can see that list comprehension may be a little bit faster -

In [1]: def foo1():
   ...:     l = []
   ...:     for i in range(100):
   ...:                 for j in range(100):
   ...:                         l.append([i,j])
   ...:     return l

In [3]: def foo2():
   ...:     return [[i,j] for i in range(100) for j in range(100)]
   ...: 

In [4]: %timeit foo1()
100 loops, best of 3: 3.08 ms per loop

In [5]: %timeit foo2()
100 loops, best of 3: 2.16 ms per loop

In [6]: %timeit foo2()
100 loops, best of 3: 2.18 ms per loop

In [7]: %timeit foo1()
100 loops, best of 3: 3.11 ms per loop
Sign up to request clarification or add additional context in comments.

Comments

1

I completely don't get what you are trying to archive with your lists. You could get the same output with this:

for i in range(1,4):
    for j in range(1,4-i):
        for k in range(0,5-i-j):
            print([i,j,k])

Comments

0
from itertools import product
mylist =[[i,j,k] for i,j,k in product(range(1,4),range(1,4),range(2))]

Comments

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.