2017-05-26 27 views
3
def print_most_numbers_occurrences(numbers_str): 
    number_list = list(numbers_str) 
    for i in number_list: 
     i=max(number_list,key=number_list.count) 
    print(i) 

def test_print_most_numbers_occurrences(): 
    print_most_numbers_occurrences('2 3 40 1 5 4 3 3 9 9') 
    print_most_numbers_occurrences('9 30 3 9 3 1 4') 
    print_most_numbers_occurrences('19 30 13 4 9 3 1 4') 


def main(): 
    print(test_print_most_numbers_occurrences()) 


main() 

輸出發現大多數的數字出現在列表中

None 

它的工作原理,當我嘗試這樣:

>>> lst = [1, 2, 45, 55, 5, 4, 4, 4, 4, 4, 545, 56, 6, 7, 67] 
>>> max(lst,key=lst.count) 
4 

我想,以確定發生的次數最多的號碼。我不確定我的第一個def函數的錯誤。

+2

擺脫循環。並且做'numbers_str。split()'而不是'list(numbers_str)' – abccd

+0

注意所有的答案(上面的評論暗示):沒有參數的'.split()'默認處理雙空格 –

回答

3

首先,您需要正確地分析您的輸入。由於其中一個輸入具有雙倍空間,因此您不能僅使用split(" ")

其次,你不需要循環,因爲max爲你做循環。

def print_most_numbers_occurrences(numbers_str): 
    number_list = [int(x) for x in numbers_str.split()] 
    i=max(number_list,key=number_list.count) 
    print(i) 

既然你是我的循環冒昧地認爲你試圖解決的情況下多個號碼可能具有相同的事件(例如:'2 3 40 1 5 4 3 3 9 9 9')。在這種情況下,下面的代碼會得到所有的最大值:

def print_most_numbers_occurrences(numbers_str): 
    print(numbers_str.split(" ")) 
    number_list = [int(x) for x in numbers_str.split()] 
    most_occurances = [] 
    for i in set(number_list): 
     occurances = number_list.count(i) 
     if len(most_occurances) == 0: 
      most_occurances.append(i) 
     elif most_occurances[0] < occurances: 
      most_occurances = [i] 
     elif most_occurances[0] == occurances: 
      most_occurances.append(i) 
    print(most_occurances) 

下面是一個使用稍微複雜的代碼更簡潔的版本:

def print_most_numbers_occurrences(numbers_str): 
    number_list = [int(x) for x in numbers_str.split()] 
    result = {i : number_list.count(i) for i in set(number_list)} 
    highest = max(result.values()) 
    most_occurances = [k for k, v in result.items() if v == highest] 
    print(most_occurances) 

如果你需要高效的代碼,這是明智的使用collectionsCounter

from collections import Counter 

def print_most_numbers_occurrences(numbers_str): 
    number_list = [int(x) for x in numbers_str.split()] 
    result = Counter(number_list) 
    highest = max(result.values()) 
    most_occurances = [k for k, v in result.items() if v == highest] 
    print(most_occurances if len(most_occurances) > 1 else most_occurances[0]) 
+0

輸出應該字符串是9和3('9 30 3 9 3 1 4') – Kee

+0

是。這是一個清單:'[9,3]'。你想用其他格式嗎?我指的是第二個和第三個發佈的方法(第一個只獲得一個最大值)。 – Darkstarone

+0

如何刪除方括號中的第三個方法的輸出? – Kee

2

使用.split(」「),而不是列表()

string = '9 30 3 9 3 1 4' 
lst = map(int, filter(None, string.split(' '))) 
print(max(lst, key = lambda x: lst.count(x))) 
# returns 9 
0

實際上有與代碼一對夫婦的問題,因爲它目前爲。你似乎是打印返回None函數的結果,你沒有正確區分你的字符串:

def print_most_numbers_occurrences(numbers_str): 
    number_list = [int(number) for number in numbers_str.split(' ') if number != ""] 
    for i in number_list: 
     i=max(number_list,key=number_list.count) 
    print(i, number_list.count(i)) 

def test_print_most_numbers_occurrences(): 
    print_most_numbers_occurrences('2 3 40 1 5 4 3 3 9 9') 
    print_most_numbers_occurrences('9 30 3 9 3 1 4') 
    print_most_numbers_occurrences('19 30 13 4 9 3 1 4') 


def main(): 
    test_print_most_numbers_occurrences() 


main() 
+0

你是對的,但如何獲得輸出9,3的字符串('9 30 3 9 3 1 4')? – Kee

+0

@Kee更新了代碼。它現在使用'.count'函數來查找出現次數。 – Neil

+0

你試過了嗎?它不以這種方式工作。 – Kee

0

您可以輕鬆的寫代碼:

>>> import re 
>>> x 
'2  3 40 1 5 4 3 3 9 9' 
>>> list = re.sub(' +', ' ', x).split(' ') 
>>> list 
['2', '3', '40', '1', '5', '4', '3', '3', '9', '9'] 
>>> max(list, key=list.count) 
'3' 
>>> x = '19 30 13 4 9 3 1 4' 
>>> list = re.sub(' +', ' ', x).split(' ') 
>>> list 
['19', '30', '13', '4', '9', '3', '1', '4'] 
>>> max(list, key=list.count) 
'4' 
>>> 

所以你的功能應該是作爲:

def fun(num_string): 
    num_list = re.sub(' +', ' ', num_string).split(' ') 
    print(max(num_list, key=num_list.count)) # you can write print but I prefer you should use return max(num_list, key=num_list.count) 
1

使用可以使用str.split()將您的字符串打破列表。然後,您可以使用map將字符串轉換爲整數。 A collections.Counter可用於以相對有效的方式對每個整數的出現進行計數。最後,max帶有一個lambda參數,可以用來提取出目標值的出現次數。

string_list = '2 3 40 1 5 4 3 3 9 9'.split() 
int_list = map(int, string_list) 

from collections import Counter 

counter = Counter(int_list) 
target_value, occurrence_count = max(counter.items(), key=lambda t: t[1]) 

注意,沒有參數提供給str.split並避免使用list.count(這是相當低效在這種情況下)。如果要獲取所有出現次數最多的目標值,可以放棄最後一行並使用:

max_occurrences = max(counter.values()) 
target_values = [k for k, v in counter.items() if v == max_occurrences]