内存高效的替代Python字典(Memory Efficient Alternatives to Python Dictionaries)
在我目前的一个项目中,我正在扫描一些文字,查看单词三元组的频率。 在我第一次去,我使用默认字典三级深。 换句话说,
topDict[word1][word2][word3]
返回这些单词出现在文本中的次数,topDict[word1][word2]
返回一个字典,其中包含单词1和2之后出现的所有单词等。这功能正常,但是它非常内存密集。 在我的初始测试中,它使用像存储三进制存储在文本文件中的内存的20倍,这似乎是一个过多的内存开销。
我的怀疑是,许多这些字典正在创造出比实际使用的更多的插槽,所以我想用这种方式使用更高内存效率的其他内容替换字典。 我强烈希望有一个解决方案,允许按照字典的关键查找。
从我所知道的数据结构,使用红黑或AVL的平衡二叉搜索树可能是理想的,但我真的不愿意自己实现。 如果可能的话,我更喜欢坚持使用标准的python库,但如果他们能最好的工作,我肯定会接受其他替代方案。
那么有没有人对我有任何建议?
编辑添加:
感谢您的答复。 到目前为止,有几个答案建议使用元组,当我将前两个词汇集成一个元组时,这对我来说并没有太大的帮助。 我犹豫使用这三个键作为关键,因为我希望能够容易地查找前两者的所有第三个字。 (即我想要像
topDict[word1, word2].keys()
)的结果。我正在玩的当前数据集是Wikipedia For Schools的最新版本。 例如,解析一千页的结果对于一个文本文件是像11MB,其中每行是三个单词,并且count个tab分隔。 以字典格式存储文本我现在使用大约需要185MB。 我知道指针会有一些额外的开销,但是差异似乎是过分的。
In one of my current side projects, I am scanning through some text looking at the frequency of word triplets. In my first go at it, I used the default dictionary three levels deep. In other words,
topDict[word1][word2][word3]
returns the number of times these words appear in the text,topDict[word1][word2]
returns a dictionary with all the words that appeared following words 1 and 2, etc.This functions correctly, but it is very memory intensive. In my initial tests it used something like 20 times the memory of just storing the triplets in a text file, which seems like an overly large amount of memory overhead.
My suspicion is that many of these dictionaries are being created with many more slots than are actually being used, so I want to replace the dictionaries with something else that is more memory efficient when used in this manner. I would strongly prefer a solution that allows key lookups along the lines of the dictionaries.
From what I know of data structures, a balanced binary search tree using something like red-black or AVL would probably be ideal, but I would really prefer not to implement them myself. If possible, I'd prefer to stick with standard python libraries, but I'm definitely open to other alternatives if they would work best.
So, does anyone have any suggestions for me?
Edited to add:
Thanks for the responses so far. A few of the answers so far have suggested using tuples, which didn't really do much for me when I condensed the first two words into a tuple. I am hesitant to use all three as a key since I want it to be easy to look up all third words given the first two. (i.e. I want something like the result of
topDict[word1, word2].keys()
).The current dataset I am playing around with is the most recent version of Wikipedia For Schools. The results of parsing the first thousand pages, for example, is something like 11MB for a text file where each line is the three words and the count all tab separated. Storing the text in the dictionary format I am now using takes around 185MB. I know that there will be some additional overhead for pointers and whatnot, but the difference seems excessive.
原文:https://stackoverflow.com/questions/327223
最满意答案
这些符号没有任何意义,它们只是用来更好地读取树的输出!
下面是一个更复杂的输出,可以更好地查看它的功能,以及
spring-webmvc
依赖关系:[INFO] +- org.springframework:spring-webmvc:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:4.2.2.RELEASE:compile [INFO] | | \- org.springframework:spring-aop:jar:4.2.2.RELEASE:compile [INFO] | | \- aopalliance:aopalliance:jar:1.0:compile [INFO] | +- org.springframework:spring-core:jar:4.2.2.RELEASE:compile [INFO] | | \- commons-logging:commons-logging:jar:1.2:compile [INFO] | +- org.springframework:spring-expression:jar:4.2.2.RELEASE:compile
将依赖关系树视为级别:第一级对应于直接依赖关系; 第二级对应于那些直接依赖关系的传递依赖关系等。
基本上,如果对同一个工件有相同级别的多个依赖关系,则会显示
+-
,否则将显示\-
,表示树的“结束”(即通向叶子的路径) 。Those symbols do not have any meaning whatsoever, they are just present to read the output of the tree better!
Here's a more complex output to see better what it does, on a
spring-webmvc
dependency:[INFO] +- org.springframework:spring-webmvc:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:4.2.2.RELEASE:compile [INFO] | | \- org.springframework:spring-aop:jar:4.2.2.RELEASE:compile [INFO] | | \- aopalliance:aopalliance:jar:1.0:compile [INFO] | +- org.springframework:spring-core:jar:4.2.2.RELEASE:compile [INFO] | | \- commons-logging:commons-logging:jar:1.2:compile [INFO] | +- org.springframework:spring-expression:jar:4.2.2.RELEASE:compile
Consider the dependency tree as levels: the first level correspond to the direct dependencies; the second level corresponds to the transitive dependencies of those direct dependencies, etc.
Basically, if there is more than one dependency on the same level for the same artifact, a
+-
will be shown, otherwise a\-
will be shown, indicating an "end" of the tree (i.e. a path leading to a leaf).
相关问答
更多-
这些符号没有任何意义,它们只是用来更好地读取树的输出! 下面是一个更复杂的输出,可以更好地查看它的功能,以及spring-webmvc依赖关系: [INFO] +- org.springframework:spring-webmvc:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-beans:jar:4.2.2.RELEASE:compile [INFO] | +- org.springframework:spring-c ...
-
新新 200新新新新200新新新200新200新200新新200新200新200新200新200新新200新200新200新新200新200新200新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新200新新新200新新200新新200新新200新新200新新200新新200新新新200新新200新新200新新200新新200新新200新新200新新200新 ...
-
插件和依赖都是Jar文件。 但它们之间的区别是,maven中的大部分工作都是使用插件完成的; 而依赖关系只是一个Jar文件,它将在执行任务时添加到类路径中。 例如,您使用编译器插件来编译java文件。 您不能使用compiler-plugin作为依赖关系,因为它只会将插件添加到类路径中,并且不会触发任何编译。 要在编译文件时添加到类路径中的Jar文件将被指定为依赖关系。 与你的情况一样。 你必须使用spring-plugin来执行一些spring的可执行文件[我不知道用什么spring-plugins。 我 ...
-
如何在项目中显示*插件*的Maven依赖关系树?(How can you display the Maven dependency tree for the *plugins* in your project?)[2022-01-29]
通过mvn -X的输出将间接打印信息。 目前没有其他选项来获取Maven-Plugin的依赖关系。 更新您可以使用以下命令获取插件依赖关系的列表(依赖关系插件的resolve-plugin目标 ): mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:resolve-plugins 较短的版本(指定插件版本是一个不好的习惯) mvn dependency:resolve-plugins The output via mvn -X will ... -
我正在用maven 3.1.1运行,并且依赖关系spring-expression被正确添加到WEB-INF / lib中。 有效的pom不包含spring-expression ,但这是正常的,因为有效的pom不能解决传递性依赖。 有效的pom只是一个合并的xml文件,与当前的pom以及所有父母合并在一起。 Maven依赖关系解析机制仅在建立有效的pom后应用。 这就是为什么有效的pom只包含spring-context依赖的原因,因为这是pom.xml中指定的唯一东西。 如果在父pom中会有父亲依赖关系 ...
-
这个怎么样: ^ *((\| +)*\+-+|(\| +)*\\-+) 我匹配一行的开头,后跟零或多个空格,然后管道的尾随空格为零或多次,后跟加号和短划线一次或多次,或者管道的尾随空格为零或多次后跟一个反冲和一个冲破一次或多次。 这涵盖了我在测试用例和实际maven输出中可以看到的所有模式。 How about this: ^ *((\| +)*\+-+|(\| +)*\\-+) I match the start of a line followed by zero or more spaces th ...
-
Maven依赖与多模块?(Maven dependency vs multimodule?)[2021-09-25]
依赖是预先构建的实体。 您从Maven Central(或Nexus等)获得该依赖项的工件。对属于其他团队或项目的代码使用依赖关系是很常见的。 例如,假设您需要Android中的一个CSV库。 你把它作为一个依赖。 Maven模块的构建就像您的项目一样。 将Maven模块用于项目所拥有的组件是很常见的。 例如,您的项目可能会创建三个jar文件。 A dependency is a pre-built entity. You get the artifact for that dependency from ... -
Maven 3使用Aether进行依赖关系解析。 在文档底部附近是另一个在Maven插件中使用Aether的链接。 Maven 3 uses Aether for dependency resolution. Near the bottom of the docs is another link to using Aether in Maven plugins.
-
parent是一个“遗传”的POM,它基本上通过覆盖与您当前的POM结合。 (您可以使用“mvn help:effective-pom”打印组合结果)。 因此,父级中的所有依赖项和插件将成为您的(以及所有托管的依赖项版本,这在弹簧引导的情况下占多数)。 dependendency指定您的类路径中需要用于编译,测试或运行项目(取决于其范围)的工件。 而plugin是在构建时被执行的工件(如编译器,报表生成器和其他东西)。 我建议阅读一本很好的教程或者关于maven的书,我的简单解释不足以充分利用它。 http ...
-
您可以使用 mvn dependency:tree -X 它产生调试输出 。 否则在关于verbose的文档中说明 - 请注意,此功能实际上使用Maven 2算法,并且在与Maven 3一起使用时可能会给出错误的结果 。 You can use mvn dependency:tree -X which produces the debug output. Otherwise stated in the documentation about verbose - Notice this feature a ...