【发布时间】:2015-07-23 14:37:34
【问题描述】:
我在for 循环中有一个列表,它使用itertools.product() 来查找不同的字母组合。我想使用collections.Counter() 来计算一个项目的出现次数,但是,现在它会打印“A”和“G”的所有不同组合:
['a', 'A', 'G', 'G']
['a', 'A', 'G', 'g']
['a', 'A', 'G', 'G']
['a', 'A', 'G', 'g']
['a', 'A', 'G', 'g']
#...
['a', 'G', 'A', 'G']
['a', 'G', 'a', 'g']
['a', 'G', 'A', 'G']
['a', 'G', 'a', 'G']
['a', 'G', 'a', 'G']
#...
['a', 'G', 'a', 'G']
['a', 'G', 'A', 'G']
['a', 'G', 'a', 'g']
['a', 'G', 'A', 'G']
['a', 'G', 'a', 'G']
#...
['a', 'G', 'A', 'G']
['a', 'G', 'a', 'G']
['a', 'G', 'a', 'G']
# etc.
现在,这还不是全部,但正如您所见,虽然顺序不同,但有些出现是相同的,例如:
['a', 'G', 'A', 'G']
['a', 'A', 'G', 'G']
我更喜欢后一种排序,所以我想找到一种方法来打印所有在小写字母之前的大写字母组合,并且因为“a”在“g”之前,也是按字母顺序排列的。最终产品应类似于['AaGG', 'aaGg', etc]。我应该使用什么功能?
这是生成数据的代码。标记为“计数”的部分是我遇到的问题。
import itertools
from collections import Counter
parent1 = 'aaGG'
parent2 = 'AaGg'
f1 = []
f1_ = []
genotypes = []
b = []
genetics = []
g = []
idx = []
parent1 = list(itertools.combinations(parent1, 2))
del parent1[0]
del parent1[4]
parent2 = list(itertools.combinations(parent2, 2))
del parent2[0]
del parent2[4]
for x in parent1:
f1.append(''.join(x))
for x in parent2:
f1_.append(''.join(x))
y = list(itertools.product(f1, f1_))
for x in y:
genotypes.append(''.join(x))
break
genotypes = [
thingies[0][0] + thingies[1][0] + thingies[0][1] + thingies[1][1]
for thingies in zip(parent1, parent2)
] * 4
print 'F1', Counter(genotypes)
# Counting
for genotype in genotypes:
alleles = list(itertools.combinations(genotype,2))
del alleles[1]
del alleles[3]
for x in alleles:
g.append(''.join(x))
for idx in g:
if idx.lower().count("a") == idx.lower().count("g") == 1:
break
f2 = list(itertools.product(g, g))
for x in f2:
genetics.append(''.join(x))
for genes in genetics:
if genes.lower().count("a") == genes.lower().count("g") == 2:
genes = ''.join(genes)
print Counter(genes)
【问题讨论】:
-
您能否提供一个较小的样本输入及其预期输出?
-
@AshwiniChaudhary 我不是 100% 确定我明白你在问什么。你能解释一下吗