Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从列表中的特定范围中选择随机元素-python_Python_List_Copying - Fatal编程技术网

从列表中的特定范围中选择随机元素-python

从列表中的特定范围中选择随机元素-python,python,list,copying,Python,List,Copying,我正在创建一个刽子手游戏,其中我有一个包含5个秘密单词的列表,以及从文本文件中读取的每个单词的提示: list = ['word1', 'hint1', 'word2', 'hint2','word3', 'hint3','word4', 'hint4','word5', 'hint5'] 我需要创建两个单独的列表,分别只包含秘密单词和提示。我该怎么做呢 预期结果: words = ['word1','word2','word3','word4','word5'] hints = ['hin

我正在创建一个刽子手游戏,其中我有一个包含5个秘密单词的列表,以及从文本文件中读取的每个单词的提示:

 list = ['word1', 'hint1', 'word2', 'hint2','word3', 'hint3','word4', 'hint4','word5', 'hint5']
我需要创建两个单独的列表,分别只包含秘密单词和提示。我该怎么做呢

预期结果:

words = ['word1','word2','word3','word4','word5']
hints = ['hint1','hint2','hint3','hint4','hint5']

使用切片和步骤表示法生成两个列表,
l[::2]
将从第一个元素开始执行步骤2元素,而
l[1::2]
也将从第二个元素开始执行步骤2元素:

In [145]:

l = ['word1', 'hint1', 'word2', 'hint2','word3', 'hint3','word4', 'hint4','word5', 'hint5']
words = l[::2]
hints = l[1::2]
print(words)
print(hints)
['word1', 'word2', 'word3', 'word4', 'word5']
['hint1', 'hint2', 'hint3', 'hint4', 'hint5']

我不知道切片中可能有4种东西。我以为这只是开始、结束和步骤。从2.3开始就使用python:@Ogen:它们都没有四个,它们都有三个
l[1::2]
l[start:(end):step]
start=1
end=None
,以及
step=2
@Ogen这只是开始、结束和步骤-第四步是什么?@EdChum这正是我要找的。非常感谢。