r/pythonhelp • u/[deleted] • Dec 01 '21
How to sort whole strings alphabetically?
So I was wondering if I have strings like this:
“hi my name is Sara” “Hello dog” “hi name”
Then how would I get an output where it looks like:
“Hello dog”(since it has a H and is capital letter) “hi my name is Sara” “hi name” (since n comes after m)
I can’t seem to figure this out as the sort() function cannot work with this as I don’t want it to sort individually but as strings.
0
u/MT1961 Dec 01 '21
Sort can handle this, but it isn't entirely obvious. The idea is to provide a sort lambda function:
newlist = sorted(mylist, key = lambda s: s.casefold())
Casefold will do a case insensitive sort when it is used as a lambda.
If you aren't into weirdly named functions, just use lower()
1
u/Goobyalus Dec 02 '21
You don't need a lambda, you can directly pass casefold or lower. It also sounds like /u/PaleontologistGrand6 does not want to treat upper and lowercase letters the same, so they can just use
sortedwith no special args. Sounds like they are trying to use a different sort function?>>> l = ["hi my name is Sara", "Hello dog", "hi name"] >>> sorted(l, key=str.casefold) ['Hello dog', 'hi my name is Sara', 'hi name'] >>> sorted(l) ['Hello dog', 'hi my name is Sara', 'hi name']1
u/MT1961 Dec 02 '21
That part is unclear. The key part is a lambda, whether you write one or not, but your point is well made, sorted would be easier. I prefer to not modify existing lists, since you might need them in the original order, which is why I prefer sort, but that's a personal thing.
1
u/Goobyalus Dec 02 '21
Huh?
You wrote:
newlist = sorted(mylist, key = lambda s: s.casefold())This is equivalent to the following; the lambda is an extra step for no reason:
newlist = sorted(mylist, key = str.casefold)I didn't mention sort vs sorted at all.
1
u/MT1961 Dec 02 '21
My apologies, I wasn't looking at the code I wrote. You are correct. Writing key= is easier and more direct.
1
u/Goobyalus Dec 02 '21 edited Dec 02 '21
Make sure you're sorting a list (or some other collection) of the entire strings:
If you put string literals next to each other, they are treated as one string that's been concatenated. Maybe you're declaring a long string instead of a list of strings, which might result in sorting the characters of the string. If that's all good, we'll talk about sorting.
list's
sortis an in-place sort:Notice that
lstarted out in one order, we sorted it, and thenlwas in a different order. Also notice that l.sort() does not return anything.Python's
sortedreturns a new list of the iterable that you pass in which is sorted:Notice that
sortedreturns a sorted copy of the list, butlremains the same afterward. Also notice the difference in how we're calling these:vs
because
sortis a method on lists, and sorted is a Python funciton to return a new sorted list from an arbitrary iterable object.Either of these can sort your whole strings, given that they are in the proper data structure and you understand how the result is given to you.