Python Forum
How to transform an enumerate object into a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to transform an enumerate object into a dictionary
#1
Hi everyone,
I am using the enumerate function on a string returned by the user and I am trying to transform this to a dictionary. If I try to use the dict() command python just ignores the request.

Here is the code I have:
import string
what = input ("Please tell me the string you want to use:  ")
letter = input ("What is the letter you want me to find: ")
values = {}
numbs = ()
a = what.find (letter)
if a == -1:
    print ("not there")
else:
    e = enumerate(what)
    #The below is code to test what is happening
    print (type(e))
    print (e)
    for x in e:
        print (x)
Reply
#2
You can turn a string into a dictionary with:
a = "my_string"
my_dictionary = dict()
enumerated_string = enumerate(a)

for number, letter in enumerated_string:
    my_dictionary[number] = letter

print(my_dictionary)
Or you can switch number and letter, depending on what you want to be key and value. But in this case beware of repeating characters in the string. Just try with e.g. Mississippi.
Reply
#3
It appears it is still a string?

import string
what = input ("Please tell me the string you want to use:  ")
letter = input ("What is the letter you want me to find: ")
values = {}
a = what.find (letter)
if a == -1:
    print ("not there")
else:
    e = enumerate(what)
    for x, y in e:
        values=[x] = y
        print (type(values))
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>

If I reverse x and y I get this error

values=[y] = x
TypeError: 'int' object is not iterable

I see I had errors in my code.

This code works.

import string
what = input ("Please tell me the string you want to use:  ")
letter = input ("What is the letter you want me to find: ")
values = dict()
a = what.find (letter)
if a == -1:
    print ("not there")
else:
    e = enumerate(what)
    for x, y in e:
        values[x] = y
        print (type(values))
Thank you!
Reply
#4
On line 11 I think you don't want the first equal sign:
values=[x] = y
And maybe also last print() statement not repeating with each loop in for:
    for x, y in e:
        print(x,y)
        values[x] = y
    print (type(values))
Reply
#5
I'm confused. What are we trying to do?
You can make a dictionary directly from an enumerated string:
>>> d = dict(enumerate("spam and eggs"))
>>> d
{0: 's', 1: 'p', 2: 'a', 3: 'm', 4: ' ', 5: 'a', 6: 'n', 7: 'd', 8: ' ', 9: 'e', 10: 'g', 11: 'g', 12: 's'}
>>>
Reply
#6
@Mekire you got the goal right. And your solution is much neater - more Pythonic. I like :)
Reply
#7
j.crater,
did you run the code you posted. you can't print(my_Dictionary), its a dict.

Here's another way to do it:
    aString = "Mississippi"
    my_dictionary = dict()
    enumerated_string = enumerate(aString)
    print("enumerated_string:\t" + str(enumerated_string))
    for element in enumerated_string:
        print(str(element))
        my_dictionary[element[0]] = element[1]
    # rather than this, since you don't really need the names of the tuple elements:
    #for number, letter in enumerated_string:
    #    my_dictionary[number] = letter
    print("my_dictionary:\t" + str(my_dictionary))
Reply
#8
Or if you want a dictionary of words (to elaborate on Mekire's post
d = dict(enumerate("spam and eggs".split()))
Output:
>>> d = dict(enumerate("spam and eggs".split())) >>> d {0: 'spam', 1: 'and', 2: 'eggs'} >>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Alternative of this mini code without Enumerate and join function. Ace4Benji 2 2,472 Mar-09-2020, 08:22 PM
Last Post: micseydel
  Code Review: Range and Enumerate lummers 2 2,097 Jan-11-2020, 12:40 AM
Last Post: lummers
  Help using a pre-code for Discrete Fourier Transform ToucheP 0 1,656 Mar-28-2019, 06:18 AM
Last Post: ToucheP
  enumerate and output control atux_null 7 5,093 Oct-24-2017, 06:50 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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