简体   繁体   English

如何从列表中的元素中删除括号(Python)

[英]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. 例如,对于我的列表中的第一个元素我有103.92246(11),我想从中剥离它给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)

You can use str.translate , passing whatever chars you want to remove: 您可以使用str.translate ,传递要删除的任何字符:

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 ( : 如果你想删除parens中的nums,请拆分第一个(

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 : 或者使用str.rfind

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

output as above. 输出如上。

A little change in your regex: 你的正则表达式有点变化:

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

you can do this: 你可以这样做:

result = []
for num in mylist:
    a = num[0].index('(') #find the position of (
    result.append(num[0][:a])

a oneliner version oneliner版本

[x[0][:x[0].index('(')] for x in mylist]
import re    

my_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']]    

result = [re.sub(r'([0-9\.])\(.*?\n', r'\1', x[0]) for x in my_list]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM