I'm asking the user to input a keyword and then remove any duplicate characters.
Example:
Input: balloon
Output: balon
I've tried this solution: List of all unique characters in a string? but it marks it as a syntax error.
Any ideas?
I'm asking the user to input a keyword and then remove any duplicate characters.
Example:
Input: balloon
Output: balon
I've tried this solution: List of all unique characters in a string? but it marks it as a syntax error.
Any ideas?
Try:
In [4]: from collections import OrderedDict
In [5]: def rawInputTest():
...: x = raw_input(">>> Input: ")
...: print ''.join(OrderedDict.fromkeys(x).keys())
In [6]: rawInputTest()
>>> Input: balloon
balon
For your answer, order is important. Here is a one line solution:
word = raw_input()
reduced_word = ''.join(
[char for index, char in enumerate(word) if char not in word[0:index]])
You can use OrderedDict:
In [15]: from collections import OrderedDict
In [16]: s="ballon"
In [17]: od = OrderedDict.fromkeys(s).keys()
In [18]: print(od)
['b', 'a', 'l', 'o', 'n']
In [19]: "".join(od)
Out[19]: 'balon'
You are performing a set operation. If order is not import, just insert each character into a set and print the contents of the set when done. If the order is important, then create a set. For each character, if it is not in the set append it to a list and insert it into the set. Print the contents of the list in order when done.
dict1 = {"cat":6,"rat":7,"dog":4,"elephent":20,"lion":15,"cow":9,"bat":4,"hen":2,"bird":2}
dic_values= dict1.values()
unique_val = set(dic_values)
print("dic_val :",dic_values)
print("unique_dict_valu :",unique_val)