Re: Zip Question

2009-10-10 Thread Victor Subervi
Ah! Thanks :)
V

On Fri, Oct 9, 2009 at 1:10 PM, Stephen Hansen apt.shan...@gmail.comwrote:



 On Fri, Oct 9, 2009 at 11:04 AM, Victor Subervi 
 victorsube...@gmail.comwrote:

 So, because the results in sstp were duplicates ( ['prescriptions',
 'prescriptions'] ) it only returned one result in the dict(zip()) statement.
 Weird. Bug or feature? ;)
 Thanks,
 V


 Feature.

 zip() returned two results, but dictionaries are mappings of keys to
 values. If you duplicate a key, you don't get multiple values for that key
 -- you replace the value.  If you want something dictionary like which has
 multiple values, try something like:

 http://code.activestate.com/recipes/52219/

 Or even this dict subclass:

 http://code.activestate.com/recipes/440502/

 HTH,

 --S

-- 
http://mail.python.org/mailman/listinfo/python-list


Zip Question

2009-10-09 Thread Victor Subervi
Hi;
I have the following code:

elif table[0] == 't': # This is a store subtype table
  bits = string.split(table, '0')
  sst.append(bits[2])
  sstp.append(bits[1])
  subtypes = dict(zip(sstp, sst))

When I print these out to screen, I get this:

sst: ['doctors', 'patient']
sstp: ['prescriptions', 'prescriptions']
subtypes: {'prescriptions': 'patient'}

Why do I only get one item from sst and sstp zipped? Why not both??
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Stephen Hansen
On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi victorsube...@gmail.comwrote:

 Hi;
 I have the following code:

 elif table[0] == 't': # This is a store subtype table
   bits = string.split(table, '0')
   sst.append(bits[2])
   sstp.append(bits[1])
   subtypes = dict(zip(sstp, sst))

 When I print these out to screen, I get this:

 sst: ['doctors', 'patient']
 sstp: ['prescriptions', 'prescriptions']
 subtypes: {'prescriptions': 'patient'}

 Why do I only get one item from sst and sstp zipped? Why not both??

I think you have a logic problem that's not shown in that code sample:

 sst = ['doctors', 'patient']
 sstp = ['prescriptions', 'prescriptions']
 zip(sst,sstp)
[('doctors', 'prescriptions'), ('patient', 'prescriptions')]
 dict(zip(sst,sstp))
{'patient': 'prescriptions', 'doctors': 'prescriptions'}


--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Chris Kaynor
On Fri, Oct 9, 2009 at 10:10 AM, Stephen Hansen apt.shan...@gmail.comwrote:



 On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi 
 victorsube...@gmail.comwrote:

 Hi;
 I have the following code:

 elif table[0] == 't': # This is a store subtype table
   bits = string.split(table, '0')
   sst.append(bits[2])
   sstp.append(bits[1])
   subtypes = dict(zip(sstp, sst))

 When I print these out to screen, I get this:

 sst: ['doctors', 'patient']
 sstp: ['prescriptions', 'prescriptions']
 subtypes: {'prescriptions': 'patient'}

 Why do I only get one item from sst and sstp zipped? Why not both??

 I think you have a logic problem that's not shown in that code sample:

  sst = ['doctors', 'patient']
  sstp = ['prescriptions', 'prescriptions']
  zip(sst,sstp)
 [('doctors', 'prescriptions'), ('patient', 'prescriptions')]
  dict(zip(sst,sstp))
 {'patient': 'prescriptions', 'doctors': 'prescriptions'}
 

 --S

 --
 http://mail.python.org/mailman/listinfo/python-list


The issue is:
 subtypes = dict(zip(sstp, sst))

If you remove the dict, you'll see the following:
[('prescriptions', 'doctors'), ('prescriptions', 'patient')]

When this is converted to a dict, the first element of each tuple is placed
as the dict's key, and the second as the value. This means that you have two
keys of prescriptions, and so the final one happens to be chosen as the
value.

Changing the line:
subtypes = dict(zip(sstp, sst))
to:
subtypes = dict(zip(sst, sstp))
as I believe Stephen misread it to be causes the zip operation to return:
[('doctors', 'prescriptions'), ('patient', 'prescriptions')]
and thus the dict will contain:
{'patient': 'prescriptions', 'doctors': 'prescriptions'}


Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Victor Subervi
You're right...how strange. Here's the whole code:

  tables = []
  bst = []
  bdt = []
  spt = []
  sst = []
  sstp = []
  cursor.execute('show tables;')
  all = cursor.fetchall()
  for a in all:
tables.append(a[0])
  for table in tables:
if table[0] == 'b': # This is a basic table
  if table[1] == '0': # This is a basic static table
bst.append(table)
#  elif table[1] == '1': # This is a basic dynamic table
#bdt.append(table)
# Basic dynamic tables, like pic below, have to be manually created.
elif table[0] == 's': # This is a store table
  if table[1] == '0': # This is a store primary table
spt.append(table)
elif table[0] == 't': # This is a store subtype table
  bits = string.split(table, '0')
  sst.append(bits[2])
  sstp.append(bits[1])
  subtypes = dict(zip(sstp, sst))
  print sst
  print 'br /'
  print sstp
  print 'br /'
  print subtypes
  print 'br /'

This is what prints out:

['doctors', 'patient']
['prescriptions', 'prescriptions']
{'prescriptions': 'patient'}

TIA,
V

On Fri, Oct 9, 2009 at 12:10 PM, Stephen Hansen apt.shan...@gmail.comwrote:



 On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi 
 victorsube...@gmail.comwrote:

 Hi;
 I have the following code:

 elif table[0] == 't': # This is a store subtype table
   bits = string.split(table, '0')
   sst.append(bits[2])
   sstp.append(bits[1])
   subtypes = dict(zip(sstp, sst))

 When I print these out to screen, I get this:

 sst: ['doctors', 'patient']
 sstp: ['prescriptions', 'prescriptions']
 subtypes: {'prescriptions': 'patient'}

 Why do I only get one item from sst and sstp zipped? Why not both??

 I think you have a logic problem that's not shown in that code sample:

  sst = ['doctors', 'patient']
  sstp = ['prescriptions', 'prescriptions']
  zip(sst,sstp)
 [('doctors', 'prescriptions'), ('patient', 'prescriptions')]
  dict(zip(sst,sstp))
 {'patient': 'prescriptions', 'doctors': 'prescriptions'}
 

 --S

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Stephen Hansen
Changing the line:
  subtypes = dict(zip(sstp, sst))
 to:
 subtypes = dict(zip(sst, sstp))
 as I believe Stephen misread it to be causes the zip operation to return:
 [('doctors', 'prescriptions'), ('patient', 'prescriptions')]
 and thus the dict will contain:
 {'patient': 'prescriptions', 'doctors': 'prescriptions'}


Whoops! You're absolutely right. I totally flipped the options to zip in my
head when eyeing it. I suppose I'm suffering from late-onset dyslexia. Ahem.

Yeah, I think the arguments to zip() were probably just flipped in Victor's
code. Unless he wants a result different then what I assume is expected. I
assume he's expecting {doctors: prescriptions, patient:
prescriptions}. If instead its something more like {prescriptions:
[doctors, patient]} then zip() isn't how to accomplish it.

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Victor Subervi
So, because the results in sstp were duplicates ( ['prescriptions',
'prescriptions'] ) it only returned one result in the dict(zip()) statement.
Weird. Bug or feature? ;)
Thanks,
V

On Fri, Oct 9, 2009 at 12:37 PM, Stephen Hansen apt.shan...@gmail.comwrote:



 Changing the line:
  subtypes = dict(zip(sstp, sst))
 to:
 subtypes = dict(zip(sst, sstp))
 as I believe Stephen misread it to be causes the zip operation to return:
 [('doctors', 'prescriptions'), ('patient', 'prescriptions')]
 and thus the dict will contain:
 {'patient': 'prescriptions', 'doctors': 'prescriptions'}


 Whoops! You're absolutely right. I totally flipped the options to zip in my
 head when eyeing it. I suppose I'm suffering from late-onset dyslexia. Ahem.

 Yeah, I think the arguments to zip() were probably just flipped in Victor's
 code. Unless he wants a result different then what I assume is expected. I
 assume he's expecting {doctors: prescriptions, patient:
 prescriptions}. If instead its something more like {prescriptions:
 [doctors, patient]} then zip() isn't how to accomplish it.

 --S

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zip Question

2009-10-09 Thread Stephen Hansen
On Fri, Oct 9, 2009 at 11:04 AM, Victor Subervi victorsube...@gmail.comwrote:

 So, because the results in sstp were duplicates ( ['prescriptions',
 'prescriptions'] ) it only returned one result in the dict(zip()) statement.
 Weird. Bug or feature? ;)
 Thanks,
 V


Feature.

zip() returned two results, but dictionaries are mappings of keys to values.
If you duplicate a key, you don't get multiple values for that key -- you
replace the value.  If you want something dictionary like which has multiple
values, try something like:

http://code.activestate.com/recipes/52219/

Or even this dict subclass:

http://code.activestate.com/recipes/440502/

HTH,

--S
-- 
http://mail.python.org/mailman/listinfo/python-list