Dato un corpus/testi come ad esempio:modo più veloce per l'ordinamento di un dizionario corpus in un OrderedDict - pitone
Resumption of the session
I declare resumed the session of the European Parliament adjourned on Friday 17 December 1999 , and I would like once again to wish you a happy new year in the hope that you enjoyed a pleasant festive period .
Although , as you will have seen , the dreaded ' millennium bug ' failed to materialise , still the people in a number of countries suffered a series of natural disasters that truly were dreadful .
You have requested a debate on this subject in the course of the next few days , during this part @[email protected] session .
In the meantime , I should like to observe a minute ' s silence , as a number of Members have requested , on behalf of all the victims concerned , particularly those of the terrible storms , in the various countries of the European Union .
potrebbe semplicemente fare questo per ottenere un dizionario con le frequenze di parole:
>>> word_freq = Counter()
>>> for line in text.split('\n'):
... for word in line.split():
... word_freq[word]+=1
...
ma se l'obiettivo è quello di realizzare un dizionario ordinato dal più alto al più basso di frequenza, dovrò fare questo:
>>> from collections import OrderedDict
>>> sorted_word_freq = OrderedDict()
>>> for word, freq in word_freq.most_common():
... sorted_word_freq[word] = freq
...
Immaginate di avere 1 miliardo di chiavi nell'oggetto Counter
, l'iterazione attraverso lo most_common()
avrebbe una complessità di passare attraverso un corpus (istanze non univoche) una volta e il vocabolario (chiave univoca).
Nota: La Counter.most_common()
chiamerebbero un annuncio-hoc sorted()
, vedere https://hg.python.org/cpython/file/e38470b49d3c/Lib/collections.py#l472
Detto questo, ho visto il seguente codice che utilizza numpy.argsort()
:
>>> import numpy as np
>>> words = word_freq.keys()
>>> freqs = word_freq.values()
>>> sorted_word_index = np.argsort(freqs) # lowest to highest
>>> sorted_word_freq_with_numpy = OrderedDict()
>>> for idx in reversed(sorted_word_index):
... sorted_word_freq_with_numpy[words[idx]] = freqs[idx]
...
che è più veloce?
C'è qualche altro modo più veloce per ottenere tale OrderedDict
da un Counter
?
diverso da OrderedDict
, ci sono altri oggetti Python che ottengono la stessa coppia chiave-valore ordinata?
Supporre che la memoria non sia un problema. Dato 120 GB di RAM, non ci dovrebbe essere molto problema per mantenere 1 miliardo di coppie valore-chiave giusto? Assumi una media di 20 caratteri per chiave per 1 miliardo di chiavi e un singolo intero per ogni valore.
memoria potrebbe anche essere un problema così come la velocità. Immagino che una sorta di unire sort sarebbe meglio su entrambi i fronti. C'è qualche discussione sull'argomento migliore in http://stackoverflow.com/questions/1180240/best-way-to-sort-1m-records-in-python. –
presume che la memoria non sia un problema. – alvas
Forse un trilione di coppie valore-chiave non occuperebbe quella quantità di memoria, giusto? – alvas