如何计算字典中单词的长度



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

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

我想计算"text"的元素个数和长度和";text_2"通过"id"理想的输出是:

myList = [
{
'id':1,
'text':(3,7),
'text_2': (2,8)   
},
{
'id':2,
'text':(2,6),
'text_2':(2,7)    
}
]

'text':(3,7)意味着:3个元素('我喜欢奶酪。,"我喜欢奶酪。","哦!");7个字(我喜欢奶酪,我爱奶酪,哦)

"text_2":(8)的意思是:2元素(("大卫","大卫","我不喜欢奶酪。"),(‘大卫’,‘大卫’,'奶酪是好的。'));8个词(我,做,不,喜欢,奶酪,奶酪,是好的))

有什么建议吗?

如果你是新手,我的回答很难消化,但我希望你能找到一些对你未来有用的好组合…也因为你没有提供任何尝试。

  • ' '.join(my_list)生成以空格分隔的list元素字符串
  • my_string.split()通过在单个空格处(->所以你可以计数)
  • set(my_list)删除元素的多次出现
  • itertools.chain函数用于连接可迭代对象,将列表中的元组合并为单个对象
  • 列表推导式,例如[i for i in range(10) if i > 5]

由于您没有指定如何处理同一元素的多次出现的任何规则,我只计算它们一次(因此'david','david'被计数为1)

我对你的建议的回答是分而治之,把一个大问题分成几个小问题,解决它们,把它们粘合在一起。

import itertools as it
myList = # see dictionary in the question
for d in myList:
for k, v in d.items():
if isinstance(v, list):
pair = len(v), len(' '.join(v).split()) if isinstance(v[0], str) else len(' '.join([t for t in set(it.chain(*v))]).split())
print(pair)
else:
print(k, v)

输出
id 1
(3, 7)
(2, 9)
id 2
(2, 6)
(2, 8)

例如:

from itertools import chain
from string import punctuation
def remove_punctuation(text):
return "".join(filter(lambda x: x not in punctuation, text))
def count_items_and_words(items, label):
items_cnt = len(items)

if label == "text":
total_text = " ".join(items)
elif label == "text_2":
total_text = " ".join(chain(*[it[2:] for it in items]))
total_text_clean = remove_punctuation(total_text)

words_cnt = len(total_text_clean.split())
return (items_cnt, words_cnt)
def count_all(my_list):
results = list()
for it in my_list:
if not isinstance(it, dict):
continue
res = {"id": it["id"]}
for label in "text", "text_2":
res[label] = count_items_and_words(it[label], label)
results.append(res)
return results
results = count_all(myList)
results

输出:

[{'id': 1, 'text': (3, 7), 'text_2': (2, 8)},
{'id': 2, 'text': (2, 6), 'text_2': (2, 7)}]

见下文

lst = [
{
'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.')
]
}
]
out = []
for entry in lst:
out.append({})
for k, v in entry.items():
if k == 'id':
out[-1][k] = v
elif k == 'text':
out[-1][k] = (len(v), sum(len(x.split()) for x in v))
else:
out[-1][k] = (len(v),sum(len(x) for x in v))
print(out)

输出
[{'id': 1, 'text': (3, 7), 'text_2': (2, 6)}, {'id': 2, 'text': (2, 6), 'text_2': (2, 6)}]

最新更新