【发布时间】:2021-11-03 00:24:15
【问题描述】:
我有一个这样的字典列表:
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.')
]
}
]
我想通过“id”计算“text”和“text_2”的元素数量和长度。理想的输出是:
myList = [
{
'id':1,
'text':(3,7),
'text_2': (2,8)
},
{
'id':2,
'text':(2,6),
'text_2':(2,7)
}
]
'text':(3,7) 表示:3 个元素('I like cheese.'、'I love cheese.'、'oh!'); 7个字(我喜欢奶酪,我喜欢奶酪哦)
'text_2':(2,8) 表示:2个元素(('david','david','我不喜欢奶酪。'),('david','david','cheese is good. ')); 8个字(I, do, not, like, cheese, cheese, is good))
有什么建议吗?
【问题讨论】:
-
您也可以发布您的代码吗?
-
你能解决像
['I like cheese.', 'I love cheese.', 'oh!']这样的单个列表的问题吗?如果可以,那么在您自己的估计中,是什么阻止您将该技术应用于整个数据结构?如果你不能,为了解决这个问题,你到底需要知道什么?请阅读How to Ask 和meta.stackoverflow.com/questions/284236/…,并注意您应该先尝试自己解决问题。
标签: python list dictionary word-count