如何安装 itertools 包?

新手上路,请多包涵

我想尝试 itertools 模块中的 permutations 功能。但是每次我尝试实现它时,我都会收到以下错误:

代码:

 from itertools import permutations

txt=permutations('SKIN')
print(txt)

输出:

 <itertools.permutations object at 0x7fee48665950>

我尝试在命令提示符下使用命令 pip install itertools 但我一直收到错误消息:

 ERROR: Could not find a version that satisfies the requirement itertools (from versions: none)
ERROR: No matching distribution found for itertools

如何安装软件包?

原文由 user12735269 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
1 个回答

itertools 是内置模块,无需安装:

 Help on module itertools:

NAME
    itertools - Functional tools for creating and using iterators.

FILE
    /usr/lib64/python2.7/lib-dynload/itertoolsmodu

permutations(<iterable>) 返回一个生成器,它产生可迭代元素的连续 r 长度排列:

 >>> type(txt)
<type 'itertools.permutations'>

>>> dir(txt)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'next']

所需排列列表:

 list_perms = [ "".join(i) for i in permutations("SKIN")]

# ['SKIN', 'SKNI', 'SIKN', 'SINK', 'SNKI', 'SNIK', 'KSIN', 'KSNI', 'KISN', 'KINS', 'KNSI', 'KNIS', 'ISKN', 'ISNK', 'IKSN', 'IKNS', 'INSK', 'INKS', 'NSKI', 'NSIK', 'NKSI', 'NKIS', 'NISK', 'NIKS']

原文由 dejanualex 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