所以我有一个清单看起来是这样的:
[['hostile', 'angry'], ['elated', 'happy'], ['elated', 'grateful'], ['depressed', 'sad']]
由此产生:
c.execute("""SELECT category, wordlist from wordtest order by category""")
categoryfile = c.fetchall()
categoryfile = [list(x) for x in categoryfile]
我希望category的所有值都合并到一个键中,然后wordlist中与该类别配对的所有单词都合并到一个列表中。有可能吗?你知道吗
所以最终,有了这个列表,你会看到
['elated', 'happy'], ['elated', 'grateful']
变成:
{'elated': ['happy', 'grateful']}
使用
collections.defaultdict
:输出:
相关问题 更多 >
编程相关推荐