Python Forum
Where did this dictionary come from?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where did this dictionary come from?
#1
Please help me understand where this output comes from when I input just the enter key:

{33: None, 34: None, 35: None, 36: None, 37: None, 38: None, 39: None, 40: None, 41: None, 42: None, 43: None, 44: None, 45: None, 46: None, 47: None, 58: None, 59: None, 60: None, 61: None, 62: None, 63: None, 64: None, 91: None, 92: None, 93: None, 94: None, 95: None, 96: None, 123: None, 124: None, 125: None, 126: None}
<class 'dict'>

Here's my code:
import string

contacts = {}
#line = []
count = 0 #continue/stop counter

while count == 0:
    print("To create contacts dictionary, enter <last name>, <first name>, <phone number>.  ")
    rawcntc = input("If done type DONE: ")
    if rawcntc == "DONE":
        count = 1
        continue #this "if" block works fine...
    else: #...this "else" block does not.
        translation = rawcntc.maketrans("","",string.punctuation) #still a string... or evidently not??
        print(translation) #debug
        print(type(translation)) #debug
I'm really not understanding the syntax of the translate method. I've gotten "ValueError: the first two maketrans arguments must have equal length" multiple times in trying to figure this out [maybe when typing (",",string.punctuation) rather than ("","",string.punctuation) ].

Thank you!

I did notice there are the same number of characters (32) in string.punctuation as there are entries in the dictionary: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ . Do those numbers somehow correspond to those characters?
Reply
#2
as described in the docs for str.maketrans() method:
Quote:If there is a third argument, it must be a string, whose characters will be mapped to None in the result.
in this particular case the first and second argument are irrelevant - the goal is to map all punctuation chars to None. The numbers (the keys) are Unicode ordinals for punctuation chars
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
#3
The documentation of maketrans() says it all. If there are two arguments to maketrans(), they must be strings of equal length.

In fact you don't need to understand the table returned by maketrans to use it:
>>> table = str.maketrans("abc", "ABC")
>>> s = "At dawn, if it was low tide on the flats, I would awaken to the chatter of gulls."
>>> s.translate(table)
'At dAwn, if it wAs low tide on the flAts, I would AwAken to the ChAtter of gulls.'
Reply
#4
I do understand now why the first two arguments must be of equal length.

I still don't understand exactly what's wrong with my line 14.

Doesn't my use of two sets of double quotes for the first two arguments essentially map nothing onto nothing, which changes nothing?

Isn't the third argument, then, characters to be deleted from the string, which should be the set of 32 punctuation marks? Instead of deleting them, though, it is translating them into Unicode ordinals.

Are the double quotes being interpreted as nothing, leaving just one argument total (string.punctuation)?
Reply
#5
maketrans() is just creating [reusable] mapping table (dict). It doesn't replace anything. @Griboullis provided an example how to use the created table to translate a string.
in effect you pass as first and second argument equal length stringh (empty string with length 0). This is important because maketrans takes positional arguments.
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


Forum Jump:

User Panel Messages

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