[英]How do replace a list with another list in python?
I am making a hangman game and I want to be able to replace the list of original words with a list of new words typed in by the user.我正在制作一个刽子手游戏,我希望能够用用户输入的新单词列表替换原始单词列表。 At the minute my code is this:
目前我的代码是这样的:
gamewords[:] = newgamewords[:]
But this does not seem to work...但这似乎不起作用......
The original list is this:原来的名单是这样的:
gamewords= ['blue','violet','red','orange','fuchsia','cyan','magenta','azure','black','turquoise','pink','scarlet']
A word is then chosen for the list randomly然后随机选择一个词作为列表
word=gamewords[random.randint(0,len(gamewords)-1)]
i want to change it so that the word is chosen from the new list, how do i do this?我想更改它以便从新列表中选择单词,我该怎么做?
You probably meant to do this:您可能打算这样做:
gamewords = newgamewords[:] # i.e. copy newgamewords
Another alternative would be另一种选择是
gamewords = list(newgamewords)
I find the latter more readable.我发现后者更具可读性。
Note that when you 'copy' a list like both of these approaches do, changes to the new copied list will not effect the original.请注意,当您像这两种方法一样“复制”列表时,对新复制列表的更改不会影响原始列表。 If you simply assigned
newgamewords
to gamewords
(ie gamewords = newgamewords
), then changes to gamewords
would effect newgamewords
.如果您只是将
newgamewords
分配给gamewords
(即gamewords = newgamewords
),那么对gamewords
更改将影响newgamewords
。
Relevant Documentation相关文件
I'm not sure what you exactly want.我不确定你到底想要什么。 There are two options:
有两种选择:
gamewords = newgamewords[:]
gamewords = newgamewords
The difference is that the first option copies the elements of newgamewords and assigns it to gamewords .不同之处在于第一个选项复制newgamewords的元素并将其分配给gamewords 。 The second option just assigns a reference of newgamewords to gamewords .
第二个选项只是将newgamewords的引用分配给gamewords 。 Using the second version, you would change the original newgamewords -list if you changed gamewords .
使用第二个版本,如果您更改了gamewords ,您将更改原始的newgamewords -list 。
Because you didn't give more of your source code I can't decide which will work properly for you, you have to figure it out yourself.因为你没有提供更多的源代码,我无法决定哪些适合你,你必须自己弄清楚。
The obvious choice of functions to use to select one, or a set number of items randomly from a larger list would be random.choice()
or random.choices()
.用于从较大列表中随机选择一个或一组项目的函数的明显选择是
random.choice()
或random.choices()
。
>>> gamewords= ['blue','violet','red','orange','fuchsia','cyan','magenta','azure','black','turquoise','pink','scarlet']
>>> random.choice(gamewords)
'turquoise'
>>> random.choice(gamewords)
'orange'
>>> random.choice(gamewords)
'cyan'
>>> random.choices(gamewords, k=3)
['fuchsia', 'orange', 'red']
>>> random.choices(gamewords, k=2)
['turquoise', 'black']
>>>
Not sure if you were able to find the answer to this but I had a similar issue and I resolved it using this method:不确定您是否能够找到答案,但我遇到了类似的问题,我使用以下方法解决了它:
for x, y in zip(original_col, np.arange(0, len(original_col), 1)):
df['Term Ldesc'] = df['Term Ldesc'].replace(x, y)
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.