Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
thanks for the reply. i should have been more specific in my question ;)

the order in which 'other' is listed is not always the last item of
the list as it is dependent on where in the CSV file it is included.

what i was trying to do is to take create the list of dictionary items
and then find the item which has name=='other' and then put this as
the last item in the list.

so from this list http://pastie.org/1185974 how do i select the item
name == 'other' and put it at the end of the list?

On Mon, Sep 27, 2010 at 11:39 PM, Emile van Sebille em...@fenx.com wrote:
 On 9/27/2010 1:22 PM Norman Khine said...

 what is the correct way to ensure that {'industry': 'travel', 'name':
 'other','value': MSG(uOther)} is always added to the end of this
 list after all the items have been sorted?

 here is the code which returns this list:

   options.sort(key=itemgetter('name'))
   return options

 So, to answer the question you ask above, you can do:

   options.sort(key=itemgetter('name'))
   options.append({'industry':'travel',
      'name':'other','value':MSG(uOther)}
   return options

 But I don't think that's the question you're looking to get answered.

 I think you want other to be found only at the end and not elsewhere.

 Then you might try excluding other from options allowing the above to append
 it to the end:

 for index, row in enumerate(topics.get_rows()):
    if row[0] != 'other':
        options.append({'name': row[0], 'value': MSG(row[1])})

 HTH,

 Emile



 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
thanks for the reply, i think i have it now, perhaps it could be done better

http://pastie.org/1186545

On Tue, Sep 28, 2010 at 2:56 PM, Emile van Sebille em...@fenx.com wrote:
  Hi Norman,

 Read my reply again -- that's the second question I answered.

 Emile


 On 9/28/2010 12:56 AM Norman Khine said...

 thanks for the reply. i should have been more specific in my question ;)

 the order in which 'other' is listed is not always the last item of
 the list as it is dependent on where in the CSV file it is included.

 what i was trying to do is to take create the list of dictionary items
 and then find the item which has name=='other' and then put this as
 the last item in the list.

 so from this list http://pastie.org/1185974 how do i select the item
 name == 'other' and put it at the end of the list?

 On Mon, Sep 27, 2010 at 11:39 PM, Emile van Sebilleem...@fenx.com
  wrote:

 On 9/27/2010 1:22 PM Norman Khine said...

 what is the correct way to ensure that {'industry': 'travel', 'name':
 'other','value': MSG(uOther)} is always added to the end of this
 list after all the items have been sorted?

 here is the code which returns this list:

   options.sort(key=itemgetter('name'))
   return options

 So, to answer the question you ask above, you can do:

   options.sort(key=itemgetter('name'))
   options.append({'industry':'travel',
      'name':'other','value':MSG(uOther)}
   return options

 But I don't think that's the question you're looking to get answered.

 I think you want other to be found only at the end and not elsewhere.

 Then you might try excluding other from options allowing the above to
 append
 it to the end:

 for index, row in enumerate(topics.get_rows()):
    if row[0] != 'other':
        options.append({'name': row[0], 'value': MSG(row[1])})

 HTH,

 Emile



 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor








-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Peter Otten
Norman Khine wrote:

 thanks for the reply, i think i have it now, perhaps it could be done
 better

  topics.sort(key=itemgetter('name'))
  for i, t in enumerate(topics):
 ... for (k, v) in t.iteritems():
 ... if v == 'other':
 ... topics.append(topics.pop(i))
 ... 
 
You should never iterate over a list or dictionary and add or remove items 
to it at the same time. That is a recipe for disaster even if it doesn't 
fail explicitly*

As Christian Witts explains in the trouble with list.remove() loop thread 
you will not see all items.

I suggest that you use a better sort key instead:

 def sort_key(topic):
... name = topic[name]
... return name == other, name
...
 topics.sort(key=sort_key)

 pprint(topics)
[{'industry': 'travel',
  'name': 'assistant-manager',
  'value': 'Assistant Manager'},

snip

 {'industry': 'travel', 'name': 'university', 'value': 'University'},
 {'industry': 'travel', 'name': 'other', 'value': 'Other'}]

The above sort_key() checks only the name key for an other value. It 
will return a (True, name) tuple if the name is other and (False, name) 
else. As 

False  True 

it ensures that topics with topic[name] == other are placed after all 
others. If (like your attempt suggests) you want to check all values instead 
of just the one associated with the name key use

def sort_key(topic):
return other in topic.itervalues(), topic[name]

Remember that both functions are case-sensitive.

Peter

(*) I'll leave it to Steven D'Aprano to add the fine print ;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Emile van Sebille

On 9/28/2010 7:12 AM Norman Khine said...

thanks for the reply, i think i have it now, perhaps it could be done better



I think I'd use a helper function to sort:

def sortOtherToEnd(val):
  if val['name'] == 'other:
return ''
  return val['name']

#then sort it

topics.sort(key=sortOtherToEnd)

But, generally, I'd stop once I got it going without worrying too much 
about 'better' ways -- that's a subjective measure.  There is often one 
obvious way to do it, but unless you've seen that way before, there'll 
often be many alternatives that work as well.


HTH,

Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
thank you, here is the updated version:

http://pastie.org/1186860

On Tue, Sep 28, 2010 at 5:50 PM, Emile van Sebille em...@fenx.com wrote:
 On 9/28/2010 7:12 AM Norman Khine said...

 thanks for the reply, i think i have it now, perhaps it could be done
 better


 I think I'd use a helper function to sort:

 def sortOtherToEnd(val):
  if val['name'] == 'other:
    return ''
  return val['name']

 #then sort it

 topics.sort(key=sortOtherToEnd)

 But, generally, I'd stop once I got it going without worrying too much about
 'better' ways -- that's a subjective measure.  There is often one obvious
 way to do it, but unless you've seen that way before, there'll often be many
 alternatives that work as well.

 HTH,

 Emile

 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Emile van Sebille

On 9/28/2010 9:37 AM Norman Khine said...

thank you, here is the updated version:

http://pastie.org/1186860




The only obvious redundancy is the duplicated sort of options just 
before the return.  You only need the newer sort_key based one.


Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
ok, great.

one thing i wanted to ask is how could i extend the class so that i
can just change the name of the csv file?

On Tue, Sep 28, 2010 at 6:53 PM, Emile van Sebille em...@fenx.com wrote:
 On 9/28/2010 9:37 AM Norman Khine said...

 thank you, here is the updated version:

 http://pastie.org/1186860



 The only obvious redundancy is the duplicated sort of options just before
 the return.  You only need the newer sort_key based one.

 Emile

 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] if value in list of dictionaries

