0
votes

Comment copier la chaîne de la liste dans la liste?

J'ai une liste xxx

i wnat à copier toutes les chaînes de la liste

comme ceci xxx avant > Xxx

mais quand j'utilise ce code xxx

i get xxx

Comment puis-je Changez mon code pour accomplir mon résultat?


0 commentaires

3 Réponses :


1
votes

J'utiliserais itheroTools.chain code> avec itheroTools.repeat code>: xxx pré>

sortie: p>

['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']


1 commentaires

Maintenant c'est une bonne réponse.



1
votes

sans utiliser itoTools, cela pourrait être fait avec une compréhension de liste imbriquée comme ci-dessous

list1=['a','b','c']

print([y for x in list1 for y in [x]*2])
# ['a', 'a', 'b', 'b', 'c', 'c']

print([y for x in list1 for y in [x]*3])
# ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']


0 commentaires

0
votes

Vous pouvez utiliser le second pour en boucle avec la fonction plage () : xxx


0 commentaires