0

i'm trying to write some program that take lines that represent matrix from user input and store it in list of lists. If the lists are not the same length, we add zeros to all the lists that are not in the same length of the longest one. after that we store all the lists in dictionary.

I just want to know if I can do it in a more efficient way

I would like to improve the second loop and instead of a nested loop do it in one loop, is that possible?

For example: the input is:

1,2,4,6
3,4
2,4,1
end

the output should be :

{1:[1,2,4,6], 2:[3,4,0,0],3:[2,4,1,0]}

my code is:

lst = []
sub_list = []

while sub_list != 'end':
    sub_list = input()
    if sub_list !='end':
        lst.append(list(sub_list.split(',')))

lst = [list(map(int, sublist)) for sublist in lst]

l = len(max(lst,key=len))

for i in lst:
    while len(i) != l:
        i.append(0)

res = {idx + 1: lst[idx] for idx in range(len(lst))}

print(str(res))
  • 1
    Can you give an example of the data input and output you're expecting? – daveydave Jul 26 '21 at 09:57
  • For example: the input is: 1,2,4,6 3,4 2,4,1 the output should be : {1:[1,2,4,6], 2:[3,4,0,0],3:[2,4,1,0]} – Amit Ben-David Jul 26 '21 at 10:00
  • This question is hard to answer without an explanation what the algorithm is supposed to do. – timgeb Jul 26 '21 at 10:13
  • 1
    The algorithm just takes user input and store it in list of lists, if the lists are not the same length, we add zeros to all the lists that not in the same length of the longest one. after that we store all the lists in dictionary. – Amit Ben-David Jul 26 '21 at 10:20
  • 1
    It is better to use https://codereview.stackexchange.com/ as this stack exchange is specifically for optimizing and improving code. Stack overflow is more used to fix programming related *issues* not optimization. – James Barnett Jul 26 '21 at 11:41

2 Answers2

1

You can pad each sublist without an explicit loop.

sublist += [0]*(max_length - len(sublist))
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • hey, why if I write: sublist = sublist + [0]*(max_length - len(sublist)) instead of sublist += [0]*(max_length - len(sublist)) it does not work? – Amit Ben-David Jul 26 '21 at 11:07
  • 1
    @user2733981 I can only guess but probably due to [this](https://stackoverflow.com/questions/52747784/is-i-i-n-truly-the-same-as-i-n/52747886). – timgeb Jul 26 '21 at 11:55
0

I rewrote the whole program by myself. the "myList" is the main list and it depends on you how you want to fill it.

myList = [[1, 2, 3], [3, 2], [2, 4], [1, 3, 2, 4]]
max_length = 5
for j in range(len(myList)):
    for i in range(max_length-len(myList[j])):
        myList[j] += [0]

And to print each and every list out:

for i in range(len(myList)):
    print(myList[i])
Ariocodes
  • 13
  • 5
  • Magic numbers such as "3" in your code are poor practice, as it is unclear to a reader what the 3 represents. – blackbrandt Jul 26 '21 at 11:59
  • @blackbrandt what do you mean? – Ariocodes Jul 26 '21 at 12:29
  • My comment was referring to the code before it was edited. Previously, when you used `for j in range(0,3)`, it is unclear why the code used the number 3 specifically. However, you have switched it to code that uses `len(myList)` instead. This is much better. As a side note, `range(0,3)` and `range(3)` are the same thing, so the 0 is redundant. – blackbrandt Jul 26 '21 at 12:39
  • @blackbrandt yeah I realized that. I admit I hurried a bit in answering the question and the coded needed some edits to be more, "pythonic", I better say? thanks a lot for the help by the way I appreciate it! – Ariocodes Jul 26 '21 at 12:53