0

I am new to Python and want to resolve WITHOUT NUMPY this case: A script/function that take matrix[x][y] and generate/return x lists l1,l2,...lx each of them with y elements. For example:

matrix=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]] 

Expected result:

l1=[1,2,3] l2=[4,5,6] l3=[7,8,9] l4=[10,11,12]    
  • 2
    If your matrix is a list of lists as you signalized, you can access the i-th row by matrix[i]. Did you try using that? – Banana Dec 06 '19 at 10:51
  • 1
    Possible duplicate: [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – martineau Dec 06 '19 at 11:00
  • Since a variable number of variables can be created, how are you going to write code that references only the ones created? – martineau Dec 06 '19 at 11:02
  • You have already achieved what you are trying to create: you have `x` lists, each of them with `y` elements, and they are already packed neatly inside a list. You can access each one of them with indexing. Why complicate with separated variables? – Tomerikoo Dec 06 '19 at 11:07
  • @Tomerikoo that was the assignment....;-) – user3881615 Dec 06 '19 at 13:09
  • Sorry to say then, but that shows something about the person who gave this assignment. It is not very practical to base data access upon variable names. How can you know whether there are 3, 5, or 10 lists to look for? This is what data-structures are for ;-) – Tomerikoo Dec 06 '19 at 20:01
  • Even me, as novice, have to agree you are right.... – user3881615 Dec 07 '19 at 04:53

2 Answers2

1

I suggest to use a dictionary, where keys are row indeces and values are rows:

rows={}
for i,r in enumerate(matrix):
    rows[i] = r  

rows
{0: [1, 2, 3], 
 1: [4, 5, 6], 
 2: [7, 8, 9], 
 3: [10, 11, 12]}

In this way you can access the i-th row by rows[i] (remark index starts from 0)

To generate your expected output:

for i,r in enumerate(matrix):
    globals()[f'l{i+1}'] = r
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • Is it possible to generate separate lists as I asked? If yes how can than be accessed if their name would be in form l1, l2, l3,....ln? – user3881615 Dec 06 '19 at 11:48
  • Edited my answer, in this way you create variables dynamically in the for loop – FBruzzesi Dec 06 '19 at 11:55
  • 1
    I hope you realize that your dictionary achieves nothing over using the original matrix except creating a memory overhead. A dict with index keys is basically a list, which is how the data is already input – Tomerikoo Dec 06 '19 at 20:06
  • 1
    The OP asked to access each element by a specific name, a dictionary gives you this freedom, I am aware that it is creating memory overhead. – FBruzzesi Dec 06 '19 at 22:30
0

You can use locals() or globals(), they will help you to create variables so try this:

for idx, value in enumerate(matrix):
    locals()['l%d'%(idx+1)] = value
Minh Phan
  • 1
  • 3
  • 1
    THe [documentation](https://docs.python.org/3/library/functions.html#locals) for `locals()` clearly states that the returned dictionary should not be modified. – martineau Dec 06 '19 at 10:58
  • Same comment as for @FBruzzesi – user3881615 Dec 06 '19 at 11:50
  • Sure! It will help you to generate variables as you asked but you can't modify them. Anyway this way satisfies your request I think so. Just try `print(l1)` – Minh Phan Dec 06 '19 at 13:33
  • 1-does not understand what's the role of modulo (%) operator in `['l%d'%(idx+1)]` 2-how do I call all variables(l1,l2,...lx) in a for loop ? I tried `for x in range(len(matrix)) print(lx)` but didn't work – user3881615 Dec 09 '19 at 08:37
  • 1. It just help you name your variable. `%d` helps you pass digit param to your string. 2. You have already assigned a matrix item to your variable names so if you want to print them out. try this `for idx in range(len(matrix)) print(locals()['l%d'%(idx+1)])` – Minh Phan Dec 09 '19 at 09:00
  • Got it! Thank you! – user3881615 Dec 09 '19 at 09:31