Heim  >  Fragen und Antworten  >  Hauptteil

Python – Problem mit allen möglichen Permutationen und Kombinationen

Im Moment versteht man darunter alle Kombinationsmethoden von Buchstaben in einer Zeichenfolge, wie folgt, eine gewalttätige und hässliche erschöpfende Methode. . . Ich würde gerne fragen, ob es eine bessere Methode in itertools gibt, aber keine davon entspricht meinen Wünschen. Danke!

base='ATCG'
list=[]
for i in base:
    for j in base:
        for k in base:
            for m in base:
                for l in base:
                    for n in base:
                        seq=i+j+k+m+l+n
                        list.append(seq)
print(len(set(list)))
4096
漂亮男人漂亮男人2712 Tage vor639

Antworte allen(3)Ich werde antworten

  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:51:14

    # coding: utf8
    from itertools import product
    base = 'ATCG'
    result = product(base, repeat=6)  # 因为内容太多, 所以返回生成器, 可以用list方法使其变成列表
    print(len(set(result)))
    
    
    # --- 结果 ----
    4096
    

    Antwort
    0
  • 怪我咯

    怪我咯2017-05-18 10:51:14

    import itertools
    len(list(itertools.product(base, repeat=6)))

    Antwort
    0
  • 仅有的幸福

    仅有的幸福2017-05-18 10:51:14

    from itertools import product
    print(list(map("".join, product("ATCG", repeat=6))))

    Antwort
    0
  • StornierenAntwort