-1

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)
4

3 に答える 3

0

並べ替え機能を試してみてください。私が意味するものの例:

parent1 = "absdksakjcvjvugoh"
parent1sorted = list(parent1)
parent1sorted.sort()
print (parent1sorted)

結果は次のようになります: ['a', 'a', 'b', 'c', 'd', 'g', 'h', 'j', 'j', 'k', 'k ', 'o', 's', 's', 'u', 'v', 'v']

これは役に立ちますか?

tldr: 文字列をリストに変換、リストをソート

于 2015-07-23T14:53:54.137 に答える