Code: Alles auswählen
from sets import Set
def count_words3(liste):
out_liste = list(Set(liste))
nr_liste = [(w,liste.count(w)) for w in out_liste]
return out_liste, nr_liste, len(liste)
Code: Alles auswählen
from sets import Set
def count_words3(liste):
out_liste = list(Set(liste))
nr_liste = [(w,liste.count(w)) for w in out_liste]
return out_liste, nr_liste, len(liste)
Code: Alles auswählen
def count_words4(liste):
liste.sort()
first_pos = 0
current_pos = 0
out_liste = []
nr_liste = []
list_length = len(liste)
if not list_length: # nothing to do
return out_liste, nr_liste, list_length
word = liste[0]
for current_pos in xrange(1, list_length):
new_word = liste[current_pos]
if word != new_word:
last_pos = current_pos - 1
anzahl_vorkommen = last_pos - first_pos
out_liste.append(word)
nr_liste.append([word, anzahl_vorkommen + 1])
first_pos = current_pos
word = new_word
else:
last_pos = current_pos
anzahl_vorkommen = last_pos - first_pos
out_liste.append(word)
nr_liste.append([word, anzahl_vorkommen + 1])
return out_liste, nr_liste, list_length
Code: Alles auswählen
def deldoub1(liste):
res = []
tmp = []
for item in liste:
if not item[0] in tmp:
tmp.append(item[0])
res.append(item)
return res
Danke, eigentlich gar nicht so schwierigDookie hat geschrieben:Hi gast,
im Prinzip müsstest du beim obigen Script nur die Zeile mit dem if so ändern das word[0] mit new_word[0] verglichen wird, allerdings bleibt dann deine Sortierung nicht bestehen.
Hier eine spezialisierte Funktion:GrußCode: Alles auswählen
def deldoub1(liste): res = [] tmp = [] for item in liste: if not item[0] in tmp: tmp.append(item[0]) res.append(item) return res
Dookie