【问题标题】:Need to remove items from both a list and a dictionary of tuple value pairs at same time需要同时从列表和元组值对字典中删除项目
【发布时间】:2016-07-21 16:11:05
【问题描述】:

这与previous question 非常相关,但我意识到我的目标要复杂得多:

我有一句话:"Forbes Asia 200 Best Under 500 Billion 2011"

我有类似的标记:

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'}

token ID对应有位置或编号的token的索引,目的是获得一组新的token,如:

newTokens = [u'Asia', u'200', u'Best', u'Under', u'500', u'2011']

使用新的数字和位置 tokenID 可能像(以避免索引越界异常):

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

基本上我想通过新的减少标记集,并最终能够创建一个新句子,称为:

"LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT"

通过检查新的令牌集并将正确的令牌ID 替换为“LOCATION_SLOT”或“NUMBER_SLOT”。如果我使用当前的一组数字和位置令牌 ID 执行此操作,我会得到:

"LOCATION_SLOT LOCATION_SLOT NUMBER_SLOT Best Under NUMBER_SLOT NUMBER_SLOT NUMBER_SLOT".

我该怎么做?

另一个例子是:

Location token IDs are:  (0, 1)
Number token IDs are:  (3, 4)
Old sampleTokens [u'United', u'Kingdom', u'USD', u'1.240', u'billion']

我想同时删除令牌并更改位置和数字令牌 ID 以便能够替换以下句子:

sampleTokens[numberTokenID] = "NUMBER_SLOT"
sampleTokens[locationTokenID] = "LOCATION_SLOT"

这样被替换的令牌是[u'LOCATION_SLOT', u'USD', u'NUMBER_SLOT']

【问题讨论】:

    标签: python dictionary tuples


    【解决方案1】:

    不是一个非常优雅但有效的解决方案:

    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'}
    

    【讨论】:

    • 嗨 Vadim,我无法将其用于创建类似 ForbesAsia 200 Best Under 500Billion 2011 的句子,因此我将位置和值连接起来,但是如果没有,我也不希望这样做需要例如如果位置令牌 ID 和数字令牌 ID 对的长度不超过 1。
    • 更简单的方法是添加一个索引列表,以便程序知道 ID 将在哪些位置连接。然后只需添加额外的检查:如果索引在列表中 - 进行连接。另一种解决方案是将现有架构从[...] (2,): 200.0, (5,6): 500000000000.00 修改为[...] (2,): (200.0, 0), (5,6): (500000000000.00, 1)
    • 明白。我应该就此提出一个单独的问题,还是您可以将其作为您答案的第二个版本?
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多