def purify(num):
for number in num:
if number%2 == 0:
number = list(str(number))
print(number)
purify([1,8,9,6,5,3,4,6,5,8,8,9,9,2,3,1,5])
o / p - [' 8'] [' 6'] [' 4'] [' 6'] [' 8'] [' 8'] [' 2']
我正在尝试从列表中删除所有奇数,我已将所有数字转换为字符串,但在打印后我得到多个列表。
答案 0 :(得分:2)
这是以同样的方式完成的:
num = [1,8,9,6,5,3,4,6,5,8,8,9,9,2,3,1,5]
newlist = [str(i) for i in num if i%2==0]
print(newlist)
它给出了这样的o / p:
['8', '6', '4', '6', '8', '8', '2']
答案 1 :(得分:2)
def purify(num):
even = []
for number in num:
if number%2 == 0:
even.append(str(number))
print("['{}']".format("']['".join(even)))
然后
>>> purify([1,8,9,6,5,3,4,6,5,8,8,9,9,2,3,1,5])
['8']['6']['4']['6']['8']['8']['2']
答案 2 :(得分:1)
def purify(num):
newlist=[]
for number in num:
if number%2 == 0:
number = str(number)
newlist.append(number)
print(newlist)
return newlist
试试这个。您需要一个列表来存储数字。输出:
['8', '6', '4', '6', '8', '8', '2']
答案 3 :(得分:0)
如果您上次的结果是奇数,那么您可以尝试:
ls=[1,8,9,6,5,3,4,6,5,8,8,9,9,2,3,1,5]
print(list(filter(lambda x:not x%2,ls)))
输出:
[8, 6, 4, 6, 8, 8, 2]
如果你想要字符串:
[['8'], ['6'], ['4'], ['6'], ['8'], ['8'], ['2']]