标签: python-itertools

在python 3中使用itertools.product和列表

我正在尝试根据蛋白质序列创建一个可能的密码子列表.

基本上,我正在尝试创建的脚本将处理给定的字符串输入并输出输入所代表的另一组字符串的可能组合.

例如,字符"F"代表"UUU"或"UUC"; 角色'I'代表'AUU','AUC'或'AUA'.

鉴于输入'FI',我试图创建的脚本应该输出:'UUUAUU','UUUAUC','UUUAUA','UUCAUU','UUCAUC'和'UUCAUA'.

我目前仍然坚持使用此代码:

import itertools

F = ['UUU', 'UUC']
I = ['AUU', 'AUC', 'AUA']

seq, pool = 'FI', []

for i in seq:
   pool.append(eval(i))

for n in itertools.product(pool):
   print(n)
Run Code Online (Sandbox Code Playgroud)

当我替换pool时itertools.product,它工作pool[0], pool[1].但我无法弄清楚如何使其工作,以便用户可以输入超过2个字符的字符串(即不使其硬编码).

在此先感谢您的帮助!

python python-itertools python-3.x

6
推荐指数
1
解决办法
6432
查看次数

python:是否有用于分块输入流的库函数?

我想将输入流分块以进行批处理.给定输入列表或生成器,

x_in = [1, 2, 3, 4, 5, 6 ...]
Run Code Online (Sandbox Code Playgroud)

我想要一个返回该输入块的函数.说,如果chunk_size=4,那么,

x_chunked = [[1, 2, 3, 4], [5, 6, ...], ...]
Run Code Online (Sandbox Code Playgroud)

这是我一遍又一遍地做的事情,并且想知道是否有比我自己写的更标准的方式.我错过了什么itertools吗?(人们可以用enumerate和解决问题groupby,但是感觉很笨.)如果有人想要看到一个实现,这里是,

def chunk_input_stream(input_stream, chunk_size):
    """partition a generator in a streaming fashion"""
    assert chunk_size >= 1
    accumulator = []
    for x in input_stream:
        accumulator.append(x)
        if len(accumulator) == chunk_size:
            yield accumulator
            accumulator = []
    if accumulator:
        yield accumulator
Run Code Online (Sandbox Code Playgroud)

编辑

灵感来自kreativitea的答案,这是一个解决方案islice,它很简单,不需要后置过滤,

from itertools import islice

def chunk_input_stream(input_stream, chunk_size):
    while True:
        chunk = …
Run Code Online (Sandbox Code Playgroud)

python stream chunking python-itertools

6
推荐指数
2
解决办法
1231
查看次数

使用itertools进行递归函数应用

我需要一个Python函数iterate(f, x),它创建一个迭代器,返回值x,f(x),f(f(x)),f(f(f(x)))等(例如,Clojure的iterate).首先,我想知道:这是否已经存在于标准库的某个地方,我只是错过了它?当然,使用生成器实现起来很容易:

def iterate(f, x):
    while True:
        yield x
        x = f(x)
Run Code Online (Sandbox Code Playgroud)

只是出于好奇:在Python中有没有更多功能性的方法来实现这一点,例如使用一些itertools或functools魔术?

在Python 3.3中,这可行

def iterate(f, x):
    return accumulate(repeat(x), lambda acc, _ : f(acc))
Run Code Online (Sandbox Code Playgroud)

但看起来像是对我的虐待.我可以更好地做到这一点吗?

python iterator functional-programming generator python-itertools

6
推荐指数
1
解决办法
1033
查看次数

使用itertools在Python中的Powersets

我正在尝试在Python 3中创建一个powerset.我找到了对该itertools 模块的引用,并且我使用了该页面上提供的powerset代码.问题是:代码返回itertools.chain对象的引用,而我想访问powerset中的元素.我的问题:如何做到这一点?

非常感谢您的见解.

python python-itertools python-3.x

6
推荐指数
1
解决办法
9439
查看次数

如何在python中创建顺序组合列表?

我有一个列表['a','b','c','d']我需要一个列表['a','ab','abc','abcd','b',' bc','bcd','c','cd','d'].

我一直在看itertools,但我没有看到如何使这项工作.

对于所有组合,代码将是:

from itertools import permutations
stuff = ['a','b','c','d']
for i in range(0, len(stuff)+1):
    for subset in permutations(stuff, i):
           print(subset)
Run Code Online (Sandbox Code Playgroud)

如果只返回顺序组合,我需要做什么?我想我可以随时检查每个排列的顺序,但这似乎不是最好的方法.

python python-itertools

6
推荐指数
1
解决办法
205
查看次数

创建具有两个值的字典时错过的值

我有两个列表如下.

count = (1, 0, 0, 2, 0, 0, 1, 1, 1, 2)
bins = [[2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0], [7.0, 8.0], [8.0, 9.0], [9.0, 10.0], [10.0, 11.0], [11.0, 12.0], [12.0]]
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下创建字典;

dictionary = dict(itertools.izip(count, bins))
Run Code Online (Sandbox Code Playgroud)

