簡體   English   中英

python 3.2-使用遞歸查找列表中的第二小數字

[英]python 3.2 - find second smallest number in a list using recursion

因此,我需要使用遞歸來找到整數列表中的第二個最小數字,但是我一生都無法想出一種方法來做到這一點。 我可以使用以下方法找到最小的數字:

def smallest(int_list):

    if(len(int_list) == 1):
        return int_list[0]
    else:
        a = smallest(int_list[1:])
        b = int_list[0]

        if(a <= b):
            return a
        else:
            return b

誰能指出我正確的方向?

這是一個簡短的實現,它不使用min()sorted() 當列表中有重復值時,它也適用。

def ss(e):
    if len(e)==2 and e[0]<=e[1]:return e[1]
    return ss(e[:-1]) if e[0]<=e[-1]>=e[1] else ss([e[-1]]+e[:-1])

print("The selected value was:", ss([5, 4, 3, 2, 1]))

這應該工作。 get_smallest_two_items()返回具有輸入的3個元素中最小2個元素的元組。 實施它應該是微不足道的。 另請注意,這假設列表的最小長度為2。

def smallest(int_list):
   smallest_two = smallest_helper(int_list)
   return smallest_two[0] if smallest_two[0]<=smallest_two[1] else smallest_two[1]

def smallest_helper(int_list):

  if(len(int_list) == 2):
    return (int_list[0],int_list[1])
  else:
    a_b = smallest(int_list[1:])
    a = a_b[0]
    b = a_b[1]
    c = int_list[0]

    return get_smallest_two_items(a,b,c)

比我的評論更簡單的例子:

def find_nth_smallest(n, int_list):
    smallest = min(int_list)
    if n <= 1:
        return smallest
    int_list = [v for v in int_list if v != smallest]
    if int_list:
        return max(find_nth_smallest(n - 1, int_list), smallest)
    return None

例:

Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from kthTerm import find_nth_smallest
>>> import random
>>> random.seed()
>>> int_list = [random.randint(1, 100) for _ in range(20)]
>>> int_list
[68, 50, 6, 36, 98, 15, 81, 36, 13, 71, 76, 77, 69, 75, 79, 53, 26, 25, 18, 62]
>>> find_nth_smallest(2, int_list)
13
>>> sorted(int_list)
[6, 13, 15, 18, 25, 26, 36, 36, 50, 53, 62, 68, 69, 71, 75, 76, 77, 79, 81, 98]

這里:

def r_sort(a_list, ind):
    def rf(list_to_be_sorted, list_already_sorted):
        li = []
        if len(list_to_be_sorted) == 0:
            return list_already_sorted
        else:    
            x = min(list_to_be_sorted)    
            list_to_be_sorted.remove(x)
            li.append(x)
            return rf(list_to_be_sorted, list_already_sorted + li)
    return rf(a_list, [])[ind]

>>> r_sort([1,10,9,5,55,3,2], 1)  
2

這是一種不會使大型列表崩潰的方法。 最好手動管理搜索端點,而不要使用引入復制的切片。

import random

def min2(xs):
    if len(xs) < 3: return xs
    n = len(xs) // 2
    return sorted(min2(xs[:n]) + [xs[n]] + min2(xs[n+1:]))[:2]

tk = range(100000)
random.shuffle(tk)
print min2(tk)

這是一種純遞歸方法。 您需要返回元組中的第一個最小元素和第二個最小元素,依此類推。

def smallest(int_list):

    if(len(int_list) == 2):
        if (int_list[0] >= int_list[1]):
            return (int_list[0],int_list[1])
        else:
            return (int_list[1],int_list[0])
    else:
        first_smallest,second_smallest = smallest(int_list[1:])
        current_elem = int_list[0]
        if(second_smallest <= current_elem):
            return (second_smallest,current_elem)
        else:
            if (current_elem<=first_smallest):
               return (current_elem,first_smallest)
            else:
               return (first_smallest,second_smallest)



if __name__ == "__main__":    
    small = smallest([1,2,3,4])
    print small[1]
    //output 2 

def second_smallest(my_list):

if len(my_list) == 2:
    return my_list[0] if my_list[0] > my_list[1] else my_list[1]

else:
    sec_least = second_smallest(my_list[1:])
    return sec_least if sec_least < my_list[0] else my_list[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM