Finding part of string in list of strings

GCM = ([519,520,521,522,533],[534,525],[526,527,530,531], [4404])

slice = int(str(df["CGM"][row_count])[:3])

I am looking through a row in a csv file and taking out the number I want. i want the number that starts with the number I have in GCM. since they represent info I want in other columns. this has working fine with the slice function because all the number i wanted started with 3 digits. now that i need to look for any number that starts with 4404 and later on going to probably need to look for 57052 the slice function no longer work.

is there a way I can, instead of slicing and comparing to list, can take 5 digit number and see if part of it is in list. preferably look for it starting 3 or more same digits. the real point of that part of code is finding out which list in GCM list the number is. it need to be able to have the number 44042 and know that the part of it a care about is in GCM[3], but on the other side do not want it to say that 32519 is in DCM[0] since I only care about number that start with 519 not ends with it.

enter image description here

ps. I am norwegian and have been learning programming by myself. been some long nights. so something here can be lost in translation.

1 answer

  • answered 2021-04-14 23:09 Barmar

    You can use str.startswith() to test if a substring is the beginning of another string, then you don't need to slice a particular length.

    And you can use any() to test if any of the strings in a GCM list matches this way.

    GCM = (['519','520','521','522','533'],['534','525'],['526','527','530','531'], ['4404'])
    num = '44042'
    for i, l in enumerate(GCM):
        if any(num.startswith(code) for code in l)
            print(f'Found {num} in GCM[{i}]')
            break
    else:
        print(f'{num} not found in GCM')