[Tutor] A Dictionary question
Wayne Werner
waynejwerner at gmail.com
Mon Apr 11 13:31:02 CEST 2011
On Mon, Apr 11, 2011 at 6:01 AM, Sophie DeNofrio <swimbabe339 at yahoo.com>wrote:
> Hi Everyone,
>
> I am a super beginner and am little muddled right now. So I apologize for
> the low level question
>
That's what this list is for, so you're asking in the right place!
> but I am trying to write a function that will return a dictionary of a
> given list of strings containing two coordinates separated by a space with
> the first numbers as a key and the second numbers as its corresponding
> value. I thought maybe a set might be helpful but that didn't seem to work
> at all. I am pretty much as confused as they come and any help would be very
> much appreciated. Thank you so much for your time.
>
It's always helpful to give example input and output, as well as show us
what you've done so far.It's *also* a good idea to tell us what your end
goal is, rather than just "I have this data and I want to manipulate it
thus", because oft times it turns out that there are much better ways to
accomplish the end goal.
Here's what it sounds like you say you want to do:
line_xy = ['1 1', '2 2', '3 3', '4 4'] # Coordinates along the line y = x
def dictify_list(coords):
returnval = {}
for pair in coords:
x, y = pair.split()
returnval[x] = y
return returnval
dictify_list(line_xy) # produces {'1':'1', '3':'3', '2':'2', '4':'4'} or
some equivalent dict - they're non-ordered
But I can't think of a time that I would want data collected this way. My
guess is that unless you have a super special case, you don't really need
the data that way either.
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110411/c2c99679/attachment.html>
More information about the Tutor
mailing list