Home>

I don't understand why the code below is an error. .. ..
I changed sort to sorted and it worked fine.
I investigated the difference between the methods, but
I'm not hungry.

List type method sort (): Sort the original list
Built-in function sorted (): Generates a new sorted list

book_list = ['To the little you',
'Pupelle of Chimney Town',
'Harapeko Aomushi',
'Good night, Roger',
"Then, let's go!"]

sorted_book = sort (book_list)

for book in sorted_book:
print (book)

◆ Error log

Traceback (most recent call last):
File "sorted_2.py", line 9, in
sorted_book = sort (book_list)
NameError: name'sort' is not defined

  • Answer # 1

    Here is the URL you should refer to

    Built-in functions

    Functions that python itself hasPointing tosortedMeans that it can be used only

    Built-in methods

    class list ([iterable])FunctionsMeans
    So when using the method of list classlist. method nameIs required

    In documents that use functions and methods properly, function =What can be called by itselfMethod =Functions that Class hasIt's okay if you think

  • Answer # 2

    the function is"Non-destructive processing"When"Destructive processingIt is classified into. Methods like sort are also a type of function.

    Non-destructive processing does not change arguments.. The processing result is represented by the return value.
    Destructive processing changes the argument.. In many cases, the return value is None, etc. to avoid misuse with non-destructive processing.

    Non-destructive treatment is recommendedis. This is because changing the arguments adds more elements to programming and complicates. However, some functions can be destructive, have both, or optionally switch between them.Destructive processing simplifies code in some situationsBecause there is a merit.

    sorted () corresponds to non-destructive processing, and sort () corresponds to destructive processing.

    Let's exemplify with a simpler "addition". Non-destructive processing is + and destructive processing is + =.Each has its advantages and disadvantagesYou can see that.

    x = 5
    #Non-destructive processing
    y = x + 1
    print ('x =', x)
    # x = 5 Arguments do not change
    print ('y =', y)
    # y = 6 Return value changes
    z = (x + 1)-2
    print ('z =', z)
    # z = 4 Functions can be overlaid (another function can be called with the return value as an argument)
    # Destructive processing
    x + = 1 # The description becomes simple
    print ('x =', x)
    # x = 6 Arguments change
    #
    #y = (x + = 1)
    #Syntax Error Return value is undefined (may be None)
    #
    # (x + = 1)-= 2
    #Syntax Error functions cannot be overlaid

  • Answer # 3

    Of the listsort ()Methods and built-in functionssorted ()The usage of is as follows.

    book_list1 = ['To the little you',
    'Pupelle of Chimney Town',
    'Harapeko Aomushi',
    'Good night, Roger',
    "Then, let's go!"]
    book_list2 = ['To the little you',
    'Pupelle of Chimney Town',
    'Harapeko Aomushi',
    'Good night, Roger',
    "Then, let's go!"]
    book_list1.sort ()
    for book in book_list1:
        print (book)
    print ('=====')
    sorted_list = sorted (book_list2)
    for book in sorted_list:
        print (book)
    # Output result
    #Pupelle of Chimney Town
    #Good night, Roger
    # Then, chattering
    #To the little you
    #Harapeko Aomushi
    # =====
    #Pupelle of Chimney Town
    #Good night, Roger
    # Then, chattering
    #To the little you
    #Harapeko Aomushi

    The behavior and differences in behavior are explained in detail on the following site.

    The difference between sorted and sorted to sort the list in Python