- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
python 2.7
给定一个等位基因
列表和数组numb_alleles
的长度,例如:
alleles = [11, 12, 13, 14, 15, 16]
numb_alleles = 8
我一直在尝试遍历每个笛卡尔积并选择符合以下选择标准的与我的研究相关的等位基因:
[13, 15, 11, 12, 14 , 15, 16, 16]
会满足选择标准,而 [13, 15, 16, 12, 14, 15, 16, 16]
不会因为索引 2 和 3 .等位基因
中的每个值都必须存在于笛卡尔积中。例如,[13, 15, 11, 12, 14, 15, 16, 16]
将满足选择标准,而 [13, 15, 11, 12, 14, 15, 11, 13]
不会,因为 16
不在产品中。我一直在使用 itertools.product(alleles, repeat = numb_alleles)
遍历每个可能的笛卡尔积以进一步分析。然而,随着 numb_alleles
增加到 10 或 12,整体计算量显着增加。
我试图通过使用下面的递归函数选择相关的笛卡尔积来解决这个问题。
def check_allele(allele_combination, alleles):
"""Check if all the alleles are present in allele_combination"""
for allele in alleles:
if allele not in allele_combination:
return False
return True
def recursive_product(alleles, numb_alleles, result):
current_len = len(result[0])
new_result = []
final_result = []
for comb in result:
for allele in alleles:
if current_len % 2 == 0:
new_result.append(comb + [allele])
elif current_len % 2 == 1:
if comb[-1] <= allele:
new_result.append(comb + [allele])
if (check_allele(comb + [allele], alleles)):
final_result.append(comb + [allele])
if current_len + 1 < numb_alleles:
return recursive_product(alleles, numb_alleles, new_result)
else:
return final_result
a = (recursive_product(alleles, numb_alleles, [[]]))
但是,使用这种方法我仍然无法处理数组到 numb_alleles = 12
或者当 alleles
的长度增加时,因为我正在使用 return
而不是 yield
。因此,它会导致内存不足错误。
我想知道我是否有可能将这个函数变成一个生成器,或者是否有人可以建议不同的方法,以便我可以进一步计算 numb_alleles = 12
和更长的输出等位基因
数组。
非常感谢!
最佳答案
您说:“笛卡尔积中的每一秒值都必须大于它之前的值。”但是在您的示例中 [13, 15, 11, 12, 14, 15, 16, 16]
插槽中的项目 7 (16) 等于前一个插槽中的项目,所以我假设您的意思是奇数索引处的项目必须 >= 前一个偶数索引处的项目。
下面的生成器比您当前的方法更有效,并且它避免在 RAM 中保存大量临时列表。核心思想是使用 itertools.product
为偶数槽生成组合,然后再次使用 product
填充满足选择标准#1 的奇数槽。我们使用集合操作来确保最终组合包含等位基因
中的每个项目。
from itertools import product
def combine_alleles(alleles, numb_alleles):
''' Make combinations that conform to the selection criteria. First create
the items for the even slots, then create items for the odd slots such
that each odd slot item >= the corresponding even slot item. Then test
that the whole combination contains each item in alleles.
'''
# If the number of unique items in the even slots is < min_len, then it's
# impossible to make a full combination containing all of the alleles.
min_len = len(alleles) - numb_alleles // 2
# Create a function to test if a given combination
# contains all of the alleles.
alleles_set = set(alleles)
complete = alleles_set.issubset
# Make lists of alleles that are >= the current allele number
higher = {k: [u for u in alleles if u >= k] for k in alleles}
# Make combinations for the even slots
for evens in product(alleles, repeat=numb_alleles // 2):
if len(set(evens)) < min_len:
continue
# Make combinations for the odd slots that go with this
# combination of evens.
a = [higher[u] for u in evens]
for odds in product(*a):
if complete(evens + odds):
yield [u for pair in zip(evens, odds) for u in pair]
# test
alleles = [11, 12, 13, 14, 15, 16]
numb_alleles = 8
for i, t in enumerate(combine_alleles(alleles, numb_alleles), 1):
print(i, t)
此代码找到 16020 个组合,因此输出太大,无法包含在此处。
这是一个更接近您的版本的替代生成器,但在我的测试中它比我的第一个版本慢一点。
def combine_alleles(alleles, numb_alleles):
total_len = len(alleles)
# Make lists of alleles that are >= the current allele number
higher = {k: [u for u in alleles if u >= k] for k in alleles}
def combos(i, base):
remaining = numb_alleles - i
if len(set(base)) + remaining < total_len:
return
if remaining == 0:
yield base
return
ii = i + 1
for u in higher[base[-1]] if i % 2 else alleles:
yield from combos(ii, base + [u])
yield from combos(0, [])
此版本适用于 Python 3。Python 2 没有 yield from
,但这很容易修复:
yield from some_iterable
相当于
for t in some_iterable:
yield t
关于python - 使用递归的生成器对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48256088/
我使用以下代码和嵌套生成器迭代文本文档并使用 get_train_minibatch() 返回训练示例。我想保留( pickle )生成器,这样我就可以回到文本文档中的相同位置。但是,您不能 pick
在本教程中,您将借助示例了解 JavaScript 生成器。在 JavaScript 中,生成器提供了一种使用函数和迭代器的新方法。 使用生成器, 您可以从函数内部的任何位置停止执行函数 并从
LESS is very cool .我一直想知道是否有任何好的 html 生成器可以让我更轻松地编写表单或做其他事情。除了 html,是否有一些类似的东西? 最佳答案 已尝试 Haml ? 从它的网
前言 如果是做python或者其他语言的小伙伴,对于生成器应该不陌生。但很多php开发者或许都不知道生成器这个功能,可能是因为生成器是php 5.5.0才引入的功能,也可以是生成器作用不是很明显。
我正在尝试编写一个使用生成器语法生成日期时间列表的函数: let dateRange = let endDate = System.DateTime.Parse("6/1/2010")
我遇到了一些看起来像的代码: [func(val) for val in iterable] 有一个可迭代对象(在我的例子中是一个生成器),用户想要为其副作用调用每个值的函数(例如 func 可以只是
Delphi 有内置的东西来生成 UUID 吗? 最佳答案 program Guid; {$APPTYPE CONSOLE} uses SysUtils; var Uid: TGuid; Result
我正在深入研究 javascript 生成器,但我真的很困惑。 我使用 node@0.11.x 运行此示例: function find() { process.nextTick(functi
有人知道一些关于如何为 hibernate 创建自定义 ID 生成器的好教程吗? 最佳答案 在 Google 上粗略搜索“hibernate 自定义 id 生成器教程”发现了以下可能性。我排除了那些看
我正在关注 Python 大师 David Beazley 的幻灯片。它指出“生成器也用于并发。这是一个示例: from collections import deque def countdown(
我有一个生成事件的生成器,我想用可以从 API 获取的附加元数据来丰富它。 某些事件具有与其链接的对象 ID,而其他事件则具有对象的哈希值,但不能同时具有两者。我无法根据哈希获取对象 id,我只能执行
假设我有一个自定义类: public class CustomClass { private String name; private String data; public
我正在考虑实现一个函数来在 SQL 请求中“构建”WHERE 子句,如下所示: "SELECT * FROM table $where" 使用如下所示的循环构建 $where: $arr=array(
我正在寻找执行此操作的标准函数: def Forever(v): while True: yield v 这看起来太琐碎了,我不敢相信没有标准版本。 就此而言,有人知道指向所有标准生成器函
我知道这个网站上有几个非常相似的相关问题,但是在看了这部剧之后,我相信这个问题本身就是独一无二的。如果有人能找到并提供证据证明我的问题完全被骗了,我会自己撤回它(所以请不要否决这个!)。 我是 Jav
void __fastcall TForm1::Button1Click(TObject *Sender) { int size = MemoEnter->GetTextLen() + 1;
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我试图在我的生成器的以下两个定义之间做出决定。哪个更好?哪个“更像 python ”?无论如何,有没有办法减轻每一个的缺点? def myGenerator1(howMany): result
我有一个 Python 生成器 lexg,它在每次迭代时生成一个列表。该代码似乎在传统的 for 循环意义上工作,即 for i in lexg(2,2): print(i) 产生: [2, 0] [
我希望这不会超出 Python 生成器的能力,但我想构建一个这样,每次调用该函数时,它都会返回下一分钟直到结束时间。 因此该函数读取开始时间和结束时间,并以分钟为单位返回时间,直到涵盖其间的所有时间。
我是一名优秀的程序员,十分优秀!