如何检查此元素是否属于结果列表? [关闭]

问题描述 投票:-4回答:2
[请帮助我在下面的代码中填空。但是,我也需要解释。

def skip_elements(elements): # Initialize variables new_list = [] i = 0 # Iterate through the list for ___ # Does this element belong in the resulting list? if ___ # Add this element to the resulting list ___ # Increment i ___ return ___ print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g'] print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach'] print(skip_elements([])) # Should be []

python list for-loop
2个回答
-2
投票
我不会为您回答,因为这似乎是作业,如果我告诉您答案,您将不会学到任何东西。但我会给您一条生命线,并为您指明正确的方向。

首先,列表是什么?清单只是值的集合。例如,对于单个存储变量(例如整数),您可能会有类似的内容:

a = 1

很酷,但是如果您想拥有多个值怎么办?那就是我们使用列表的时候。阅读有关here的信息。

# Iterate through the list for ___
其次,您需要了解什么是for循环,您可以阅读有关here的信息。

# Does this element belong in the resulting list? if ___

第三,了解是否语句here

return ___
最后,你要还什么? (提示:查看您创建的[initialized“变量new_list

希望这会有所帮助。祝你好运。


-2
投票
def skip_elements(elements): # Initialize variables new_list = [] i = 0 # Iterate through the list for i in range(0,len(elements),2 ): # Does this element belong in the resulting list? # Add this element to the resulting list new_list.append(elements[i]) return new_list print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # Should be ['a', 'c', 'e', 'g'] print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach'] print(skip_elements([])) # Should be []
© www.soinside.com 2019 - 2024. All rights reserved.