2016-08-29 1 views
0

Sagen Sie bitte eine Liste haben, zum Beispiel:Wie vergleiche ich zwei Listen und um 1 basierend auf der Anzahl der Spiele

first_list = ['a', 'b', 'c'] 

Und Sie haben die folgende Liste:

second_list = ['a', 'a b c', 'abc zyx', 'ab cc ac'] 

Wie würde Sie erstellen eine Funktion, die einfach die zweite Liste neu anordnet, basierend auf der Gesamtzahl, wie oft ein Element aus der gesamten ersten Liste mit einem Teil einer einzelnen Zeichenfolge aus der zweiten Liste übereinstimmt?


Weitere Klarheit:

  • in der zweiten Liste der 'a' string enthielte 1 Spiel
  • die 'abc' string 3 Spiele
  • das zweite Beispiel enthalten würde, Liste würde im wesentlichen in umgekehrter Reihenfolge am Ende, wenn die Funktion beendet hat

Mein Versuch:

first_list = ['a', 'b', 'c'] 
second_list = ['a', 'a b c', 'abc zyx', 'ab cc ac'] 

print second_list 

i = 0 
for keyword in first_list: 
    matches = 0 
    for s in second_list: 
     matches += s.count(keyword) 
     if matches > second_list[0].count(keyword): 
      popped = second_list.pop(i) 
      second_list.insert(0, popped) 

print second_list 
+0

Könnten Sie bitte mit uns Ihren eigenen Versuch teilen? – QuakeCore

+0

Ich habe meinen Versuch hinzugefügt @QuakeCore – Jarrod

Antwort

2

ähnliche Antwort:

first_list = ['a', 'b', 'c']  
second_list = ['a', 'a b c', 'abc zyx', 'ab cc ac'] 

#Find occurrences 
list_for_sorting = [] 
for string in second_list: 
    occurrences = 0 
    for item in first_list: 
     occurrences += string.count(item) 

    list_for_sorting.append((occurrences, string)) 

#Sort list 
sorted_by_occurrence = sorted(list_for_sorting, key=lambda tup: tup[0], reverse=True) 
final_list = [i[1] for i in sorted_by_occurrence] 
print(final_list) 

['ab cc ac', 'a b c', 'abc zyx', 'a'] 
1

Hier ist ein instabiler Weg, es zu tun:

>>> l1 = ['a', 'b', 'c'] 
>>> l2 = ['a', 'a b c', 'abc zyx', 'ab cc ac'] 
>>> [s for _, s in sorted(((sum(s2.count(s1) for s1 in l1), s2) for s2 in l2), reverse=True)] 
['ab cc ac', 'abc zyx', 'a b c', 'a'] 

Wenn stabile Sortierung erforderlich Sie enumerate nutzen könnten:

>>> l1 = ['a', 'b', 'c'] 
>>> l2 = ['a', 'a b c', 'ccc ccc', 'bb bb bb', 'aa aa aa'] 
>>> [x[-1] for x in sorted(((sum(s2.count(s1) for s1 in l1), -i, s2) for i, s2 in enumerate(l2)), reverse=True)] 
['ccc ccc', 'bb bb bb', 'aa aa aa', 'a b c', 'a'] 

Above erzeugt Tupel, in denen das zweite Element eine Zeichenfolge ist l2 und erste Element ist die Anzahl der Spiele von l1:

>>> tuples = [(sum(s2.count(s1) for s1 in l1), s2) for s2 in l2] 
>>> tuples 
[(1, 'a'), (3, 'a b c'), (3, 'abc zyx'), (6, 'ab cc ac')] 

Da nun die Tupel absteigender Reihenfolge sortiert werden:

>>> tuples = sorted(tuples, reverse=True) 
>>> tuples 
[(6, 'ab cc ac'), (3, 'abc zyx'), (3, 'a b c'), (1, 'a')] 

Und schließlich nur die Saiten gesetzt werden:

>>> [s for _, s in tuples] 
['ab cc ac', 'abc zyx', 'a b c', 'a'] 

in der zweiten Version Tupeln hat Reverse-Index-Stabilität zu gewährleisten:

>>> [(sum(s2.count(s1) for s1 in l1), -i, s2) for i, s2 in enumerate(l2)] 
[(1, 0, 'a'), (3, -1, 'a b c'), (3, -2, 'abc zyx'), (6, -3, 'ab cc ac')] 
+0

Warum ist das instabil? –

+0

Da die Zeichenfolge von 'l2' das zweite Element der Tupel ist, wird sortiert, wenn die Anzahl gleich ist, wird es verwendet, um die Reihenfolge zu bestimmen. – niemmi

+0

Ich habe eine Menge Schwierigkeiten, diesen Code zu verstehen, sorry – Jarrod

3

Der einfachste Ansatz wäre die key parameter of the sorted built-in function zu benutzen:

>>> sorted(second_list, key = lambda s: sum(s.count(x) for x in first_list), reverse=True) 
['ab cc ac', 'a b c', 'abc zyx', 'a'] 

Die Schlüsselfunktion aufgerufen wird einmal pro Element in der Liste sortiert werden. Dies ist immer noch ineffizient, da count lineare Zeit benötigt.

Verwandte Themen