How to sort the letters in a string alphabetically in Python
You can do:
>>> a = 'ZENOVW'
>>> ''.join(sorted(a))
'ENOVWZ'
Python: How to sort the letters in a string alphabetically keeping distinction between uppercases and lowercases
sorted()
sorts based off of the ordinal of each character. Capital letters have ordinals that are lower than all lowercase letters. If you want different behavior, you'll need to define your own key:
c = sorted(s, key=lambda c: (c.lower(), c.islower()))
That way, c
would be sorted by ('c', 1)
and C
is sorted by ('c', 0)
. Both come before ('d', ...)
or ('e', ...)
etc., but the capital C
is earlier (lower) than the lowercase c
.
By the way, you shouldn't say d = "".join(sorted(c))
because c
has already been sorted. Just do d = "".join(c)
Sorting characters of a string in Python [closed]
Your solution is correct as strings are immutable in python. So, it's impossible to change (in your case - sort) an existing string. You have to create new one (you do it with join() call).
Also, good notes about sorting letters in string in python can be found here: How to sort the letters in a string alphabetically in Python
How to sort alphabetically without repetitious letters in python? [closed]
from collections import Counter
def layered_sort(s):
if not s: return s
c = Counter(s)
keys = sorted(c)
max_repeats = max(c.values())
return "".join([k for i in range(max_repeats) for k in keys if c[k] > i])
test_cases = (
"aaccbb",
"aacb",
"aaab",
"the rain in spain falls mainly on the plain"
)
for s in test_cases:
print(repr(s), "sorts to", repr(layered_sort(s)))
Result:
'aaccbb' sorts to 'abcabc'
'aacb' sorts to 'abca'
'aaab' sorts to 'abaa'
'the rain in spain falls mainly on the plain' sorts to ' aefhilmnoprsty aehilnpst ailn ailn ain n '
How to properly sort characters alphabetically with mixed case?
Use a tuple inside your key function:
sorted_list = sorted(word ,key = lambda s: (s.upper(),s ))
The second param of the tuple will come in hand when the first param is identical: this will sort by uppercases letters first (sorting 'D'
and 'd'
the same), then sort by the letter ('D'<'d'
- hence D comes first)
and you get:
DhLLllOooowy
str.capitalize
is not needed, you are dealing with single letters anyway - capitalize is good for words to Start Wth A Capital Letter - you can use upper here.
How to sort list of strings alphabetically with upper and lower values?
Okay, so the sort function allows you to specify a key with which to compare elements;
v.sort(key=str.lower)
Source: https://docs.python.org/3/howto/sorting.html
Trying to sort two combined strings alphabetically without duplicates
You have two options here. The first is the answer you want and the second is an alternative method
To filter out duplicates, you can make a blank string, and then go through the returned string. For each character, if the character is already in the string, move onto the next otherwise add it
out = ""
for i in returned_string:
if i not in out:
out += i
return out
This would be empedded inside a function
The second option you have is to use Pythons sets. For what you want to do you can consider them as lists with no dulicate elements in them. You could simplify your function to
def longest(a: str, b: str):
return "".join(set(a).union(set(b)))
This makes a set from all the characters in a
, and then another one with all the characters in b
. It then "joins" them together (union) and you get another set. You can them join all the characters together in this final set to get your string. Hope this helps
Related Topics
Find and Replace Specific Values Within 2D Array
Python, Pandas:Write Content of Dataframe into Text File
Regular Expression: Match Everything After a Particular Word
How to Reset Anaconda Root Environment
How to Remove the Decimal Point in a Pandas Dataframe
Pandas Populate New Dataframe Column Based on Matching Columns in Another Dataframe
Checking If a Button Has Been Pressed in Python
Find Specific Words in Text File and Print the Line Using Python
Counting Non Zero Values in Each Column of a Dataframe in Python
Deal With Overflow in Exp Using Numpy
No Output Displays When Execute Python File
How to Check All Versions of Python Installed on Osx and Centos
How to Make Python Get the Username in Windows and Then Implement It in a Script
Printing the Number of Days in a Given Month and Year [Python]
Python: Pandas Pd.Read_Excel Giving Importerror: Install Xlrd >= 0.9.0 for Excel Support
Print Floating Point Values Without Leading Zero
How to Read a CSV File from an S3 Bucket Using Pandas in Python