Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 4
There are 1 users currently browsing forums.
General Programming Here you can talk about scripting in other languages such as Perl, Delphi, Pascal and Python.

Reply
  #1  
Old 02-05-2010
Toddler
 
Join Date: Jan 2010
Posts: 7
Rep Power: 0
Galileo228 is on a distinguished road
python cursor and dictionary question

Hey all, after many of your suggestions I've been teaching myself python, and it's coming along well.

Question 1: I'm starting with a list, and trying to add each list item as a key to a dictionary, AND prompt for the value to be assigned to each key.

for x in list:
print "Enter value for", x,": "
dict[x] = input()

So for list = ['A', 'B'], the output would be

Enter value for A:
__

Enter value for B:
__

What I'm trying to do is get the cursor to remain after the colon, rather than go to the next line. I've tried this:

for x in list:
dict[x] = input("Enter grade for", x,": ")

but then I get an error that too many arguments are being passed.
Any thoughts on how to get the cursor to stay on the "print" line?

Question 2: Can someone please explain the difference between these two statements:

dict[x] = value
dict[x].append(value)

Thank you all for your continued help!

Matt
Reply With Quote
  #2  
Old 02-05-2010
Moderator
 
Join Date: Dec 2005
Posts: 1,862
Rep Power: 6
Umang is on a distinguished road
Re: python cursor and dictionary question

First of all, you shouldn't use input. Python parses input as python code not as text. So someone can actually enter python code into the input.
Second, avoid using names like dict and list for your variables, as this is what python uses for the dict and list objects and you are creating a conflict.
Here's an example of how you can achieve what you want:
Code:
my_list = ['A', 'B', 'C']
results = {}
for x in my_list:
    results[x] = raw_input("Enter grade for %s: "%x)
print results
As far your second question goes, dict[x].append calls the append method of the value of dict[x]. That means that dict[x] will have to be a list in order for you to call it. You would normally use dict[x] = value. If you want to modify an item and have a default value in case the item doesn't exist, then you could use dict.setdefault:
Code:
>>> ex_dict = {}
>>> ex_dict['bcd'] = ['b', 'c', 'd']
>>> ex_dict['bcd'].append('d')
>>> ex_dict
{'bcd': ['b', 'c', 'd', 'd']}
>>> ex_dict['abc'].append('c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'abc'
>>> ex_dict
{'bcd': ['b', 'c', 'd', 'd']}
>>> ex_dict.setdefault('abc', ['a', 'b', 'c']).append('c')
>>> ex_dict
{'bcd': ['b', 'c', 'd', 'd'], 'abc': ['a', 'b', 'c', 'c']}
>>>
Note that ex_dict['bcd'].append is essentially calling append on the item 'bcd' of ex_dict (which is a list). Since this is a list, we can actually call append. When ex_dict['abc'] is not defined, then append gives us an error.

So we use ex_dict.setdefault('abc', ['a', 'b', 'c']).append('c'). What this does is it checks to see if abc is a key in ex_dict. If it isn't it sets ex_dict['abc'] = ['a', 'b', 'c'] and then allows to further manipulate the item. If it is already set, it lets us manipulate it, without actually setting it to ['a', 'b', 'c']. That's why this doesn't work:
Code:
>>> ex_dict['abc'] = 'abc'
>>> ex_dict.setdefault('abc', ['a', 'b', 'c']).append('c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'
>>>
Since abc is already set to a string, which doesn't have an append method, you cannot call append. If abc wasn't set, then setdefault would set it to a list and then you can use append.

Last edited by Umang; 02-05-2010 at 08:05 PM.
Reply With Quote
  #3  
Old 02-06-2010
Toddler
 
Join Date: Jan 2010
Posts: 7
Rep Power: 0
Galileo228 is on a distinguished road
Re: python cursor and dictionary question

Umang,

Greatly appreciate the time you are taking to answer my questions. Your explanation makes sense. Can you further explain to me the uses of %s and %x? I tried looking up the % operator in the python docs, but I think they presume a bit more programming knowledge that I have. Obviously, in this case, %s is functioning as a kind of variable that returns the particularly key being added to the dictionary, but I'm not quite sure how it functions at a general level and where else I might use it.

Same goes for %x (and %course -- see my reply to my other thread).

Thank you.
Reply With Quote
  #4  
Old 02-07-2010
Moderator
 
Join Date: Dec 2005
Posts: 1,862
Rep Power: 6
Umang is on a distinguished road
Re: python cursor and dictionary question

It's called string formatting. (6. Built-in Types &mdash; Python v2.6.4 documentation)

It's pretty simple. If you have a string that would like to add some text/numbers into, then you just find the appropriate conversion letter (given in the table on the page I've linked to above). In our case we want to add a string, so we put 's' after the '%'.

Once we've told Python where to put what we want to put, we've got to tell it what to put also. So, at the end of the string we put another '%' to indicate that we're now giving Python the data. Here's a more complicated example that will help you understand better:

Code:
>>> print("Hello, my name is %s. I wrote the %s test yesterday and received a score of %.2f from a possible %.0f"%('Umang', 'example', 92.58547, 100))
Hello, my name is Umang. I wrote the example test yesterday and received a score of 92.59 from a possible 100
So the % at the end gives Python the data to put into the string.

Hope this helps.

PS: You should read the section on string formatting. It is pretty useful.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On