473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help needed with dictionary

lee
hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....

i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']

Aug 29 '08 #1
3 1020
lee a écrit :
hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....

i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']
Please post the minimal *running* code exhibiting your problem. The
above snippet raises a NameError about person_name on line 3.

Anyway, looking at my crystal ball, I'd say that you're (re)binding the
variable 'kev' to a new empty dict each time.

Here's a working snippet:

import sys

kev = {}
try:
while True:
answer = raw_input("type a name :")
answer = answer.strip()
if answer:
try:
kev['name'].append(answer)
except KeyError:
kev['name'] = [answer]
print "name is now : %s" % " ".join(kev['name'])
print ""

except KeyboardInterrupt:
sys.exit("bye")
Aug 29 '08 #2
On Fri, Aug 29, 2008 at 5:02 AM, lee wrote:
i wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values.
http://mail.python.org/pipermail/pyt...st/505509.html
http://mail.python.org/pipermail/pyt...st/505584.html

-Miles
Aug 29 '08 #3
lee
hi,
thank you, ur code was helpful.... :)


On Aug 29, 2:18 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
lee a écrit :
hi all,
i am a newbie in python. i was trying to work with dictionaries. i
wanted to input values through command line and store the values in a
dictionary. i mean for the same key , multiple values. can any1
suggest me how can i do it.....thank you....
i tried this, but the old value is replaced by new one, but i want to
store al values entered by user.
kev = {}
if kev.has_key('Name'):
kev['Name'].append(person_name)
print 'name is ', kev['Name']
else:
kev['Name'] = [person_name]
print "kevin's name is %s" % kev['Name']

Please post the minimal *running* code exhibiting your problem. The
above snippet raises a NameError about person_name on line 3.

Anyway, looking at my crystal ball, I'd say that you're (re)binding the
variable 'kev' to a new empty dict each time.

Here's a working snippet:

import sys

kev = {}
try:
while True:
answer = raw_input("type a name :")
answer = answer.strip()
if answer:
try:
kev['name'].append(answer)
except KeyError:
kev['name'] = [answer]
print "name is now : %s" % " ".join(kev['name'])
print ""

except KeyboardInterrupt:
sys.exit("bye")
Aug 29 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pekka Niiranen | last post by:
Hi there, I have tree-like dictionary data = {p_name: {p_keyp: {p_cost: }}} which I update by creating nonexisting branches when necessary. If the full branch already exists I update its...
6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
16
by: lreames | last post by:
I am new to XML, but not ASP. I have the following XML that I load via Dim xml Set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = False xml.loadXML xmldom.xml
0
by: ajay.padala | last post by:
Hi I would like to be able to get the inputs that are needed into a template file programmatically. For example if we had a template: ===================== $name has $num marbles in his...
0
by: | last post by:
I am testing the following piece of code: Is it possible to list the row number containing the checked box? After checking the desired box, I would want a message box to popup listing the row...
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
6
by: daohuy.hua | last post by:
The context is that I have a C# class named MainModel which has a private Dictionary<string, FileStreammember named dict. I also have a property Dict to access to this member: public...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.