1.将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1
# 将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1 list_num = [3, 7, 0, 5, 1, 8] list_num1 = [] for i in list_num: if i > 5: i = 0 list_num1.append(i) elif i == 5: i = i list_num1.append(i) else: list_num1.append(1) print(list_num1)
输出结果: