How to extract integers from a string separated by spaces in Python 2.7?

后端 未结 4 2091
南笙
南笙 2021-01-05 16:25

I want to extract integers from a string in which integers are separated by blank spaces i.e \' \'. How could I do that ??

Input

I=\'1 15 163 132\'
<         


        
相关标签:
4条回答
  • 2021-01-05 16:45

    Try this code:

    myIntegers = [int(x) for x in I.split()]    
    

    EXPLANATION:


    Where s is the string you want to split up, and a is the string you want to use as the delimeter. Then:

    s.Split(a)
    

    Splits the string s, at those points where a occurs, and returns a list of sub-strings that have been split up.

    If no argument is provided, eg: s.Split() then it defaults to using whitespaces (such as spaces, tabs, newlines) as the delimeter.

    Concretely, In your case:

    I = '1 15 163 132'
    I = I.split() 
    print(I)
    

    ["1", "15", "163", "132"]

    It creates a list of strings, separating at those points where there is a space in your particular example.

    Here is the official python documentation on the string split() method.


    Now we use what is known as List Comprehensions to convert every element in a list into an integer.

    myNewList = [operation for x in myOtherList]
    

    Here is a breakdown of what it is doing:

    • Assuming that myOtherList is a list, with some number of elements,
    • then we will temporarily store one element at a time as x
    • and we will perform some operation for each element in myOtherList
    • assuming that this operation we perform has some return value,
      • then the returned value will be stored as an element in a new list that we are creating
    • The end result is that we will populate a new list myNewList, that is the exact same length as myOtherList

    Concretely, In your case:

    myIntegers = [int(x) for x in I.split()]    
    

    Performs the following:

    • We saw that I.split() returns ["1", "15", "163", "132"]
    • for each of these string elements, simply convert them to an integer
    • and store that integer as an element in a new list.

    See the official python documentation on List Comprehensions for more information.

    Hope this helps you.

    0 讨论(0)
  • 2021-01-05 16:47

    You could simply do like this,

    >>> import re
    >>> I='bar 1 15 foo 163 132 foo bar'
    >>> [int(i) for i in I.split() if re.match(r'^\d+$', i)]
    [1, 15, 163, 132]
    

    Without regex:

    >>> I='bar 1 15 foo 163 132 foo bar'
    >>> [int(i) for i in I.split() if i.isdigit()]
    [1, 15, 163, 132]
    

    i.isdigit() returns true only for the strings which contain only digits.

    0 讨论(0)
  • 2021-01-05 16:57

    Try the following:-

    I='1 15 163 132'
    map(int, a.split(' '))
    
    0 讨论(0)
  • 2021-01-05 17:09
    import re
    x="1 15 163 132"
    print re.findall(r"[+-]?\d+(?:\.\d+)?",x)
    

    If you have only positive integers you can simply do

    print re.findall(r"\d+",x)
    
    [1, 15, 163, 132]
    
    0 讨论(0)
提交回复
热议问题