pop的运用

pop():弹出列表最后一个元素

练习题:

num_list =  [12, 45, 34,13, 100, 24, 56, 74, 109]
one_list = []
two_list = []
three_list = []
while len(num_list) > 0:
    num_pop = num_list.pop()
    if num_pop % 3 == 0:
        one_list.append(num_pop)
    elif num_pop % 3 == 1:
        two_list.append(num_pop)
    else:
        three_list.append(num_pop)
print("被3整除的有:",one_list)
print("取余为1的有:",two_list)
print("取余为2的有:",three_list)

运行结果:

被3整除的有: [24, 45, 12]
取余为1的有: [109, 100, 13, 34]
取余为2的有: [74, 56]

 

posted @ 2019-06-10 18:29  持刀文人  阅读(209)  评论(0编辑  收藏  举报