How to identify numeric digits in numbers within a list?

Asked

Viewed 98 times

-2

I need some help. I am stuck in an exercise where beyond several guidelines, asks me to remove all numbers within a list that end with 7 (17, 27, 37, etc)

How would I do that? (python)

  • what you tried to do?

  • Tried using % operator (rest of division)?

  • I tried to use another for to go through each digit of each item, but I couldn’t. It’s one with numbers from 1 to 100. I tried to use the logic to check if the second digit of the number ends in '7' (as if it were the input of a string). But it did not give kk. (I used %to remove multiples of 7, one of the things that exercise asks for)

3 answers

4

You can use list comprehension to solve your problem:

lista = list(range(100))
lista = [x for x in lista if x % 10 != 7]

In the second line of code we are saying that we want all the elements of the list since the element divided by 10 has rest other than 7.

2


One possibility is to use the built-in function filter(função , iterável ) creating an iterator from the elements of iterável for which the função filter, passed in first parameter, return true. The iterable can be a sequence, a container that supports iteration, or an iterator.

As a filter pass a lambda expression that nothing else is an anonymous function:

lambda x: x % 10 != 7 

This lambda expression returns the reference to an anonymous function containing a single parameter,x, such that the unity of x is not 7 returns true, otherwise returns false.

Code:
In this case I created a hypothetical list called lista the elements of which are natural numbers from 0 to 99.
Then I applied the filter, described above, so that only a single iteration is made by the elements of lista.

#Cria uma lista com os naturais de 0 até 99 para teste.
lista = list(range(100))

#Filtra os elementos da lista cujo a unidade seja 7
resultado = list(filter(lambda x: x % 10 != 7, lista))

print(resultado)  #imprime o resultado

Test the code on Repl.it

Exit:

[0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24,   
 5, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 
49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71, 
72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 88, 89, 90, 91, 92, 93, 94, 
95, 96, 98, 99]

Related:

Related searches here on the site

1

Try:

for i in range((len(lista)-1),-1,-1):
  if (lista[i] % 10) == 7:
    del(lista[i])
  • It worked perfectly! Thank you! tried before: for i in range(0, Len(list): if(list[i] %10) == 7: del(list[i]) But gave Indexerror: list index out of range. Can you tell me why? (learning python yet)

  • Starting the scan by the top of the list: range(len(lista)) It turns out that as the numbers are deleted the list size decreases. So at some point the range would eventually reach a number above the list of elements, which would cause this error. Doing the opposite and sweeping the list from its end: range((len(lista)-1), 0, -1), the number decreases along with the step of the iterator "-1".

  • 1

    If the first item in the list is 7 or ended in 7 it will not be removed from the list with the above loop. A solution would be to pass -1 like stop the range, would look like this: for i in range(len((lista[i]-1), -1, -1).

  • Thanks, I’ll edit there :)

  • 2

    For small lists it makes no difference, but in grid list operations this strategy is not very performative. Removing items individually can be one costly operation, compare to time complexity between attaching an item at the bottom of the list(Append[1]) and remove any item(Delete Item).

Browser other questions tagged

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