栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

选择最大的奇数python

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

选择最大的奇数python

最好过滤数字,然后对它们进行排序。

numbers = [x, y, z]sorted_odd_nums = sorted((x for x in enumerate(numbers) if x[1]%2),    key = lambda x:x[1],    reverse=True)if not sorted_odd_nums:   # all numbers were even and filtered out.elif sorted_odd_nums[0][0] == 0:   # x is the biggest odd numberelif sorted_odd_nums[0][0] == 1:   # y is the biggest odd numberelif sorted_odd_nums[0][0] == 2:   # z is the biggest odd number

它能做什么:

enumerate(numbers)
返回一个序列
(index, item)
对。因为原来的名单是
[x, y,z]
,我们可以保持的轨道
x
y
z
甚至筛选和排序后。

(x for x in enumerate(numbers) if x[1]%2)
如果给定元组中的第二项不是偶数,则在枚举上方进行过滤。

sort( ... , key=lambda x:x[1], reverse=True)
使用第二索引项目的值(原始编号)以降序对过滤的项目进行排序。

用户输入

要从用户那里读取信息,最简单的方法是使用

raw_input
(py2)/
input
(py3k)。

number = int(raw_input('enter a number: '))

仅使用if语句

您将必须嵌套if语句。喜欢:

if x%2: # x is odd  if y%2: # y is odd    if z%2: #z is odd      if x>y and x>z: #x is the biggest odd number      elif y>z and y>x: #y is the biggest odd number      elif z>x and z>y: #z is the biggest odd number    else: #z is even      if x>y: #x is the biggest odd number      else: #y is the biggest odd number  else: #y is even    if z%2: #z is odd...


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号