-
Notifications
You must be signed in to change notification settings - Fork 8
Lecture "Organising information: ordered structures", exercise 1 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
|
|
protagonist_list ={"Harry","Draco","Hermione","Ron","Severus"}
I was thinking also about using the commands >, < in order to put the names in alphabetical order, However, it seems as a too long and much more complex process, especially when the command "sort" is available and has the same functions. |
|
|
list_str = list ( ) list_str.sort( ) |
Characters_list=list( ) Characters_list.append ("Draco") print (Characters_list) |
harry_potter_und_ein_stein = list(["Harry","Draco","Hermione","Ron","Severus"]) Optional: |
|
"Harry", "Draco", "Hermione", "Ron", "Severus" dislike_harrypotter = list() print dislike_harrypotter With the use of "sort" built-in function dislike_harrypotter = list() |
or
|
harry_potter_list=list () Print(harry_potter_list) |
Write a sequence of instructions in Python so as to create a list with the following elements ordered alphabetically: "Harry", "Draco", "Hermione", "Ron", "Severus". Starting from the list items in the given order, the first list would look this way: harry_potter_characters = list() print(harry_potter_characters) And the result would be: ['Harry', 'Draco', 'Hermione', 'Ron', 'Severus'] But then, we need to order properly all the names. This can be done in two ways (even though the third one is writing them by hand, of course). The first method doesn't use instructions to give the order. It just removes all the items but "Draco" from the list, and then adds them back in order. Like this: [...] And here only "Draco" remains in the list. Then: harry_potter_characters.append("Harry") print(harry_potter_characters) The result is the list in the alphabetical order: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'] The second method uses a proper instruction: [...] print(harry_potter_characters) And the result is the same, but it's way faster to obtain it: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'] |
my_harrypotter_list = list () my_harrypotter_list.append("Harry") my_harrypotter_list.sort() print(my_harrypotter_list) |
HPCharacters_list = list() HPCharacters_list.append("Draco") HPCharacters_list.sort( ) print(HPCharacters_list) Output: [‘Draco', 'Harry', 'Hermione', 'Ron', 'Severus'] |
I don't see in the pdf where the .sort method is, and how to use it. |
characters_list = list() |
Hi all, thanks for the answers, and also thanks for proposing additional strategies that go beyond the simpler "organise the item in the list by hand", that was of course one of the possibilities. Just a suggestion: try always to test your code in Python (if you didn't), since I've found a bunch of syntactical error here and there. |
"Harry", "Draco", "Hermione", "Ron", "Severus" happy_potter_list = list () happy_potter_list.sort() print (happy_potter_list) list ["Draco", "Harry", "Hermione", "Ron", "Severus"] |
harry_potter_list = list() result: ['Draco', 'Harry', 'Hermione', 'Ron', 'Severus'] |
Write a sequence of instructions in Python so as to create a list with the following elements ordered alphabetically:
"Harry"
,"Draco"
,"Hermione"
,"Ron"
,"Severus"
.The text was updated successfully, but these errors were encountered: