>>> import itertools
>>> n = [1,2,3,4]
>>> combObj = itertools.combinations(n,3)
>>>
>>> combObj
<itertools.combinations object at 0x00000000028C91D8>
>>>
>>> list(combObj)
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
>>>
>>> for i in list(combObj): #This prints nothing
... print(i)
...
Run Code Online (Sandbox Code Playgroud)
我怎样才能遍历combObj?
我怎样才能转换
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
为
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
我执行了以下脚本:
from itertools import groupby
from pprint import pprint as prnt
dt = [('23271800', 0.00066790780636275307),
('23271812', 0.0010018617095441298),
('26112103', 0.00066790780636275307),
('27111616', 0.0056772163540834012),
# ... many lines deleted ...
('40161500', 0.00040074468381765189)
]
agg = groupby(dt, lambda x: x[0])
lst = list(agg)
lst1 = map(lambda x: (x[0], list(x[1])), lst)
prnt(lst1)
Run Code Online (Sandbox Code Playgroud)
对于该项目,'23271800'它应报告[('23271800', 0.00066790780636275307)]为其对应的groupby项目.但是,我得到的输出不正确.
[('23271800', []),
('23271812', []),
('26112103', []),
('27111616', []),
# ... many lines deleted ...
('40161500', [('40161500', 0.00040074468381765189)])]
Run Code Online (Sandbox Code Playgroud)
如果我在这里做错了什么,需要帮助.
PS:代码粘贴:http://codepad.org/cCd8DfoT
我只需要得到有字母和数字的排列(排列不可能."A,B,C,D"我需要它像这样:"A,B,C,1")
简而言之,排列不能只包含字母,而不仅仅是数字.必须是两者的结合.
我的代码:
import itertools
print list(itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4))
Run Code Online (Sandbox Code Playgroud)
然后我得到:
[(0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 2, 'a'), (0, 1, 2, 'b'), (0, 1, 2, 'c'), (0, 1, 2, 'd'), (0, 1, 3, 4), (0, 1, 3, 'a'), (0, 1, 3, 'b'), (0, 1, 3, 'c'), (0, 1, 3, 'd'), (0, 1, 4, 'a'), (0, 1, 4, 'b'), (0, 1, 4, 'c'), (0, 1, 4, 'd'), (0, 1, 'a', 'b'), (0, 1, 'a', 'c'), (0, 1, 'a', …Run Code Online (Sandbox Code Playgroud) 我需要生成所有可能的长度在5至15之间的核苷酸组合的列表。
nucleotides = ['A', 'T', 'G', 'C']
Run Code Online (Sandbox Code Playgroud)
预期成绩:
AAAAA
AAAAT
AAAAC
AAAAG
AAATA
AAATT
...
AAAAAAAAAAAAAAA
AAAAAAAAAAAAAAT
etc.
Run Code Online (Sandbox Code Playgroud)
我试过了:
for i in range(5,16):
for j in itertools.permutations(nucleotides, i):
print j
Run Code Online (Sandbox Code Playgroud)
但是,如果这样做是行不通的len(nucleotides) < i。
提前致谢!
python combinations permutation bioinformatics python-itertools
我刚刚发现,各种itertools函数返回的类类型在Python类型系统中均不视为生成器。
首先,设置:
import collections
import glob
import itertools
import types
ig = glob.iglob('*')
iz = itertools.izip([1,2], [3,4])
Run Code Online (Sandbox Code Playgroud)
然后:
>>> isinstance(ig, types.GeneratorType)
True
>>> isinstance(iz, types.GeneratorType)
False
Run Code Online (Sandbox Code Playgroud)
的glob.iglob()结果,或任何其他典型的发电机,是类型的types.GeneratorType。但是itertools结果不是。如果我想编写一个必须急切地评估其输入序列的函数,这会引起很多混乱-我需要知道它是否是生成器。
我找到了这个替代方案:
>>> isinstance(ig, collections.Iterator)
True
>>> isinstance(iz, collections.Iterator)
True
Run Code Online (Sandbox Code Playgroud)
但是,这并不理想,因为iter(x)是Iterator不管的x是混凝土(热切评估)序列,或发电机(懒洋洋地评估)。
最终目标是这样的:
def foo(self, sequence):
"""Store the sequence, making sure it is fully
evaluated before this function returns."""
if isinstance(sequence, types.GeneratorType):
self.sequence = list(sequence)
else:
self.sequence = sequence
Run Code Online (Sandbox Code Playgroud)
为什么要执行此操作的一个示例是,如果对序列的求值可能会引发异常,并且我希望从而foo()不是随后使用时引发该异常self.sequence。
我不喜欢这种 …
如果这是重复的,已经回答了对不起,我没有遇到这个问题
因为我正在阅读itertools计数,使用for循环生成迭代器很容易,我试图在列表理解中这样做但我正面临这个问题
from itertools import *
Run Code Online (Sandbox Code Playgroud)
通过使用for循环
for x in itertools.count(5,2):
if x > 20:
break
else:
print(x)
5
7
9
11
13
15
17
19
Run Code Online (Sandbox Code Playgroud)
我试图在列表理解中这样做
[x if x<20 else break for x in count(5,2)]
File "<ipython-input-9-436737c82775>", line 1
[x if x<20 else break for x in count(5,2)]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我尝试使用islice方法,我得到了答案
[x for x in itertools.islice(itertools.count(5),10)]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Run Code Online (Sandbox Code Playgroud)
不使用islice方法,如何只使用count方法退出(使用break或任何东西)?
另外如何实现break的list comprehensions?
itertools.list(product([0, 1], repeat=n))当n很小时,我可以构造一个n长度二进制值组合的列表.
1000
0100
0110
1001
.
.
.
Run Code Online (Sandbox Code Playgroud)
当n很大时,如何在不首先构建大量组合列表的情况下随机选择上面列表的子集?
假设我想在n = 30时随机选择100万个组合而不进行替换(总共2 ^ 30个组合)
我查看了itertools的扩展函数http://docs.python.org/2/library/itertools.html#recipes
def random_product(*args, **kwds):
"Random selection from itertools.product(*args, **kwds)"
pools = map(tuple, args) * kwds.get('repeat', 1)
return tuple(random.choice(pool) for pool in pools)
Run Code Online (Sandbox Code Playgroud)
但它一次只返回一次.在获得100万个独特组合之前,我应该循环使用此功能吗?或者有更好的方法.谢谢!
能否将python itertools组合库用于对象而不是列表?
例如,如何在以下数据上使用它?
Rahul - 20,000 - Mumbai
Shivani - 30,000 - Mumbai
Akash - 40,000 - Bangalore
Run Code Online (Sandbox Code Playgroud)
我希望在名称和合并薪水值上使用所有可能的组合。
我该怎么做combinations?
假设使用读取pd.read_csv并存储了数据。
到目前为止的代码-
import pandas as pd
import itertools
df = pd.read_csv('stack.csv')
print (df)
for L in range(0, len(df)+1):
for subset in itertools.combinations(df['Name'], L):
print (subset)
Run Code Online (Sandbox Code Playgroud)
输出量
Name Salary City
0 Rahul 20000 Mumbai
1 Shivani 30000 Mumbai
2 Akash 40000 Bangalore
()
('Rahul',)
('Shivani',)
('Akash',)
('Rahul', 'Shivani')
('Rahul', 'Akash')
('Shivani', 'Akash')
('Rahul', 'Shivani', 'Akash')
Process finished …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算itertools.product()的第n个结果
test = list(product('01', repeat=3))
print(test)
desired_output = test[0]
print(desired_output)
Run Code Online (Sandbox Code Playgroud)
所以不要得到:
[('0', '0', '0'), ('0', '0', '1'), ('0', '1', '0'), ('0', '1', '1'), ('1', '0', '0'), ('1', '0', '1'), ('1', '1', '0'), ('1', '1', '1')]
Run Code Online (Sandbox Code Playgroud)
我想要:
('0', '0', '0')
Run Code Online (Sandbox Code Playgroud)
然而,正如您可能已经猜到的那样,它不能很好地扩展.这就是为什么我只想计算第n个值.
我阅读了 Nth Combination,但我需要product()提供的重复功能
提前致谢!
python combinations combinatorics python-itertools python-3.x
鉴于我有一个像这样的字符串:
'velvet evening purse bags'
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到所有的单词对?换句话说,这是所有2字组合:
'velvet evening'
'velvet purse'
'velvet bags'
'evening purse'
'evening bags'
'purse bags'
Run Code Online (Sandbox Code Playgroud)
我知道python的nltk软件包可以提供二元组,但是我正在寻找功能之外的东西。还是我必须用Python编写自己的自定义函数?
python ×10
python-itertools ×10
combinations ×5
python-3.x ×4
permutation ×2
generator ×1
group-by ×1
iterator ×1
nlp ×1
nltk ×1
pandas ×1
random ×1
types ×1