2010-09-27 Thread Norman Khine
hello, i have a list which is generated from a csv file:

options = [
{'industry': 'travel', 'name': 'owner-director','value':
MSG(uOwner/Director)},
{'industry': 'travel', 'name': 'manager','value': MSG(uManager)},
{'industry': 'travel', 'name': 'assistant-manager','value':
MSG(uAssistant Manager)},
{'industry': 'travel', 'name': 'travel-consultant','value':
MSG(uTravel Consultant)},
{'industry': 'travel', 'name': 'managing-director','value':
MSG(uManaging Director)},
{'industry': 'travel', 'name': 'sales-director','value': MSG(uSales
Director)},
{'industry': 'travel', 'name': 'marketing-director','value':
MSG(uMarketing Director)},
{'industry': 'travel', 'name': 'marketing-manager','value':
MSG(uMarketing Manager)},
{'industry': 'travel', 'name': 'marketing-assistant','value':
MSG(uMarketing Assistant)},
{'industry': 'travel', 'name': 'product-manager','value':
MSG(uProduct Manager)},
{'industry': 'travel', 'name': 'reservation-staff','value':
MSG(uReservation Staff)},
{'industry': 'travel', 'name': 'student','value': MSG(uStudent)},
{'industry': 'travel', 'name': 'other','value': MSG(uOther)}]

what is the correct way to ensure that {'industry': 'travel', 'name':
'other','value': MSG(uOther)} is always added to the end of this
list after all the items have been sorted?

here is the code which returns this list:

http://pastie.org/1185024

thanks

-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if value in list of dictionaries

2010-09-27 Thread Emile van Sebille

On 9/27/2010 1:22 PM Norman Khine said...


what is the correct way to ensure that {'industry': 'travel', 'name':
'other','value': MSG(uOther)} is always added to the end of this
list after all the items have been sorted?

here is the code which returns this list:


   options.sort(key=itemgetter('name'))
   return options

So, to answer the question you ask above, you can do:

   options.sort(key=itemgetter('name'))
   options.append({'industry':'travel',
  'name':'other','value':MSG(uOther)}
   return options

But I don't think that's the question you're looking to get answered.

I think you want other to be found only at the end and not elsewhere.

Then you might try excluding other from options allowing the above to 
append it to the end:


for index, row in enumerate(topics.get_rows()):
if row[0] != 'other':
options.append({'name': row[0], 'value': MSG(row[1])})

HTH,

Emile



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor