Appending a dict entry to a blank dict

This is the place for queries that don't fit in any of the other categories.

Appending a dict entry to a blank dict

Postby mckryall » Wed Apr 16, 2014 6:32 am

This is a thing to produce a map with rooms in it. map is a list of numbers 1-100. rooms is a dict, shown below what is below.
Code: Select all
Dict = {}
for x in map:
   print x
   if x in rooms:
      Dict[x].append(rooms[x])
   else:
      Dict[x].append(["", False, False, False, False])
print Dict


Code: Select all
rooms = {
   1:["It is dark. You are cold. You feel your way around the walls and find two doors.", True, False, False, True]
}

I get this error:
Code: Select all
xxxx-xxxxxx:python xxxxxxx.xxxxxxxx$ python txt_adv_frame.py
1
Traceback (most recent call last):
  File "txt_adv_frame.py", line 4, in <module>
    Dict[x].append(rooms[x])
KeyError: 1
Last edited by mckryall on Wed Apr 16, 2014 12:54 pm, edited 2 times in total.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: Appending a dict entry to a blank dict

Postby stranac » Wed Apr 16, 2014 7:24 am

Showing us an error that matches the code would be a good idea...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Appending a dict entry to a blank dict

Postby mckryall » Wed Apr 16, 2014 12:51 pm

Better?
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: Appending a dict entry to a blank dict

Postby metulburr » Wed Apr 16, 2014 1:17 pm

If you are going to update a dictionary value use the method dict.update(). If the key exists update() will overwrite the key with the new value, otherwise update() creates a new key.

Code: Select all
rooms = {
   1:["It is dark. You are cold. You feel your way around the walls and find two doors.", True, False, False, True]
}
m = range(1,100)
Dict = {}
for ind in m:
   if ind in rooms:
      Dict.update({ind:rooms[ind]})
   else:
      Dict.update({ind:[""] + [False] * 4})
print(Dict)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Appending a dict entry to a blank dict

Postby mckryall » Wed Apr 16, 2014 1:37 pm

Thank you. Also, what does "ind" mean?
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: Appending a dict entry to a blank dict

Postby metulburr » Wed Apr 16, 2014 1:40 pm

i just changed it from x to ind (or index) because it made more sense. and i changed your map to m because it was overwriting the built-in map
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Appending a dict entry to a blank dict

Postby Mekire » Wed Apr 16, 2014 1:49 pm

If we are adding one item to the dictionary at a time using dict.update is a bit silly.

Consider the simple:
Code: Select all
for ind in m:
   if ind in rooms:
      my_dict[ind] = rooms[ind]
   else:
      my_dict[ind] = [""]+[False]*4

-Mek
User avatar
Mekire
 
Posts: 1126
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Appending a dict entry to a blank dict

Postby stranac » Wed Apr 16, 2014 1:59 pm

mckryall wrote:Better?

No.
This is the error I get when I run the code you posted:
Code: Select all
Traceback (most recent call last):
  File "a.py", line 2, in <module>
    for x in map:
TypeError: 'builtin_function_or_method' object is not iterable

So either you have not posted your actual code or you have not posted your actual error.

I know it might seem like I'm just being a dick, but the details actually matter.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Appending a dict entry to a blank dict

Postby mckryall » Wed Apr 16, 2014 10:35 pm

It doesn't seem like that. I'm confused because the only difference is that rooms is much longer on my end.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: Appending a dict entry to a blank dict

Postby micseydel » Wed Apr 16, 2014 11:04 pm

That's definitely not the only difference. Take this example session
Code: Select all
>>> for x in map: pass


Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    for x in map: pass
TypeError: 'builtin_function_or_method' object is not iterable

map is a built-in, and it's not iterable. Unless you overwrite the variable, you will get that exception. (You shouldn't overwrite it by the way, but instead rename your variable.) So if you're not getting that then you must be overwriting it somewhere earlier in the code. If you change the name from "map" to something else, that something else still isn't defined anywhere here.

Paste the whole of the file you're running, don't break it up as you tried to do above. If you don't want to post that make code, copy your file, edit down the copy, replicate the error, then post that new code here along with its associated traceback.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Appending a dict entry to a blank dict

Postby mckryall » Wed Apr 16, 2014 11:37 pm

I'm working on something right now that breaks the piece of code in question. When I have a script that works on its own (without placeholders and commented code), I'll post that.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm

Re: Appending a dict entry to a blank dict

Postby mckryall » Fri Apr 18, 2014 8:08 am

This bit of code has been deprecated completely. I was never going to let the user go into empty rooms, so, if the room number isn't in "rooms", the program should just return an error. At least I learned a little bit about dictionaries and that map is a builtin function.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall
 
Posts: 110
Joined: Sat Nov 23, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests