Search code examples
pythonlistparentheses

How to remove parenthesis from elements in a list (Python)


I'm trying to remove some parenthesis from numbers in my list. Example, I have the following list

[' 103.92246(11)\n'],
[' 104.92394(11)\n'],
[' 105.92797(21)#\n'],
[' 106.93031(43)#\n'],
[' 107.93484(32)#\n'],
[' 108.93763(54)#\n'],
[' 109.94244(54)#\n'],
[' 110.94565(54)#\n'],
[' 111.95083(75)#\n'],
[' 112.95470(86)#\n'],
[' 82.94874(54)#\n'],
[' 83.94009(43)#\n'],
[' 84.93655(30)#\n'],
[' 85.93070(47)\n'],
[' 86.92733(24)\n'],
...]

for example, for the first element in my list I have 103.92246(11), were I want () stripped from it to give 103.92246. Some elements also have # which I want removed too, basically all I want is the float number. How would I go about doing this? I've tried the below code, but that doesn't seem to be working for me.

tolist = []
for num in mylist:
  a = re.sub('()', '', num)
tolist.append(a)

Solution

  • You can use str.translate, passing whatever chars you want to remove:

    l =[[' 103.92246(11)\n'],
    [' 104.92394(11)\n'],
    [' 105.92797(21)#\n'],
    [' 106.93031(43)#\n'],
    [' 107.93484(32)#\n'],
    [' 108.93763(54)#\n'],
    [' 109.94244(54)#\n'],
    [' 110.94565(54)#\n'],
    [' 111.95083(75)#\n'],
    [' 112.95470(86)#\n'],
    [' 82.94874(54)#\n'],
    [' 83.94009(43)#\n'],
    [' 84.93655(30)#\n'],
    [' 85.93070(47)\n'],
    [' 86.92733(24)\n']]
    
    for sub in l:
        sub[:] = [s.translate(None, "()#") for s in sub]
    

    Output:

    [[' 103.9224611\n'], [' 104.9239411\n'], [' 105.9279721\n'], 
    [' 106.9303143\n'], [' 107.9348432\n'], [' 108.9376354\n'],
     [' 109.9424454\n'], [' 110.9456554\n'], [' 111.9508375\n'],
     [' 112.9547086\n'], [' 82.9487454\n'], [' 83.9400943\n'], 
    [' 84.9365530\n'], [' 85.9307047\n'], [' 86.9273324\n']]
    

    If you want them cast to floats:

     sub[:] = map(float,(s.translate(None, "()#") for s in sub))
    

    which will give you:

    [[103.9224611], [104.9239411], [105.9279721], [106.9303143], 
    [107.9348432], [108.9376354], [109.9424454], [110.9456554], 
    [111.9508375], [112.9547086], [82.9487454], [83.9400943], [84.936553], 
     [85.9307047], [86.9273324]]
    

    If you want to remove the nums in the parens, split on the first (:

    for sub in l:
        sub[:] = map(float,(s.rsplit("(",1)[0] for s in sub))
    
    print(l)
    

    Output:

    [[103.92246], [104.92394], [105.92797], [106.93031], [107.93484], 
    [108.93763], [109.94244], [110.94565], [111.95083], [112.9547], 
    [82.94874], [83.94009], [84.93655], [85.9307], [86.92733]]
    

    Or using str.rfind:

    for sub in l:
        sub[:] = map(float,(s[:s.rfind("(")] for s in sub))
    

    output as above.