Browse lists of different sizes

Asked

Viewed 115 times

0

I am going through two lists and performing an operation, but when the smaller list is over or is over and the operation is not 100% completed. How do I make it so that when the shortest list is over it returns to the first element and so on until the longest list is over?

m = []
x = [1,2,3]
y = [4,5,6,7,8,9]
for e, i in zip(x, y):
   f = e
   g = i
   f = f + g
   m.append(f)
  • a tip for you to be able to solve this is to do the for to the largest and smallest list you make a if checking whether she’s finished or not

5 answers

2

How do I make it so that when the shortest list is over it returns to the first element and so on until the longest list is over?

If you need the elements of x remain cyclical until the entire list y is covered, just use the function itertools.cycle:

from itertools import cycle

x = [1,2,3]
y = [4,5,6,7,8,9]

for a, b in zip(cycle(x), y):
  print(a, b)

This will produce the output:

1 4
2 5
3 6
1 7
2 8
3 9

Note that for elements 7, 8 and 9 of y the values of x were repeated from the beginning.

  • worked perfectly, thank you!

1

Of course, there are other ways to do this, but I tried to maintain the structure of your code. In this case I used the zip_longest package itertools to go through the lists according to the size of the largest list, see that the missing values of the first list are filled with values None

import itertools
​
m = []
x = [1,2,3]
y = [4,5,6,7,8,9]
​
for e, i in itertools.zip_longest(x, y, fillvalue = None):
    if e != None: 
        f = e
        g = i
        f = f + g
    else :
        f = i
    m.append(f)
​

Output:

print(m)
[5, 7, 9, 7, 8, 9]

If you want the smallest list to be repeated you can use the cycle of the same package itertools

import itertools

m = []
x = [1,2,3]
y = [4,5,6,7,8,9]

for e, i in zip(itertools.cycle(x), y):
    f = e
    g = i
    f = f + g
    m.append(f)
print(m)

Output:

[5, 7, 9, 8, 10, 12]

1

Use itertools cycle. Assuming you know that x is the shortest list and using list comprehension in one row only:

from itertools import cycle


x = [1, 2, 3]
y = [4, 5, 6, 7, 8, 9]
m = [a + b for a, b in zip(cycle(x), y)]
print(m)

0

Thus, for each element of the list X, it will traverse the entire list Y before moving to the next element of the list X.

m = []
x = [1,2,3]
y = [4,5,6,7,8,9]
for e in x:
   for i in y:
      f = e
      g = i
      f = f + g
      m.append(f)

0

From what I understand, you’re willing to go through both lists simultaneously and add up the corresponding values of the two list, index by index. For this you should realize that there are three different cases to solve, which are:

  1. Case 1: The two lists have the same size;
  2. Case 2: The first list has smaller size;
  3. Case 3: The first list has larger size;

In order to solve this issue in a very comprehensive way, that is, that it can encompass the three cases mentioned above, we can implement the following algorithm:

from itertools import zip_longest

m = []
x = [1, 2, 3]
y = [4, 5, 6, 7, 8, 9]

if len(x) < len(y):
    for e, i in zip_longest(x, y, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)
elif len(x) == len(y):
    for e, i in zip(x, y):
        h = e + i
        m.append(h)
else:
    for e, i in zip_longest(y, x, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)

print(m)

Note that the block if checks if the list size x is minor that the size of the list y and, if so, these two lists shall be travelled using the zip_longest by completing the missing items on the list x with the value None. Then sum each matching of index-to-index elements by storing them in the list m and then displays the list m.

Already the block Elif checks if the list size x is equal to the size of the list y and, if it is, these two lists will be covered with the help of the zip, adding each match of index-to-index elements, storing them in the list m and then displayed the list m.

And last we have the block Else. This block will be executed if the list size x be it greater that the size of the list y. In this case the two lists are covered using the method zip_longest by completing the missing items on the list y with the value None. Then sum each match of index-to-index elements, store them in the list m and then displays the list m.

Now, if you need to generalize this problem to any two lists, you can use the following algorithm:


from itertools import zip_longest

x = list(map(int, input('Digite os valores da lista "X": ').split()))
y = list(map(int, input('Digite os valores da lista "Y": ').split()))

m = []

if len(x) < len(y):
    for e, i in zip_longest(x, y, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)
elif len(x) == len(y):
    for e, i in zip(x, y):
        h = e + i
        m.append(h)
else:
    for e, i in zip_longest(y, x, fillvalue=None):
        if e is not None:
            f = e
            g = i
            h = f + g
        else:
            h = i
        m.append(h)

print(m)

In this second algorithm, when the following sentence appears Digite os valores da lista "X": , you must type all the elements of the list X, on the same line, separated by a single space and then press enter. And when the next sentence appears Digite os valores da lista "Y": , you must type all the elements of the list Y, on the same line, separated by a single space and then press enter.

From that time the algorithm will perform the rest of the operations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.