List comprehension replacing items that are not float or int

后端 未结 5 562
一个人的身影
一个人的身影 2021-01-17 02:21

I have a 2 item list.

Sample inputs:

[\'19(1,B7)\', \'20(1,B8)\']
[\'16 Hyp\', \'16 Hyp\']
[\'< 3.2\', \'38.3302615548213\']
[\'18.6086945477694\         


        
5条回答
  •  孤街浪徒
    2021-01-17 02:29

    If you want to edit the list in place rather than create a new list, you can iterate over it using enumerate:

    for (list_index, item) in enumerate(values):
        if '> ' in item:
            values[list_index] = ' '.join(item_part for item_part in item.split() if is_number(item_part))
    

    Or however you want to construct your new value - I'm not sure what you want if you have '5 6' in your original list.

    If you want a new list, you can do something similar with a list comprehension.

提交回复
热议问题