首页 > 解决方案 > 使用 Python 打印列表中的奇数和偶数

问题描述

从用户那里获取值并在 Python 中使用 List 打印奇数和偶数

标签: python-3.xlist

解决方案


你想试试这个简化流程并让它变得更多的代码Pythonic

nums = map(int, input("Input some numbers: ").split())  # get all numbers in one shot 

results = [[], []]      # declare the results to store evens and odds

for n in nums:          # put each number in their own list or bucket. one shot.
    results[n % 2].append(n)
    

print(results)

evens, odds = results             # unpacking these 2 lists

print(f' evens list: {evens}' )   # confirm the results is ok
print(f' odds list: {odds} ')

推荐阅读