The goal is to remove any character that is repeated from a String, but with the restriction that the only character that can be repeated is the comma ","
That is, if I have the following String in Python:
universo = "a,f,h,4,a,h,g"
Should be:
universo = "a,f,h,4,g"
Use the following code to remove the repeated elements, but the problem is that it removes the included comma:
universo = "".join(dict.fromkeys(universo))
How could I fix it?
Solution without preserving order
The function
split(car)
allows splitting a string separated by the indicated character. In this case, we are interested in dividing by the comma, so we usesplit(",")
.The function
set
builds a set ( set ) from the input list, removing duplicates.With
",".join()
we create a string by putting "," between each element.That's it
produces:
Solution preserving order
After splitting the string using
split
, we iterate through it forming the listsin_coma
, discarding the duplicates:produces:
For an order-preserving version that scales well for large lists (runs in O(n) time instead of O(n^2)), we can take advantage of the fact that as of Python version 3.7 dictionaries guarantee insertion order.
That is, when going through a dictionary, the keys come out in the same order in which they were inserted. On the other hand, if a key already existed in a dictionary, it will not be inserted again, so it is a good mechanism to remove duplicates.
Using this idea you can reduce the code to one line:
The result in
nuevo
is"a,f,h,4,g"
How does it work
I
",".join()
am passing a dictionary that is being created right there by compression. This dictionary usesc
each of the letters that result from dividinguniverso
by the commas as keys, and 1 as the value (although the value is irrelevant).The first time a letter appears, it will be inserted into the dictionary with the value 1. The second time it appears, the 1 from the first time will be overwritten (although again with the value 1). The values are irrelevant, since what we are interested in are the keys (letters of
universo
) and the order in which they were first inserted.",".join()
will iterate through the keys of that dictionary, merging them all into a single comma-separated string, achieving the desired output.