I am new with Python so sorry about my ugly code... I need to create a program that allows me to rearange a list using a condition from another list. I have a list with first names and a list with last names (sorting is important because each first name corresponds to eahc last names). The idea is to create a new list randomly picked up from the first names ones, the condition being that two last names cannot be side by side (and the last and first item of last names must be different)... not sure I am very clear here, hope you will get it by looking at my code.
Here is my code below (it works when removing the while loop and repeating it manually...). I am sure there is a way to do it very simply...
import random
first_names = ['an','bn','ji','au','jo','ki','ko','bo','mi','li']
last_names = ['A','A','A','R','R','R','C','C','C','C']
resultats =[]
resultats_noms = []
#picking the first item
tirage = random.choice(list(enumerate(first_names)))
index = tirage[0]
pren = tirage[1]
resultats.append(pren)
resultats_noms.append(last_names[index])
first_names.remove(pren)
last_names.pop(index)
while len(first_names) > 0 :
if last_names[index] != resultats_noms[len(last_names)-1]:
resultats.append(pren)
resultats_noms.append(last_names[index])
first_names.remove(pren)
last_names.pop(index)
print(resultats)
print(resultats_noms)
if len(resultats_noms) == 10:
print('liste completed')
if len(first_names)>9 and last_names[index] == resultats_noms[len(resultats_noms)-1]:
print('bloqué, faut recommencer',len(resultats_noms),'prenoms sur 10')
Any help on that?
Mel
Aucun commentaire:
Enregistrer un commentaire