在python列表中找到特定数字的最小倍数
想象一下,您有一个数字列表,您想找到符合特定条件的数字索引(例如3)。然后,我们希望拥有最小的索引(满足上述条件)是一定数量的倍数(这里4)。以下可能是一个好的开始。您将为其余的代码提供什么建议?
a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
输出= [i,i,x in Enumerate(a)如果x> 3]
打印(输出) '''
Imagine that you have a list of numbers and you would like to find the index of numbers meeting a certain condition (say > 3). We would then like to have the smallest index (meeting the above condition) being a multiple of a certain number (here 4). Below might be a good start. What would you suggest for the rest of the code?
a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
output = [i for i, x in enumerate(a) if x > 3]
print(output)
'''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要查找Minumim索引,则可以使用
min()
函数:If you want to find the minumim index, you can use the
min()
function:这些索引已经通过上面给出的代码获得。您需要做的就是找到
b
可除外的最小索引。您可以如下实现:The indices are already obtained by the code given above. All you need to do is find the smallest index divisible by
b
. You can achieve it as follows:这个问题与列表无关:
This question is unrelated to lists: