python-3.x - 使用 Python 打印列表中的奇数和偶数
问题描述
从用户那里获取值并在 Python 中使用 List 打印奇数和偶数
解决方案
你想试试这个简化流程并让它变得更多的代码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} ')
推荐阅读
- c# - 无法打印 transform.parent.name
- php - 如何在产品表中显示用户名?API laravel 8 使用 eloquent
- python-3.x - sqlalchemy.exc.DataError:(psycopg2.errors.InvalidTextRepresentation)布尔类型的无效输入语法:“sdzf”
- reactjs - 我可以直接检查 react-final-form 是否显示特定字段的验证错误吗?
- optimization - SAS 帮助优化有限制的线性方程
- javascript - Javascript 值在输入字段值中不可见
- reactjs - 如何使用 redux 工具包 + redux thunk 创建多个异步操作?
- wordpress - 如何更改已经存在的 wordpress 网站中的颜色主题
- xcode - XCUITest 性能结果指标只有持续时间
- sql - 从非标准格式 SQL 转换 CSV 日期时遇到问题