Skip to content Skip to sidebar Skip to footer

Need To Remove Items From Both A List And A Dictionary Of Tuple Value Pairs At Same Time

This is very related to a previous question but I realised that my objective is much more complicated: I have a sentence: 'Forbes Asia 200 Best Under 500 Billion 2011' I have token

Solution 1:

Not a very elegant, but working solution:

oldTokens = [u'Forbes', u'Asia', u'200', u'Best', u'Under', u'500', u'Billion', u'2011']

numberTokenIDs =  {(7,): 2011.0, (2,): 200.0, (5,6): 500000000000.00}
locationTokenIDs = {(0, 1): u'Forbes Asia'}

newTokens = []
newnumberTokenIDs = {}
newlocationTokenIDs = {}

new_ind = 0
skip = False

for ind in range(len(oldTokens)):
    if skip:
        skip=False
        continue

    for loc_ind in locationTokenIDs.keys():
        if ind in loc_ind:
            newTokens.append(oldTokens[ind+1])
            newlocationTokenIDs[(new_ind,)] = locationTokenIDs[loc_ind]
            new_ind += 1
            if len(loc_ind) > 1: # Skip next position if there are 2 elements in a tuple
                skip = True
            break
    else:
        for num_ind in numberTokenIDs.keys():
            if ind in num_ind:
                newTokens.append(oldTokens[ind])
                newnumberTokenIDs[(new_ind,)] = numberTokenIDs[num_ind]
                new_ind += 1
                if len(num_ind) > 1:
                    skip = True
                break
        else:
            newTokens.append(oldTokens[ind])
            new_ind += 1

newTokens
Out[37]: [u'Asia', u'200', u'Best', u'Under', u'500', u'2011']

newnumberTokenIDs
Out[38]: {(1,): 200.0, (4,): 500000000000.0, (5,): 2011.0}

newlocationTokenIDs
Out[39]: {(0,): u'Forbes Asia'}

Post a Comment for "Need To Remove Items From Both A List And A Dictionary Of Tuple Value Pairs At Same Time"