我正在尝试根据蛋白质序列创建一个可能的密码子列表.
基本上,我正在尝试创建的脚本将处理给定的字符串输入并输出输入所代表的另一组字符串的可能组合.
例如,字符"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个字符的字符串(即不使其硬编码).
在此先感谢您的帮助!
我想将输入流分块以进行批处理.给定输入列表或生成器,
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) 我正在寻找一种Pythonic方法来生成(a,b) ? (b,a)包含偶数n项的所有成对唯一唯一对(其中配对是由对组成的系统,并且成对唯一表示).
我喜欢这里的代码:
for perm in itertools.permutations(range(n)):
print zip(perm[::2], perm[1::2])
Run Code Online (Sandbox Code Playgroud)
除了它生成所有订单唯一,成对唯一的配对,或者(n/2)!比我想要的更多配对(冗余),虽然我可以过滤掉,但实际上我的程序真的陷入困境n.
也就是说n = 4,我正在寻找以下输出(12个唯一配对):
[(0, 1), (2, 3)]
[(0, 1), (3, 2)]
[(1, 0), (2, 3)]
[(1, 0), (3, 2)]
[(1, 2), (0, 3)]
[(1, 2), (3, 0)]
[(1, 3), (0, 2)]
[(2, 0), (1, 3)]
[(2, 0), (3, 1)]
[(3, 1), (0, 2)]
[(0, 3), (2, 1)]
[(3, 0), (2, 1)]
Run Code Online (Sandbox Code Playgroud)
请注意(a,b) ? (b,a). …
我有一个列表['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)
如果只返回顺序组合,我需要做什么?我想我可以随时检查每个排列的顺序,但这似乎不是最好的方法.
我有两个列表如下.
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)
或者上面词典中的键和值的交换是可以接受的.(因为键应该是唯一的)我该怎么做?
我想找到一个有绑定值的向量的有效排列.
例如,如果perm_vector = [0,0,1,2]我想获得输出的所有组合[0,0,1,2], [0,0,2,1], [0,1,2,0]等等,但我不想获得[0,0,1,2]两次,这是标准itertools.permutations(perm_vector)给出的.
我试过以下但是当它perm_vector grows在len 时它真的很慢:
vectors_list = []
for it in itertools.permutations(perm_vector):
vectors_list.append(list(it))
df_vectors_list = pd.DataFrame( vectors_list)
df_gb = df_vectors_list.groupby(list(df_vectors_list.columns))
vectors_list = pd.DataFrame(df_gb.groups.keys()).T
Run Code Online (Sandbox Code Playgroud)
实际上,问题是更加普遍的"加速"性质.主要时间用于创建长向量的排列 - 即使没有两面性,创建12个唯一值的向量的排列也需要"无穷大".是否有可能迭代地调用itertools而不访问整个排列数据但是处理它的串?
我认为这个问题可以使用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个股票.谢谢.
我有一个覆盖巨大搜索空间的迭代.我的计划不是让脚本终止,而是在一段时间之后杀死脚本.
现在我需要这个空间的笛卡尔积并在那里搜索.itertools.product产生这个订单:
>>> list(itertools.product(range(3), repeat=2))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
Run Code Online (Sandbox Code Playgroud)
虽然我想按照类似于以下的对角线顺序进行搜索:
[(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (1, 2), (2, 1), (2, 2)]
Run Code Online (Sandbox Code Playgroud)
sorted使用一些返回元组元素总和的关键函数将是我的常规方法,但是对于排序所有数据都需要检查,这在我的情况下是不可行的.有没有办法做到这一点?
这个问题很相似,这一个,但sorted仍然是在回答中.此外,我不很快看到如何适应ordered_combinations到ordered_product.
python algorithm dynamic-programming combinatorics python-itertools
我有一个Python字典,字符串作为键,numpy数组作为值:
dictionary = {'first': np.array([1, 2]), 'second': np.array([3, 4])}
Run Code Online (Sandbox Code Playgroud)
现在我想用它itertools product来创建以下列表:
requested = [(1, 3), (1, 4), (2, 3), (2, 4)]
Run Code Online (Sandbox Code Playgroud)
通常在传递的项目product是numpy数组时完成.
当我执行以下操作时:
list(product(list(dictionary.values())))
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
[(array([3, 4]),), (array([1, 2]),)]
Run Code Online (Sandbox Code Playgroud) 我使用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 ×10
python-itertools ×10
dictionary ×2
python-3.x ×2
algorithm ×1
chunking ×1
list ×1
numpy ×1
optimization ×1
pandas ×1
performance ×1
python-2.7 ×1
stream ×1