【问题标题】:How to delete words that longer than a certain length in a list of dictionaries如何删除字典列表中超过一定长度的单词
【发布时间】:2021-09-05 00:39:36
【问题描述】:

我有一个这样的字典列表:

myList = [
    {
        'id':1,
        'text':['I like cheese.', 
                'I love cheese.', 'oh Ilikecheese !'],
        'text_2': [('david',
    'david',
    'I do not like cheese.'),
   ('david',
    'david',
    'cheese is good.')]    
    },
    {
        'id':2,
        'text':['I like strawberry.', 'I love strawberry'],
        'text_2':[('alice',
    'alice',
    'strawberry is good.'),
   ('alice',
    'alice',
    ' strawberry is so so.')]    
    }
]

我想删除超过一定字母数(例如 9 个字母)的单词。

理想的输出是相同的字典列表,但删除拼写错误的单词,例如删除“Ilikecheese”:

myList = [
    {
        'id':1,
        'text':['I like cheese.', 
                'I love cheese.', 'oh!'],
        'text_2': [('david',
    'david',
    'I do not like cheese.'),
   ('david',
    'david',
    'cheese is good.')]    
    },
    {
        'id':2,
        'text':['I like strawberry.', 'I love strawberry'],
        'text_2':[('alice',
    'alice',
    'strawberry is good.'),
   ('alice',
    'alice',
    ' strawberry is so so.')]    
    }
]

有什么建议吗?

【问题讨论】:

  • 进行列表理解,省略您不想要的单词。 newtext = [word for word in text if len(word) <= 9]
  • @j1-lee 感谢您的建议,我修改了问题
  • 究竟是什么阻碍了您编写代码来解决问题,就目前而言?您对代码需要如何工作有什么想法,以及您无法编写代码的哪一部分?如果您编写了代码,请展示代码并解释您尝试它时发生的情况以及与预期发生的情况有何不同。另外,为什么这被标记为tokenize?你真的不知道如何将字符串拆分成单词吗?
  • 请阅读How to Ask 并注意you are expected to make some attempt to solve the problem yourself。 Stack Overflow 不是代码编写服务,"any suggestions?" is not an actual question;你需要更具体。

标签: python string list dictionary tokenize


【解决方案1】:

删除字符串中大于或等于9的每个单词。分割字符串的标准:单个空格。

myList = # above

for d in myList:
    for k, v in d.items():
        if isinstance(v, list):
            for i, word in enumerate(v):
                v[i] = ' '.join(list(filter(lambda w: len(w)<9, word.split(' '))))

for d in myList: print(d)

输出

{'id': 1, 'text': ["I 'll tell you what . Next say ' Potts ' on the tower .", 'I assume . Light her up .', 'Cap , I need the lever !']}
{'id': 2, 'text': ['Dr. Banner .', 'Stark , we need a plan of attack !', '( taken by that )', 'Everyone ! Clear out !', "Think the guy 's a friendly ?", 'Those people need .', 'Then suit up .']}

如果tuples 而不是lists

for d in myList:
    for k, v in d.items():
        if isinstance(v, tuple):
            v = list(v)
            for i, word in enumerate(v):               
                v[i] = ' '.join([w for w in word.split(' ') if len(w) < 9])
            d[k] = tuple(v)

for d in myList: print(d)

【讨论】:

  • 可能更具可读性v[i] = ' '.join([w for w in word.split(' ') if len(w) &lt; 9])
  • 感谢您的回答!这样可行。但是,我只是注意到我的数据中有元组。当我尝试你的方法时,我记下了这个错误:AttributeError:'tuple' object has no attribute 'split'。我更改了示例数据。有解决此错误的建议吗?
  • 只需像这样编辑:if isinstance(v, tuple): 和正下方 v = list(v) 这会将每个元组转换为列表
  • 我有列表(即'text')和元组(即'text_2')...我该怎么办...?
猜你喜欢
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 2012-04-26
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
相关资源
最近更新 更多