opt=[]
opt=["opt3","opt2","opt7","opt6","opt1"]
for i in range(len(opt)):
print opt[i]
sortie pour ce qui précède estTri un tableau python
opt3,opt2,opt7,opt6,opt1
Comment trier le tableau ci-dessus dans l'ordre croissant ..
opt=[]
opt=["opt3","opt2","opt7","opt6","opt1"]
for i in range(len(opt)):
print opt[i]
sortie pour ce qui précède estTri un tableau python
opt3,opt2,opt7,opt6,opt1
Comment trier le tableau ci-dessus dans l'ordre croissant ..
print sorted(opt)
Cela dépend si vous voulez ou non un tri naturel (ce que je pense que vous faites) ou non.
Si vous utilisez sorted()
ou .sort()
vous obtiendrez:
>>> opt = ["opt3", "opt2", "opt7", "opt6", "opt1", "opt10", "opt11"]
>>> print sorted(opt)
['opt1', 'opt10', 'opt11', 'opt2', 'opt3', 'opt6', 'opt7']
Alors que vous voudrez probablement ['opt1', 'opt2', 'opt3', 'opt6', 'opt7', 'opt10', 'opt11']
.
Si oui, vous voulez regarder dans natural sorting (il existe diverses variations sur la fonction mentionnée dans cet article).
'pour i dans la plage (len (opt))' est unPythonic. Utilisez plutôt 'pour elt in opt' à la place. – katrielalex
Vous n'avez pas besoin de la première ligne ('opt = []'). –