如何删除字典中的某些单词

时间:2018-10-21 05:53:39

标签: python-3.x dictionary

我有一本以值作为列表的字典,每个列表都包含字符串。我想1)删除标点符号,但@除外,以及2)删除列表中带有“ @”的项目。但是,我似乎看不到第二部分:

>>> dat = {'2008':['what!','@cool','#fog','@dddong'],'2010':['hey','@cute']}
>>> 
>>> def remove_rounds(data):
...     import json
...     import string
...     ndata = {}
...     punct = string.punctuation
...     rpunct = punct.replace('@',"") # withold @
...     for k,v in data.items():
...         alist = []
...         ndata[k] = []
...         for word in data[k]:
...             alist.append(word.strip(rpunct))
...             ndata[k] = alist
...     sdata = {}
...     for k,v in ndata.items():
...         sdata[k] = []
...         blist = []
...         for word in ndata[k]:
...             if word.startswith('@'):
...                 blist = ndata[k].remove(word) # returns the list
...                 sdata[k] = blist
...     return sdata
... 
>>> remove_rounds(dat)
{'2008': None, '2010': None}

因此,ndata部分工作正常,我能够删除列表中的标点符号,但似乎无法使用相同的逻辑来摆脱以'@'开头的单词。另外,我不明白为什么不能应用相同的逻辑。

1 个答案:

答案 0 :(得分:0)

如果单词以@开头,请避免附加该单词​​:

dat = {'2008':['what!','@cool','#fog','@dddong'],'2010':['hey','@cute']}

def remove_rounds(data):
    import string
    ndata = {}
    punct = string.punctuation
    rpunct = punct.replace('@',"") # withold @
    for k,v in data.items():
        alist = []
        ndata[k] = []
        for word in data[k]:
            if word.startswith("@"):
                continue # ignore this word and continue with the next one
            alist.append(word.strip(rpunct))
            ndata[k] = alist

    return ndata

print(remove_rounds(dat))

结果:

{'2008': ['what', 'fog'], '2010': ['hey']}