如何删除字典列表中超过一定长度的单词

How to delete words that longer than a certain length in a list of dictionaries

我有这样一个字典列表:

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.')]    
    }
]

有什么建议吗?

去掉字符串中每一个大于等于9的单词。拆分字符串的标准:单白-space.

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)