mardi 3 mars 2020

iterate through a lisit which would have range as an element and expand it

I'm a newbie to python I want to iterate through a list which might have ranges in it and expand it.

def ExpandList(c):
    new_list = []
    for i in c:
        print(i)
        if '-' in i:
            low,high=i.split('-')[0],i.split('-')[-1]
            if low !=high:
                for j in range(low,high):
                    new_list.append(j)
            else:
                new_list.append(low)
        else:
            new_list.append(i)

return new_list

c = [1001-1002,1005-1009,1010,1010-1025]

with the above input I want to expand the list. when I iterate through the given list, it does not take 1001-1002 as the single element unless i mention it as a string '1001-1002'. I cannot force user to give that as string.. how do i evaluate this and acheive what i intend to. kindly advise

Aucun commentaire:

Enregistrer un commentaire