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
-
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 list
sort ()
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
Related articles
- python - what is the difference between the two jupyter kernels generated by conda?
- python - difference between pandas dataframeplotbar and dataframeplot (kind = bar)
- rspec - i don't understand the difference between let and let! behavior (an error occurs in requestspec)
- css - about the difference between theme and style in material ui
- what is the difference between environment variables in python and environment variables that can be seen in system-> system
- python - about downloading youtube videos by youtube-dl
- python, about the fire spread step of forest fire simulation
- javascript - differences in behavior between chrome extensions and jquery selectors in chrome developer tool
- python 3x - about functions used in python for statement
- about image output of python
- python 3x - please teach about the exchange of information with qthead
- python - about x-axis adjustment in matplotlib
- python - about "" "of" "" select === = "" "
- python - about hamiltonian neural networks
- about processing to exclude the character string group specified from list in python
- python - about write loop to csv
- about batch change of file name using python
- python - what i don't understand about yolo9000 (v2)
- about python argument and data definition
- about the behavior of the centos routing table
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
Here is the URL you should refer to
Functions that python itself has
Pointing tosorted
Means that it can be used onlyclass list ([iterable])
Functions
MeansSo 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