How to remove all integer values ​​from a list in python

I'm just a beginner in python and I want to know if all integer values ​​can be removed from a list? For example, a document looks like

['1','introduction','to','molecular','8','the','learning','module','5'] 

After deleting, I want the document to look like this:

 ['introduction','to','molecular','the','learning','module'] 
+6
python string
source share
8 answers

To remove all integers, do the following:

 no_integers = [x for x in mylist if not isinstance(x, int)] 

However, your list of examples does not actually contain integers. It contains only strings, some of which consist only of numbers. To filter them, follow these steps:

 no_integers = [x for x in mylist if not (x.isdigit() or x[0] == '-' and x[1:].isdigit())] 

As an alternative:

 is_integer = lambda s: s.isdigit() or (x[0] == '-' and x[1:].isdigit()) no_integers = filter(is_integer, mylist) 
+20
source share

You can also do this:

 def int_filter( someList ): for v in someList: try: int(v) continue # Skip these except ValueError: yield v # Keep these list( int_filter( items )) 

Why? Because int better than trying to write rules or regular expressions to recognize string values ​​that encode an integer.

+11
source share

None of the items in your list is an integer. These are strings containing only numbers. Thus, you can use the isdigit string method to filter these items.

 items = ['1','introduction','to','molecular','8','the','learning','module','5'] new_items = [item for item in items if not item.isdigit()] print new_items 

Documentation link: http://docs.python.org/library/stdtypes.html#str.isdigit

+8
source share

I personally like the filter. I think this can help keep the code readable and conceptually simple if used in a smart way:

 x = ['1','introduction','to','molecular','8','the','learning','module','5'] x = filter(lambda i: not str.isdigit(i), x) 

or

 from itertools import ifilterfalse x = ifilterfalse(str.isdigit, x) 

Note that the second returns an iterator.

+3
source share

You can also use lambdas (and obviously recursion) to achieve this (Python 3 required):

  isNumber = lambda s: False if ( not( s[0].isdigit() ) and s[0]!='+' and s[0]!='-' ) else isNumberBody( s[ 1:] ) isNumberBody = lambda s: True if len( s ) == 0 else ( False if ( not( s[0].isdigit() ) and s[0]!='.' ) else isNumberBody( s[ 1:] ) ) removeNumbers = lambda s: [] if len( s ) == 0 else ( ( [s[0]] + removeNumbers(s[1:]) ) if ( not( isInteger( s[0] ) ) ) else [] + removeNumbers( s[ 1:] ) ) l = removeNumbers(["hello", "-1", "2", "world", "+23.45"]) print( l ) 

The result (displayed with 'l') will be: ['hello', 'world']

+1
source share

Please do not use this method to remove items from the list: (edited after THC4k comment)

 >>> li = ['1','introduction','to','molecular','8','the','learning','module','5'] >>> for item in li: if item.isdigit(): li.remove(item) >>> print li ['introduction', 'to', 'molecular', 'the', 'learning', 'module'] 

This will not work from the moment the list is modified, while iterating over it will trigger the for loop. In addition, item.isdigit () will not work if the item is a string containing a negative integer, as noted by razpeitia.

0
source share

You can use the built-in filter to get a filtered copy of the list.

 >>> the_list = ['1','introduction','to','molecular',-8,'the','learning','module',5L] >>> the_list = filter(lambda s: not str(s).lstrip('-').isdigit(), the_list) >>> the_list ['introduction', 'to', 'molecular', 'the', 'learning', 'module'] 

The above can handle many objects using explicit type conversion. Since almost every Python object can be legally converted to a string, here filter takes a converted str copy for each member of the_list and checks if the string (minus any leading β€œ-” character) is a numeric digit. If so, the member is excluded from the returned copy.

Built-in functions are very useful. Each of them is optimized for the tasks that they are intended for processing, and they will save you from new solutions.

0
source share

To remove all integers from a list

 ls = ['1','introduction','to','molecular','8','the','learning','module','5'] ls_alpha = [i for i in ls if not i.isdigit()] print(ls_alpha) 
0
source share

All Articles