它给了我 {"0": [7.0, 8.0], "1": [10.0, 11.0], "2": [11.0, 12.0]}

它只提供唯一的键值,但我需要得到所有的对,如下所示.

{"0": [3.0, 4.0],"0": [4.0, 5.0],"0": [6.0, 7.0],"0": [7.0, 8.0], "1": [2.0, 3.0],"1": [8.0, 9.0], "1": [9.0, 10.0], "1": [10.0, 11.0], "2": [6.0, 7.0] ,"2": [11.0, 12.0]}
Run Code Online (Sandbox Code Playgroud)

或者上面词典中的键和值的交换是可以接受的.(因为键应该是唯一的)我该怎么做?

python dictionary python-itertools

6
推荐指数
1
解决办法
57
查看次数

Python:获得投资组合的所有权重组合

我认为这个问题可以使用itertools或者笛卡尔来解决,但我对Python很新,我正在努力使用它们:

我有5个股票的投资组合,其中每个股票的权重可以是-0.4,-0.2,0,0.2或0.4,权重加起来为0.我如何创建一个函数来生成每个可能组合的列表权重.例如[-0.4,0.2,0,0.2,0] ......等

理想情况下,该功能适用​​于n种股票,因为我最终希望对50种股票进行相同的处理.

编辑:为了澄清,我正在寻找长度为n的所有组合(在这种情况下为5),求和为0. 值可以重复:例如:[0.2,0.2,-0.4,0,0],[0.4,0 ,-0.2,-0.2,0.4],[0,0,0,0.2,-0.2],[0,0.4,-0.4,0.2,-0.2]等.[0,0,0,0,0]将是一个可能的组合.有5个可能的权重和5个股票的事实是巧合(我应该避免!),同样的问题可能是5个可能的权重和3个股票或7个股票.谢谢.

python list cartesian-product python-itertools

6
推荐指数
1
解决办法
995
查看次数

更快的numpy-solution而不是itertools.combinations?

我使用itertools.combinations()如下:

import itertools
import numpy as np

L = [1,2,3,4,5]
N = 3

output = np.array([a for a in itertools.combinations(L,N)]).T
Run Code Online (Sandbox Code Playgroud)

这产生了我需要的输出:

array([[1, 1, 1, 1, 1, 1, 2, 2, 2, 3],
       [2, 2, 2, 3, 3, 4, 3, 3, 4, 4],
       [3, 4, 5, 4, 5, 5, 4, 5, 5, 5]])
Run Code Online (Sandbox Code Playgroud)

我在多处理环境中反复使用这个表达式,我需要它尽可能快.

从这篇文章中我了解到,itertools基于代码的代码并不是最快的解决方案,并且使用numpy可能是一种改进,但是我numpy对于理解和调整在那里编写的迭代代码或者提出我自己的优化的优化技巧还不够好..

任何帮助将不胜感激.

编辑:

L 来自熊猫数据帧,所以它也可以看作是一个numpy数组:

L = df.L.values
Run Code Online (Sandbox Code Playgroud)

python optimization numpy python-itertools python-2.7

6
推荐指数
1
解决办法
1359
查看次数

用于组合异步迭代器的map,filter和itertools

Python是否支持异步迭代器上的函数式操作?我知道,我可以使用map,filter并itertools懒洋洋地转换和消耗数据与正常的发电机来:

from itertools import accumulate, takewhile

def generator():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, a + b

# create another iterator, no computation is started yet:
another_iterator = takewhile(lambda x: x < 100, accumulate(generator()))
# start consuming data:
print(list(another_iterator))
# [1, 2, 4, 7, 12, 20, 33, 54, 88]
Run Code Online (Sandbox Code Playgroud)

现在,Python 3.6的异步生成器/迭代器不支持同样的事情,因为它们当然没有实现正常的迭代器协议:

async def agenerator():
    a, b = 1, 1
    while True:
        yield a
        a, b = b, …
Run Code Online (Sandbox Code Playgroud)

python python-itertools python-asyncio python-3.6

6
推荐指数
1
解决办法
406
查看次数

任何加快itertool.product速度的方法

我正在使用itertools.product来查找资产的可能权重,因为所有权重之和总计为100。

min_wt = 10
max_wt = 50
step = 10
nb_Assets = 5

weight_mat = []
for i in itertools.product(range(min_wt, (max_wt+1), step), repeat = nb_Assets):
    if sum(i) == 100:
        weight = [i]
        if np.shape(weight_mat)[0] == 0:
            weight_mat = weight
        else:
            weight_mat = np.concatenate((weight_mat, weight), axis = 0)
Run Code Online (Sandbox Code Playgroud)

上面的代码可以工作,但是因为它经历了不可接受的组合,所以它太慢了,示例[50,50,50,50,50]最终测试了3125个组合,而不是121个可能的组合。有什么方法可以在循环中添加“求和”条件以加快处理速度?

python numpy nested-loops python-itertools

6
推荐指数
1
解决办法
83
查看次数