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 []
首先,列表是什么?清单只是值的集合。例如,对于单个存储变量(例如整数),您可能会有类似的内容:
a = 1
很酷,但是如果您想拥有多个值怎么办?那就是我们使用列表的时候。阅读有关here的信息。# Iterate through the list for ___
其次,您需要了解什么是for循环,您可以阅读有关here的信息。
# Does this element belong in the resulting list? if ___
第三,了解是否语句herereturn ___
最后,你要还什么? (提示:查看您创建的[initialized“变量new_list
)希望这会有所帮助。祝你好运。
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 []