Domanda

Ciao, sono nuovo nella programmazione e voglio imparare Python. Sto lavorando a un codice che dovrebbe restituire elementi più ridondanti in un elenco. Se ce ne sono più di 1, dovrebbe restituire tutto. Ex.

List = ['a','b','c','b','d','a'] #then it should return both a and b.
List = ['a','a','b','b','c','c','d'] #then it should return a b and c.
List = ['a','a','a','b','b','b','c','c','d','d','d'] #then it should return a b and d.

Nota: non sappiamo quale elemento sia più comune nell'elenco, quindi dobbiamo trovare l'elemento più comune e se ce ne sono più di uno dovrebbe restituire tutto. Se l'elenco ha numeri o altre stringhe come elementi, anche il codice deve funzionare

Non ho idea di come procedere. Posso usare un piccolo aiuto.

Ecco l'intero programma:

from collections import Counter

def redundant(List):
    c = Counter(List)
    maximum = c.most_common()[0][1]
    return [k for k, v in c.items()if v == maximum]

def find_kmers(DNA_STRING, k):
    length = len(DNA_STRING)
    a = 0
    List_1 = []
    string_1 = ""
    while a <= length - k:
        string_1 = DNA_STRING[a:a+k]
        List_1.append(string_1)
        a = a + 1
    redundant(List_1)

Questo programma dovrebbe prendere la stringa di DNA e la lunghezza di Kmer e trovare quali sono i Kemers di quella lunghezza che sono presenti in quella stringa di DNA.

Input del campione:

ACGTTGCATGTCGCATGATGCATGAGAGCT
4

Esempio di output:

CATG GCAT  
È stato utile?

Soluzione

Puoi usare collections.Counter:

from collections import Counter
def solve(lis):
    c = Counter(lis)
    mx = c.most_common()[0][1]
    #or mx = max(c.values())
    return [k for k, v in c.items() if v == mx]

print (solve(['a','b','c','b','d','a']))
print (solve(['a','a','b','b','c','c','d']))
print (solve(['a','a','a','b','b','b','c','c','d','d','d'] ))

Produzione:

['a', 'b']
['a', 'c', 'b']
['a', 'b', 'd']

Una versione leggermente diversa del codice sopra utilizzando itertools.takewhile:

from collections import Counter
from itertools import takewhile
def solve(lis):
    c = Counter(lis)
    mx = max(c.values())
    return [k for k, v in takewhile(lambda x: x[1]==mx, c.most_common())]

Altri suggerimenti

inputData = [['a','b','c','b','d','a'], ['a','a','b','b','c','c','d'], ['a','a','a','b','b','b','c','c','d','d','d'] ]
from collections import Counter
for myList in inputData:
    temp, result = -1, []
    for char, count in Counter(myList).most_common():
        if temp == -1: temp = count
        if temp == count: result.append(char)
        else: break
    print result

Produzione

['a', 'b']
['a', 'c', 'b']
['a', 'b', 'd']
>>> def maxs(L):
...   counts = collections.Counter(L)
...   maxCount = max(counts.values())
...   return [k for k,v in counts.items() if v==maxCount]
... 
>>> maxs(L)
['a', 'b']
>>> L = ['a','a','b','b','c','c','d']
>>> maxs(L)
['a', 'b', 'c']
>>> L = ['a','a','a','b','b','b','c','c','d','d','d']
>>> maxs(L)
['d', 'a', 'b']

Solo per il gusto di dare una soluzione non usando collections e utilizzando le comprensioni dell'elenco.

given_list = ['a','b','c','b','d','a']
redundant = [(each, given_list.count(each)) for each in set(given_list) if given_list.count(each) > 1]
count_max = max(redundant, key=lambda x: x[1])[1]
final_list = [char for char, count in redundant if count == count_max]

PS - Io stesso non ho usato Counters Eppure :( È ora di imparare!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top