我有一个嵌套列表,如下所示:
data = [[[], 'October'],
[[], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[['tiktok', 'tenaga kesehatan'], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[['kanker'], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[['jantung'], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[[], 'October'],
[['covid-19'], 'October'],
[[], 'October'],
[['jantung'], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[['covid-19'], 'October'],
[['covid-19', 'covid-19'], 'October'],
[['covid-19'], 'October'],
[[], 'September'],
[['covid-19'], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[[], 'September'],
[['covid-19', 'covid-19'], 'September'],
[['jantung'], 'September'],
[['jantung'], 'September'],
[['covid-19'], 'September'],
[['covid-19'], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[[], 'September'],
[['covid-19'], 'September'],
[[], 'September'],
[[], 'August'],
[[], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[['jantung'], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'August'],
[[], 'August'],
[['covid-19'], 'August'],
[['covid-19', 'covid-19'], 'August'],
[['covid-19'], 'August'],
[['covid-19'], 'July']]
我想用月份的名称来计算所有的代币('covid-19','JANTONG'…等),这样我就可以得到每月的代币频率
以下是我的预期产出:
result = [
['covid-19',0,0,0,0,0,0,1,19,17,21,0,0],
['tiktok',0,0,0,0,0,0,0,0,0,1,0,0],
['jantung',0,0,0,0,0,0,0,1,2,2,0,0],
['kanker',0,0,0,0,0,0,0,0,0,1,0,0],
['tenaga kesehatan',0,0,0,0,0,0,0,0,0,1,0,0],
]
请注意:'0,0,0,0,0,0,1,19,17,21,0,0'
是从一月到十二月的顺序和该月的标记的总和。请建议我一种将嵌套的标记转换为结果列表的方法
有什么想法吗
这里我们提供了一个可能的解决方案:
输出将是:
注意:不过,正如有人提到的,使用不同的数据结构来存储结果可能是个好主意。当然,一本字典会更方便,也会让你写出一个更线性的解决方案
虽然其他人写了非常好的答案,但我觉得通过
pandas
解决这个问题更容易维护,也更冗长。加上熊猫的对象真的很容易处理首先是进口:
以下是代码的主体:
输出将是:
产出将是:
您真的不应该在这样的列表中存储不同的数据,这样的列表怎么样
下面是一段代码片段,用于编写此命令:
相关问题 更多 >
编程相关推荐