简体   繁体   English

如何用python中的另一个列表替换列表?

[英]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:有两种选择:

  1. gamewords = newgamewords[:]
  2. 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.

相关问题 如何将一个单词替换为列表python中的另一个单词? - How to replace a word to another word in a list python? Python - 如何用另一个列表中的字符串值替换列表中存储的字符串的值? - Python - How do you replace the values of strings stored in a list with the string values from another list? 如何在 Python 中按另一个列表的顺序替换列表项 - How to replace list items in order of another list in Python 如何插入和替换另一个列表中的单词列表或python中的字符串 - how to insert and replace a list of words in another list or a string in python 如何将python中的列表与另一个列表进行比较并将其替换为Nan? - How to compare a list in python with another list and replace it with Nan? Python:如何用另一个列表中的所有元素替换列表中的元素? - Python: how to replace an element in a list with all elements from another list? 我应该如何在一个列表中找到并替换另一个列表(python)? - How should i find in a list and replace in another list(python)? 如何在python中替换列表列表 - How to replace list of list in python 用Python中的另一个列表替换导入的常量列表 - Replace imported list of constants with another list in Python Python - 用另一个列表替换字符列表 - Python - Replace list of characters with another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM