0

I'm trying to write a function that receives a string and then each character should be sorted by its Unicode value. I cannot get this straight in my head. I first created an empty dictionary and an empty list. I want to first get the word separated and stored in that list and then use than list's members( which is my separated word )as a key for my dictionary and then sort out the dictionary. any help would be appreciated. I'm stuck in how should I map this, using elements of my list as a value for my dictionary. pardon me for beginner's mistake. so if I give my function "ja" as parameter the list will be the follow

My_list=['j','a']

then I want my dictionary to use the members as key and be like this

word_dic={'j':1 , 'a':2}

I used arbitrary values for j and values. their real value is their unicode values

   word_dic={}
    My_List=[]
    def strsort(word):
      for i in  range(len(word)):
        My_List.append(word[i])
        for k,v in word_dic.items():
          word_dic[My_List[i]] =ord(i)
James
  • 13
  • 3
  • `return "".join(sorted(word))`? – Selcuk Sep 06 '21 at 06:59
  • 3
    Please include sample data with the input and the corresponding desired/current output. – timgeb Sep 06 '21 at 07:00
  • 1
    I edited that I want my list members to be keys to be used as key – James Sep 06 '21 at 07:01
  • 1
    I now included the desired output – James Sep 06 '21 at 07:05
  • You should get what you want with `{x: i for i, x in enumerate(lst, start=1)}`. About your code: What do you have two nested loops, but never use any of the variables in the inner loop? Why `ord`, did you mean `index`? And what does all that have to do with sorting? – tobias_k Sep 06 '21 at 07:08
  • by ord I want to get the ordinal value and assign it a value to my keys – James Sep 06 '21 at 07:15
  • it is just a demo I don't if its necessary to even use the inner loop . don't know how to construct my dictionary after creating the list – James Sep 06 '21 at 07:15
  • Why is ```a``` equals 2 in your example ? What should be the values of your dict ? – Ram Sep 06 '21 at 07:18
  • as I said I used an arbitrary number, they should be the Unicode value of a the keys – James Sep 06 '21 at 07:21

2 Answers2

0

There is a builtin sorted function which will sort any iterable object (list, str, set, ...). Using it on a string will return a list with all characters of the string with Unicode order:

>>> sorted("asdfabc")
['a', 'a', 'b', 'c', 'd', 'f', 's']
0

In newer Python Versions a dict keeps the insertion order see.

If you are using an older version, sorting before inserting the values into the dict may be useless.

This will give you a dict where the characters are sorted by their unicode value:

word_dic={}
My_List=[]
def strsort(word):
    global word_dic, My_List
    My_List = list(word)
    
    sortedListByCode = sorted(My_List)
    
    for i in sortedListByCode:
        if i not in word_dic:
            word_dic[i] = ord(i)
        
strsort("Ja")
print(word_dic)
Kartoffelkultur
  • 190
  • 1
  • 11