18、for、while循環(huán)遍歷列表元素
list_1=[1,2,3,4,5,6,7,8,9,10]
list_2=[]
list_3=[]
index=0
i=0
j=0
while index<len(list_1):
? ?element=list_1[index]
? ?if element%2 == 0:
? ? ? ?list_2.insert(i,element)
? ? ? ?i+=1
? ?index+=1
print(f"通過(guò)while循環(huán),從列表:{list_1}中取出偶數(shù),組成新列表{list_2}")
for element in list_1:
? ?if element%2 == 0:
? ? ? ?list_3.insert(j,element)
? ? ? ?j+=1
print(f"通過(guò)for循環(huán),從列表:{list_1}中取出偶數(shù),組成新列表{list_3}")
標(biāo)簽: