Python Forum
My dict is not good - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: My dict is not good (/thread-21272.html)



My dict is not good - blackknite - Sep-22-2019

I want to create a dict from txt file, with words & summed ascii value but once again I have been defeated.
I guess I have never really understood dicts, in Python they arent intuitive at all and I just keep forgetting it all.

My worthless struggles:
#   coding=utf-8

coded, words = open("scrambled-words.txt", "r", encoding="UTF-8"), open("dictionary.txt", encoding="UTF-8")
coded, words = [x.strip('\n') for x in coded], [x.strip('\n') for x in words]

def bit_ops(coded, words):
    bit_list = {}
    for x in coded:
        bit_list = dict(zip(coded, [ord(y) for y in x]))
    print(bit_list)
Gets me pathethic results like :

Output:
{'dnaoyt': 116, 'cinuertdso': 115, 'bda': 115, 'haey': 101, 'tolpap': 101, 'slaayr': 110, 'eortem': 118}
I have tried few other things with '__setitem__' and 'update' and I failed too, so Im out of ideas.


RE: My dict is not good - ichabod801 - Sep-22-2019

It's not clear to me what you are trying to do here, but I would note that you are assigning bit_list each time through the loop. Further, you loop is over coded, and you create a dict with all of coded. Say those words in your output are the words in coded. The first time through the loop, x = 'dnaoyt'. Then you create a dictionary where each word in coded is assigned the byte value of one character from 'dnaoyt'. But since there are only six letters in 'dnaoyt', zip stops after six words of coded, and you get the dict you see.

When you create a dict with zip, the first thing you pass to zip is going to be the keys of the dictionary, and the second thing you pass to zip is going to be the values of the dictionary. dict(zip('abc', [1, 2, 3])) will give you {'a': 1, 'b': 2, 'c': 3}.

I think you want this:

for x in coded:
    bit_list[x] = [ord(y) for y in x]
But I'm not sure. Maybe you want the sum of that list?


RE: My dict is not good - ndc85430 - Sep-22-2019

It's not clear why these are not the results you expect, so perhaps you could give us a sample of the input file along with the expected results?

One thing that stands out is why on line 9 are you overwriting the dictionary bit_list on each iteration of the loop? Why aren't you just adding keys with their values?

Also, it might help if you told us what was unintuitive about dictionaries to you. Perhaps someone could help clear that up.


RE: My dict is not good - blackknite - Sep-22-2019

Hey. Thx for replying!

The file with name "coded" contains permutations of a second file "words", and some random alphanumeric strings that I have to filter.
Its like 'blablah<EOL>', and EOL is stripped so we are left with a clean list of strings in "coded" on the 4th line.

I have tried to operate on the alphabetic level but it takes ages just to show me all possible permutations of strings in file 'coded' to compare it:
#   coding=utf-8
import os, itertools
coded, words = open("scrambled-words.txt", "r", encoding="UTF-8"), open("dictionary.txt", encoding="UTF-8")
coded, words = [x.strip('\n') for x in coded], [x.strip('\n') for x in words]

def unmask(coded, words):
    perms = []
    for x in coded:
         perms = ([''.join(x) for x in itertools.permutations(x)])
         print(*perms, sep="\n")
                        

unmask(coded, words)
So my idea is to check it on a binary level and thats what funciton binary_ops is about.
If ascii sum of string is the same then and only then it will compare the letters.I thought it would be nice and handy for a start to get a dict like "word" : "summed ascii value" but Im stuck here, somehow I cant make a dict like that.


RE: My dict is not good - perfringo - Sep-23-2019

It's unclear for me, so I try to articulate my understanding:

- there is file containing words
- there is another file containing scrambled words (letters of words in random order) and random alphanumeric strings (there is no corresponding word)

Do you need replace scrambled words with real ones and filtering out random strings? Or you need to filter out only random string and keep scrambled ones?

Real words sorted characters and scrambled words sorted characters are equal and one can use this to achieve desired result. However, in any case there will be problem as from same combination of letters several words can be constructed (there can be scrambled word 'tcsa' which can be either 'acts', 'cats' or 'cast')


RE: My dict is not good - wavic - Sep-23-2019

(Sep-22-2019, 02:03 PM)blackknite Wrote: I want to create a dict from txt file, with words & summed ascii value ...
In [1]: words = ['a', 'aa', 'faiou' 'ggww']

In [2]: ascii_sums = {word: sum([ord(char) for char in word]) for word in words}
   ...: 

In [3]: ascii_sums
Out[3]: {'a': 97, 'aa': 194, 'faiouggww': 976}
(Sep-22-2019, 02:50 PM)ndc85430 Wrote: It's not clear why these are not the results you expect, so perhaps you could give us a sample of the input file along with the expected results?



RE: My dict is not good - blackknite - Sep-23-2019

I just want to have a dict with words as keys, and summed ascii value of all letters in that word as values, I dont know if I can say it any clearer.

Maybe dont even look at code from my second post cause you will be just more confused. The first post says it all. The dict is now 'word':'ascii value of one letter', i want it to be like 'word':'ascii values of all letters in that word'.


RE: My dict is not good - wavic - Sep-23-2019

Did you see the code in my previous post?


RE: My dict is not good - buran - Sep-23-2019

(Sep-23-2019, 09:29 AM)blackknite Wrote: I dont know if I can say it any clearer.
well, even in this post you have contradiction:
(Sep-23-2019, 09:29 AM)blackknite Wrote: I just want to have a dict with words as keys, and summed ascii value of all letters in that word as values
this would mean word (type str) would be key and one int as value, e.g. {"spam':433, 'eggs':422}

then you continue
(Sep-23-2019, 09:29 AM)blackknite Wrote: i want it to be like 'word':'ascii values of all letters in that word'
Here you imply that you have word as key and (here is the difference) - ascii values of all characters, i.e. that would be a list of many integers - as many as there are chars in the word, e.g. {'spam':[115, 112, 97, 109], 'eggs':[101, 103, 103, 115]}.

to get real help give sample input and sample output


RE: My dict is not good - blackknite - Sep-23-2019

(Sep-23-2019, 10:01 AM)wavic Wrote: Did you see the code in my previous post?

Yeah, page wasnt refreshed or I just overlooked it. It works. thank you.