[英]Skip_elements() function using enumerate() function
Try out the enumerate function for yourself in this quick exercise.在此快速练习中亲自尝试枚举 function。 Complete the skip_elements function to return every other element from the list, this time using the enumerate function to check if an element is on an even position or an odd position.
完成 skip_elements function 以返回列表中的所有其他元素,这次使用枚举 function 检查元素是在偶数 position 还是奇数 position 上。
def skip_elements(elements):
# code goes here
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']
I have no idea how to use enumerate for this.我不知道如何为此使用枚举。 Please help.
请帮忙。
def skip_elements(elements):
# code goes here
element = []
for i, e in enumerate(elements):
if i % 2 == 0:
element.append(e)
return element
-- OR - 或者
def skip_elements(elements):
# code goes here
element = [e for i, e in enumerate(elements) if i % 2 == 0]
return element
def skip_elements(elements):
# code goes here
elements = [v for i, v in enumerate(elements) if i % 2 == 0]
return elements
I hope this will help you我希望这能帮到您
def skip_elements(elements):
list=[]
for index,element in enumerate(elements):
if index%2==0:
list.append('{}'.format(element))
return list
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"]))
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']))
Here is my output:这是我的 output:
['a', 'c', 'e', 'g'] ['a', 'c', 'e', 'g']
['Orange', 'Strawberry', 'Peach'] ['橙色','草莓','桃子']
def skip_elements(elements):
# code goes here
list = []
for index,element in enumerate(elements):
if index % 2 == 0:
list.insert(index, element)
return 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']
def skip_elements(elements):
list=[]
# code goes here
for index,element in enumerate(elements):
if index%2==0:
list.append(element)
return 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']
def skip_elements(elements):
# Initialize variables
new_list = []
i = 0
# Iterate through the list
for i in range(len(elements)):
# Does this element belong in the resulting list?
if i % 2 == 0:
# Add this element to the resulting list
new_list.append(elements[i])
# Increment i
i += 1
return new_list
def skip_elements(elements): # Initialize variables new_list = [] i = 0 def skip_elements(elements): # 初始化变量 new_list = [] i = 0
# Iterate through the list
for i in range(len(elements)):
# Does this element belong in the resulting list?
if i % 2 == 0:
# 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']3 print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # Should be ['Orange', 'Strawberry', 'Peach'] print(skip_elements([])) # Should be [] print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) # 应该是 ['a', 'c', 'e' , 'g']3 print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # 应该是 ['Orange', 'Strawberry', 'Peach'] 打印(skip_elements([])) # 应该是 []
even_elements = []
for i, alpha in enumerate(elements):
if i % 2 == 0:
even_elements.append(alpha)
i += 1
return even_elements
def skip_elements(elements):
new_list = []
for index, element in enumerate(elements):
if index % 2 == 0:
new_list += [element]
index + 1
return new_list
def skip_elements(elements): # code goes here l1=[] for index,item in enumerate(elements): if (index%2==0): l1.append("{}".format(item)) return l1 def skip_elements(elements): # 代码在这里 l1=[] for index,item in enumerate(elements): if (index%2==0): l1.append("{}".format(item)) return l1
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(["a", "b", "c", "d", "e", "f", "g"])) # 应该是 ['a', 'c', 'e' , 'g'] print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # 应该是 ['Orange', 'Strawberry', 'Peach']
def skip_elements(elements): # creating a blank list list2=[] for ind, ele in enumerate(elements): if ind == 0 or ind % 2 == 0: list2.append(elements[ind]) return list2 def skip_elements(elements): # 创建一个空白列表 list2=[] for ind, ele in enumerate(elements): if ind == 0 or ind % 2 == 0: list2.append(elements[ind]) return list2
print(skip_elements(["a","b","c","d","e","f","g"])) # Should be ['a','c','e','g'] print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) # print(skip_elements(["a","b","c","d","e","f","g"])) # 应该是 ['a','c','e' ,'g'] print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach'])) #
Also, you can use this code:此外,您可以使用以下代码:
def skip_elements(elements):
for index, e in enumerate(elements):
if index % 2 == 0:
print("{} - {}".format(index + 1, e))
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']
Output:
1 - a
3 - c
5 - e
7 - g
None
1 - Orange
3 - Strawberry
5 - Peach
None
def skip_elements(elements):
# create an empty list to store values
new_list =[]
for index, word in enumerate(elements): #iterate over index & element of given list
if index % 2 == 0: #check if index is odd/even
new_list.append(word) #appending EVEN element to new_list
return new_list #return new list
print(skip_elements(["a", "b", "c", "d", "e", "f", "g"]))
print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']))
Give it a go. ✌给它一个 go。✌
def skip_elements(elements): new_list = [obj for indx, obj in enumerate(elements) if indx % 2 == 0] return new_list print(skip_elements(["a", "b", "c", "d", "e", "f", "g"])) print(skip_elements(['Orange', 'Pineapple', 'Strawberry', 'Kiwi', 'Peach']))
There's a shortcut too!也有捷径! XD
XD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.