how to create a list of multiples of numbers of 3, from another list of numbers?

Asked

Viewed 573 times

-3

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]

for c in meus_numeros:
    if c%3 == 0:
    print(c, end='')
File "<ipython-input-12-2b3fd4ceddad>", line 3
    print(c, end='')
        ^
IndentationError: expected an indented block
  • 3

    The print is identando at the same level of if, just fix the identation that works: https://ideone.com/T1wuoe - in addition, I am voting to close as "typo", following what was defined here: https://pt.meta.stackoverflow.com/q/7476/112052

4 answers

1

You can use list comprehension, which will return you a new list with multiples of 3:

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]

nova_lista = [numero for numero in meus_numeros if numero % 3 == 0]
print(nova_lista)

0

For you to create a new lista, containing the multiples of 3, contained initially in another list, you can use the following algorithm...

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]

multiplos_tres = list()
for c in meus_numeros:
    if c % 3 == 0:
        multiplos_tres.append(c)
multiplos_tres.sort()
print(f'\033[32mA lista dos múltiplos de "3" é: {multiplos_tres}')

Note that this algorithm scans the lista inicial and checks that each of the elements are multiples of 3. If so, I have entered multiplos_tres.

After having inserted all possible multiples in multiplos_tres, the algorithm sorts the values and then displays them.

Now, if you wish apenas exibir the elements of the original list which are multiples of 3, You can use the following algorithm...

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]

print(f'\033[32mOs múltiplos de "3" são:')
for c in meus_numeros:
    if c % 3 == 0:
        print(c, end='')
        print(', ' if c < meus_numeros[-1] else '.', end='')

In this case, the algorithm scans the original list and, if the element is multiple of 3, displays on the screen.

0

Use the function .append()

.append() is a method that attaches an element to the end of the list. So, if you create a list and use .append(c), it will add the c at the end of the list.

Your code would look like this:

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]
lista = []
for c in meus_numeros: 
    if c%3 == 0: 
        lista.append(c)
  • 1

    Printing a list does not seem to be the wish of the author of the question since he has released his print as print(c, end='')

  • I removed the print

0

Your problem was having the print out of the loop if.

Since the value of variable C is the condition to print or not the line (if the number tested is a number divisible by three) and as this value depends on the comparison of the If, your print must be idented inside the loop:

meus_numeros = [1, 56, 342, 12, 781, 23, 43, 45, 123, 567]

for c in meus_numeros:
    if c%3 == 0:
        print(c, end=' ')

P.S. Seu end='' was out of space, so the numbers were printed "glued". I put a little space there to be intelligible.

  • Thanks, solved my problem.

Browser other questions tagged

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