简体   繁体   English

用python查找列表的3个最大元素

[英]Find 3 maximum elements of list with python

How can I search for the three maximum elements of a list and replace it at same index with its result when divided by 2.如何搜索列表的三个最大元素,并在除以 2 时将其替换为相同索引处的结果。

Please what am I doing wrong:请问我做错了什么:

input is: 2 5 8 19 1 15 7 20 11输入是: 2 5 8 19 1 15 7 20 11
output should be : 2 5 8 9.5 1 7.5 7 10 11输出应为: 2 5 8 9.5 1 7.5 7 10 11

Index out of range is the result displayed索引超出范围是显示的结果

def numberInput(line):
  lineInput = [int(i) for i in line.split()]

  min1 = min(lineInput)

  for j in range(len(lineInput)):

     if lineInput[j]>min1 and lineInput[j] > lineInput[j+1]:

           max1 = lineInput[j]/float(2)
     else:
           max1 = lineInput[j]/float(2)
           lineInput[j] = max1

     lineInput[j] = max1
  return(lineInput)

number = '2 5 8 19 1 15 7 20 11' 
print(numberInput(number))

If the order of list isn't important, you can simply sort the list in descending order and then replace the first 3 elements as如果列表的顺序不重要,您可以简单地按降序对列表进行排序,然后将前 3 个元素替换为

a = [2, 5, 8, 19, 1, 15, 7, 20, 11]
a.sort(reverse = True)
a[0] = a[0] / 2
a[1] = a[1] / 2
a[2] = a[2] / 2

Output输出

[10.0, 9.5, 7.5, 11, 8, 7, 5, 2, 1]

If the order is important,如果顺序很重要,

import heapq
largest = heapq.nlargest(3, a)
for i in range(len(a)):
    if a[i] in largest:
        a[i] = a[i] / 2    

Output输出

[2, 5, 8, 9.5, 1, 7.5, 7, 10.0, 11]

heapq.nlargest() is a function of heapq which can give you n largest numbers from a list. heapq.nlargest()是 heapq 的一个函数,它可以从列表中给你 n 个最大的数字。 Since lists in Python do not have a replace function, the list had to be traversed once, and then replaced manually.由于 Python 中的列表没有替换功能,因此必须遍历列表一次,然后手动替换。 Hope this resolves your issue.希望这能解决您的问题。

Why wouldn't you just walk through the list once (it maybe a bit longer code, but definitely efficient)你为什么不只浏览一次列表(它可能代码有点长,但绝对有效)

def numberInput(line):
    lineInput = [int(i) for i in line.split()]
    n = len(lineInput)
    if n <= 3:
        return [i / 2 for i in lineInput]
    max_pairs = [(lineInput[i], i) for i in range(3)] # keep track of 3 max's and their indices
    max_pairs.sort(key = lambda x: -x) # sort in descending order
    for i in range(3, n):
        if lineInput[i] >= max_pairs[0][0]: # greater than the largest element
            max_pairs = [(lineInput[i], i)] + max_pairs[:2]
        elif lineInput[i] >= max_pairs[1][0]: # greater than second element
            max_pairs = [max_pairs[0], (lineInput[i], i), max_pairs[1]]
        elif lineInput[i] >= max_pairs[2][0]: # greater than third element
            max_pairs = max_pairs[:2] + [(lineInput[i], i)]
     for pair in max_pairs:
         lineInput[pair[1]] = lineInput[pair[0]] / 2
     return lineInput

Explanation: max_pairs is a set of three tuples, containing the maximum three elements and their indices说明: max_pairs是三个元组的集合,包含最多三个元素及其索引

Note: I think the above is easiest to understand, but you can do it in a loop if you don't like all those ifs注意:我认为以上最容易理解,但如果你不喜欢所有这些 ifs,你可以循环执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM