python - 在字典中创建一个集合

标签 python linux windows dictionary set

我需要在字典中创建一个集合。

dicInvertedIndex = {}
docID = 1

for i in string:
  if condition:
    docID += 1
  dicInvertedIndex[i] = [1, set(docID)]

我有一个错误:

dicInvertedIndex[i] = [1, set(docID)]
 TypeError: 'int' object is not iterable

在我尝试这个之前,我在字典中创建了一个列表并且它有效。

dicInvertedIndex[i] = [ 1 , [ docID ] ]

而且它有效。我需要用我的文档的键和 (int, set())

的值创建一个字典

dic["awake"] = [5, {2, 30, 99, 234}]

本来我用的是list,但是速度慢,想用set。

最佳答案

set() 构造函数接受一个可迭代对象。 docID 是一个整数,因此不可迭代。

两种解决方法是

{docID} 

set([docID])

关于python - 在字典中创建一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21941855/

相关文章:

c# - 使用存储在文件中的变量来触发事件

Python gensim LDA : add the topic to the document after getting the topics

linux - 对文件中具有指定模式最高值的行进行排序

c# - 简单的 C# 应用程序无法正常工作,从 Visual Studio 中运行时工作正常

.net - .net 应用程序可以在 Linux 上运行吗?

linux - 如何删除 vim 中每一行的第一个字符串?

c# - VC# 窗体闪烁

python - 用python重命名错误

python - theano 出现奇怪的语法错误

Python asyncio 跳过处理直到函数返回