如何重新排列列表中单词的字母?

时间:2019-01-10 22:46:15

标签: python

如何重新排列单词中的字母(单词在列表中。)我正在尝试编写一个程序,在其中创建列表并从列表中随机选择一个单词,然后必须重新排列单词的字母并列出包含这些字母的列表,然后我必须告诉用户第一个列表的主题并向他们显示加扰的单词,然后他们必须根据我给他们的主题和他们所拥有的字母来猜测单词到目前为止,这是我的代码,除了重新排列字母并为它们列出列表之外,我什么都没问题:

import random
mysports= ["swimming" , "basketball" , "soccer" , "cross country" , "football" , "cross-country skiing"]
a = (random.choice(mysports)) 
if a == mysports[0]:
  #code that is supposed to re arrange the letters
elif a == mysports[1]:
  #code that is supposed to re arrange the letters
elif a == mysports[2]:
  #code that is supposed to re arrange the letters
elif a == mysports[3]:
  #code that is supposed to re arrange the letters
elif a == mysports[4]:
  #code that is supposed to re arrange the letters
elif a == mysports[5]:
  #code that is supposed to re arrange the letters

1 个答案:

答案 0 :(得分:0)

使用random.shuffle随机重新排列列表:

import random
word = list('abracadabra')
random.shuffle(word)

请注意,shuffle会就地更改列表。它不会返回列表的修改后的副本;它将更改通过的实际列表。