我正在迭代存储在数组中的数字列表,如果它们小于10,我想将它们存储在一个新数组中。这是我到目前为止所尝试过的。
num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
i []
for index in range(len(num)):
if num[index] < 10:
i = [num[index]]
for index in range(len(i)):
print i[index]
答案 0 :(得分:2)
num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
result = [x for x in num if x<10]