extracting numbers from list of strings with python

前端 未结 2 1534
既然无缘
既然无缘 2021-01-29 14:45

I have a list of strings that I am trying to parse for data that is meaningful to me. I need an ID number that is contained within the string. Sometimes it might be two or even

2条回答
  •  -上瘾入骨i
    2021-01-29 15:06

    Based on @alecxe solution you can also do it without any imports.

    If your id numbers are always after id and have a fixed (7) number of digits I would probably just use .split('id ') to separate it and get the 7 digits from the second block onwards.

    You can put them together in the desired format by using '; '.join()

    Putting everything together:

    pattern = ['; '.join([value[:7] for value in valueList.split('id ')[1:]]) for valueList in lst1]
    

    Which prints out:

    ['3999595; 3999999', '3998895; 5555456; 3998899']
    

提交回复
热议问题