Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My dict is not good
#1
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.
Reply
#2
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Reply
#5
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')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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'.
Reply
#8
Did you see the code in my previous post?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 62,492 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020