2017-06-30 10 views
3

例入力:[5,9、2、4、1、3]機能ではなく、同じ項目を持つ単一のリストのリストとして各項目を返して

期待出力:[9,2 、1]

関数は、以下の項目と同じ項目を持つ単一のリストではなく、リストとして各項目を返します。

[9]

を[2]

[1]

def divide_digits(a): 
    """ 
    This is where the function's Document string (docstring) goes. 
    """ 
    # make a shallow copy of the int_list and assign it to variable lst_copy 
    lst_copy = a[:] 
    # sort lst.copy 
    lst_copy.sort() 
    # as long as the lst_copy is not empty: 
    while lst_copy: 
     # get/pop the element from the beginning and at the end of the new_list 
     largest_num = lst_copy.pop() 
     smallest_num = lst_copy.pop(0) 

     new_list = [] 
     # perform the division of two these elements 
     result = largest_num/smallest_num 
     # round down the result to the nearest integer 
     # append the result of the division operation to the new list 
     new_list.append(round(result)) 

     # return the new_list 
     return new_list 
+2

あなた 'return'は内側にありますループ?? –

+0

あなたのインデントが奇妙に見えます。なぜあなたはwhileループの中に戻りますか?そして、while_loopの外側でnew_listを定義して、すべての要素を含めるべきではありませんか? – Secespitus

答えて

5

の問題がnew_list = []あります。すべての反復でリストを再初期化しています。そして、returnを字下げしなければなりません。

def divide_digits(a): 
    lst_copy = a[:] 
    lst_copy.sort() 
    new_list = [] # <-- this has to go here 
    while lst_copy: 
     largest_num = lst_copy.pop() 
     smallest_num = lst_copy.pop(0) 
     result = largest_num/smallest_num 
     new_list.append(round(result)) # <-- on every iteration you append to the new_list 
    return new_list # <-- when you are done looping, return the new_list 

リスト内包表記を利用してコードを短く代替はこれを次のようになります。

def divide_digits(a): 
    lst_copy = sorted(a) #<-- `sorted()`, unlike `.sort()` creates a new list 
    return [round(lst_copy.pop()/lst_copy.pop(0)) for _ in a[::2]] 
1

あなたのインデントが間違っています。 return文はwhileループ内にあります。それはその外側にあるはずです。つまり、ループ外でnew_listを定義する必要があります。次のことを試してみてください。

def divide_digits(a): 
    """ 
    This is where the function's Document string (docstring) goes. 
    """ 
    # make a shallow copy of the int_list and assign it to variable lst_copy 
    lst_copy = a[:] 
    # sort lst.copy 
    lst_copy.sort() 
    new_list = [] 
    # as long as the lst_copy is not empty: 
    while lst_copy: 
     # get/pop the element from the beginning and at the end of the new_list 
     largest_num = lst_copy.pop() 
     smallest_num = lst_copy.pop(0) 

     # perform the division of two these elements 
     result = largest_num/smallest_num 
     # round down the result to the nearest integer 
     # append the result of the division operation to the new list 
     new_list.append(round(result)) 

     # return the new_list 
    return new_list 
+0

「あなたのインデントが間違っている」とはどういう意味ですか、構文的に正しい@Secespitus – Sanket

+0

@Sanket構文的には正しいですが、OPが達成しようとしていることはしません。 – Secespitus

+0

はい、しかし、それは論理的なエラーではなく、インデント@Secespitus – Sanket

0

あなたはlist comprehensionを使用して、次の簡潔な溶液と同じ結果を得ることができます。

my_list = [5, 9, 2, 4, 1, 3] 
sorted_list = sorted(my_list) # sorted doesn't sort in place, it returns a new sorted list 
new_list = [round(sorted_list[-(i+1)]/sorted_list[i]) for i in range(len(sorted_list) // 2)] 

出力:

>>> new_list 
[9, 2, 1] 
関連する問題