Home>

There is a linen= "√ (9) -√ (4) +5", and I need to add to the list the numbers that are in parentheses (9,4). Everything under the while loop goes on a different level.

In this code, on the first pass through the loop, everything works as it should, but on the second, everything breaks down, and the place4output-√ (4

n= "√ (9) -√ (4)"
i= 0
while i <
 2:
    z= n [n.find ("(") + 1:]
    final_sr= z.split (")") [i]
    print (final_sr)
    i += 1
  • Answer # 1

    as an option by the methodisdigit ()check if a character is a digit. Well, then check the characters adjacent to it, and if it is a bracket or another digit, add it to the substring, which is then added to the list.

  • Answer # 2

    as an option by the methodisdigit ()check if a character is a digit. Well, then check the characters adjacent to it, and if it is a bracket or another digit, add it to the substring, which is then added to the list.

  • Answer # 3

    you can try this

    import re
    n= "√ (9) -√ (14) +5"
    res= list (map (int, re.findall (r '\ ((\ d +) \)', n)))
    

    r '\ ((\ d +) \)'means that the string will search for substrings that are described by the pattern(numbers)

    \ dmeans that you are looking for a digit (one)

    \ d +means that any number of digits will be searched

    \ (\)means that it will be searched inside parentheses (since parentheses in templates serve a service role, then parentheses as symbols must be escaped

    ()means that we are looking for what is in parentheses (in our case\ d +, i.e. digits), if you do not specify parentheses in the pattern, then something that matches the pattern will be searched, i.e. in our case instead ofnineandfourteenwill be found(nine)and(14)

    well, trash option:

    res= []
    for obj in n.split (')'):
        data= obj.split ('(')
        if len (data) >
     1:
            res.append (int (data [-1]))
    

    and the one-line trash option:

    res= [int (obj.split ('(') [-1]) for obj in n.split (')') if len (obj.split ('(')) >
     1]
    

    Where did you learn all this?

    Дмитрий2021-11-17 20:38:55

    ordinary self-taught :)

    Zhihar2021-11-17 20:41:09

    @Zhihar Go ahead and paint the template re. You need to explain to the people. Must "learn" Regex.

    A_Vaclav2021-11-17 20:56:17

    And if there are floats? Let's point to the pattern. @Zhihar

    A_Vaclav2021-11-17 21:03:38

    @A_Vaclav, here is the pattern for integers and real numbers in parentheses: re.findall (r "\ ((-? \ D * (?: \. \ D *)?) \)", Text);)

    MaxU2021-11-17 21:21:48
  • Answer # 4

    you can try this

    import re
    n= "√ (9) -√ (14) +5"
    res= list (map (int, re.findall (r '\ ((\ d +) \)', n)))
    

    r '\ ((\ d +) \)'means that the string will search for substrings that are described by the pattern(numbers)

    \ dmeans that you are looking for a digit (one)

    \ d +means that any number of digits will be searched

    \ (\)means that it will be searched inside parentheses (since parentheses in templates serve a service role, then parentheses as symbols must be escaped

    ()means that we are looking for what is in parentheses (in our case\ d +, i.e. digits), if you do not specify parentheses in the pattern, then something that matches the pattern will be searched, i.e. in our case instead ofnineandfourteenwill be found(nine)and(14)

    well, trash option:

    res= []
    for obj in n.split (')'):
        data= obj.split ('(')
        if len (data) >
     1:
            res.append (int (data [-1]))
    

    and the one-line trash option:

    res= [int (obj.split ('(') [-1]) for obj in n.split (')') if len (obj.split ('(')) >
     1]
    

    Where did you learn all this?

    Дмитрий2021-11-17 20:38:55

    ordinary self-taught :)

    Zhihar2021-11-17 20:41:09

    @Zhihar Go ahead and paint the template re. You need to explain to the people. Must "learn" Regex.

    A_Vaclav2021-11-17 20:56:17

    And if there are floats? Let's point to the pattern. @Zhihar

    A_Vaclav2021-11-17 21:03:38

    @A_Vaclav, here is the pattern for integers and real numbers in parentheses: re.findall (r "\ ((-? \ D * (?: \. \ D *)?) \)", Text);)

    MaxU2021-11-17 21:21:48