[Risolto] Python - sort particolare
[Risolto] Python - sort particolare
Se io ho i seguenti nomi file:
n-3.txt
n-11.txt
n-1.txt
n-2.txt
e faccio un sort, ottengo:
n-1.txt
n-11.txt
n-2.txt
n-3.txt
Ma se li visualizzo con nautilus o altri, la lista è così:
n-1.txt
n-2.txt
n-3.txt
n-11.txt
Esiste un sort che dia lo stesso risultato di nautilus? (ossia metterli in ordine di numero)
grazie
n-3.txt
n-11.txt
n-1.txt
n-2.txt
e faccio un sort, ottengo:
n-1.txt
n-11.txt
n-2.txt
n-3.txt
Ma se li visualizzo con nautilus o altri, la lista è così:
n-1.txt
n-2.txt
n-3.txt
n-11.txt
Esiste un sort che dia lo stesso risultato di nautilus? (ossia metterli in ordine di numero)
grazie
Ultima modifica di maresama il sabato 24 maggio 2014, 20:00, modificato 1 volta in totale.
- vaeVictis
- Imperturbabile Insigne

- Messaggi: 4703
- Iscrizione: venerdì 27 luglio 2012, 17:58
- Desktop: Gnome
- Distribuzione: Ubuntu 20.04 64bit
Re: Python - sort particolare
Tu per ora che metodi hai usato?
Spiega così vediamo di risolvere. Non è difficile
Anche perché se fai una ricerca con Google in cui metti "python sort string with numbers" ottieni tra i primi risultati delle indicazioni abbastanza esaustive.
Spiega così vediamo di risolvere. Non è difficile
Anche perché se fai una ricerca con Google in cui metti "python sort string with numbers" ottieni tra i primi risultati delle indicazioni abbastanza esaustive.
Pirates arrrrrrrrrrr awesome!!!
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
Re: Python - sort particolare
In effetti su internet ho fatto ricerche sbagliate. Mi ha ingannato il fatto che le chiavi non sono numeriche, ma solo parzialmente.
Grazie a te ho trovato quello che va bene.
Ho provato ed è ok. Grazie.
Ciao.
Grazie a te ho trovato quello che va bene.
Ho provato ed è ok. Grazie.
Ciao.
- vaeVictis
- Imperturbabile Insigne

- Messaggi: 4703
- Iscrizione: venerdì 27 luglio 2012, 17:58
- Desktop: Gnome
- Distribuzione: Ubuntu 20.04 64bit
Re: Python - sort particolare
Cortesemente, potresti postare il codice con cui hai risolto e modificare il titolo del primo messaggio della discussione?
(a beneficio dei futuri lettori)
Ciao
(a beneficio dei futuri lettori)
Ciao
Pirates arrrrrrrrrrr awesome!!!
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
Re: Python - sort particolare
Ho usato questo:
Però non ho risolto un secondo problema:
Oltre la chiave devo avere anche un puntatore abbinato: [key,puntatore]
e con quanto indicato sopra non si può usare una lista come chiave
(il puntatore è un'indice ad un'altra lista. Con un sort normale non ci sarebbe alcun problema)
Codice: Seleziona tutto
import re
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ atoi(c) for c in re.split('(\d+)', text) ]
f=["n-1.txt","n-11.txt","n-2.txt","n-3.txt"]
f.sort(key=natural_keys)
print(f)
#risultato: ['n-1.txt', 'n-2.txt', 'n-3.txt', 'n-11.txt']
Oltre la chiave devo avere anche un puntatore abbinato: [key,puntatore]
e con quanto indicato sopra non si può usare una lista come chiave
(il puntatore è un'indice ad un'altra lista. Con un sort normale non ci sarebbe alcun problema)
Re: Python - sort particolare
Fose basta modificare:
Appena ho tempo verificherò.....
Codice: Seleziona tutto
def natural_keys(text):
text=text[0] # riga aggiunta
return [ atoi(c) for c in re.split('(\d+)', text) ]
- vaeVictis
- Imperturbabile Insigne

- Messaggi: 4703
- Iscrizione: venerdì 27 luglio 2012, 17:58
- Desktop: Gnome
- Distribuzione: Ubuntu 20.04 64bit
Re: Python - sort particolare
Scusami ma il secondo problema non l'ho capito.
Potresti spiegarmi meglio?
Potresti spiegarmi meglio?
Pirates arrrrrrrrrrr awesome!!!
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
Re: Python - sort particolare
in pratica ho una lista con le chiavi. Dopo il sort altre tabelle devono essere nell'ordine in cui la lista delle chiavi. Pertanto, con un normale sort si fa una lista con [chiave,indice]. Dopo il sort, l'indice indica quale elemento deve essere preso dalle altre tabelle.
es liista key: [["f",0],["e",1],["a",2],["d",3],["c",4],["b",5]]
lista abbinata ["000","111","222","333","444","555"]
dopo il sort:
es liista key: [["a",2],["b",5],["c",4],["d",3],["e",1],["f",0]]
il secondo elemento permetterà di leggere la lista abbinata nello stesso ordine. (2,5,4,3.1, 0)
Spero di essere riuscito a spiegarmi (e di non aver sbagliato a scrivere l'esempio!).
es liista key: [["f",0],["e",1],["a",2],["d",3],["c",4],["b",5]]
lista abbinata ["000","111","222","333","444","555"]
dopo il sort:
es liista key: [["a",2],["b",5],["c",4],["d",3],["e",1],["f",0]]
il secondo elemento permetterà di leggere la lista abbinata nello stesso ordine. (2,5,4,3.1, 0)
Spero di essere riuscito a spiegarmi (e di non aver sbagliato a scrivere l'esempio!).
- vaeVictis
- Imperturbabile Insigne

- Messaggi: 4703
- Iscrizione: venerdì 27 luglio 2012, 17:58
- Desktop: Gnome
- Distribuzione: Ubuntu 20.04 64bit
Re: Python - sort particolare
Ti sei spiegato 
Sei riuscito a risolvere? O rimane il problema?
Sei riuscito a risolvere? O rimane il problema?
Pirates arrrrrrrrrrr awesome!!!
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
«I fear not the man who has practiced 10000 kicks once,
but I fear the man who has practiced one kick 10000 times.»
Re: Python - sort particolare
Risolto. Grazie.
Chi c’è in linea
Visualizzano questa sezione: 0 utenti iscritti e 3 ospiti
