Re: [Tutor] ***SPAM*** List to dictionary

2006-12-07 Thread Kent Johnson
Bill Campbell wrote:
 The way I usually do this is something like:
 
 outDict = dict(map(lambda x: (x, 1), inList))
 names = outDict.keys()
 names.sort()

This is a really old approach. Since Python 2.3 you can say
outDict = dict.fromkeys(inList)

or dict.fromkeys(inList, 1) if you cared about the value.

Since Python 2.4 you can use a set instead of a dict as shown in other 
replies.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ***SPAM*** List to dictionary

2006-12-07 Thread Mike Hansen
 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Luke Paireepinart
 Sent: Wednesday, December 06, 2006 10:54 PM
 To: tutor@python.org
 Subject: Re: [Tutor] ***SPAM*** List to dictionary
 
 Also, why is there now a **SPAM* in the subject heading?
 
 Wonderingly,
 -Luke
 

Maybe Bill was labeling it SPAM since it was posted twice to the list?
shrug /
Wed Dec 6 17:00:18 CET 2006
Thu Dec 7 04:31:55 CET 2006

Also, I tried to explain in my reply yesterday. The next statement
(temp[i] = 0) is where I get confused.
Can someone please explain what is happening here. 

I'm confused. %)
-

  NOTICE:  This e-mail transmission and any documents or files attached to
  it contain information for the sole use of the above-identified individual or 
entity.

  Its contents may be privileged, confidential, and exempt from disclosure 
under the law.
  Any dissemination, distribution, or copying of this communication is strictly 
prohibited.

  Please notify the sender immediately if you are not the intended recipient.

FGNS
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ***SPAM*** List to dictionary

2006-12-07 Thread Python
On Thu, 2006-12-07 at 08:22 -0700, Mike Hansen wrote:
  
  -Original Message-
  From: [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED] On Behalf Of Luke Paireepinart
  Sent: Wednesday, December 06, 2006 10:54 PM
  To: tutor@python.org
  Subject: Re: [Tutor] ***SPAM*** List to dictionary
  
  Also, why is there now a **SPAM* in the subject heading?
  
  Wonderingly,
  -Luke
  
 
 Maybe Bill was labeling it SPAM since it was posted twice to the list?
 shrug /
 Wed Dec 6 17:00:18 CET 2006
 Thu Dec 7 04:31:55 CET 2006
 
 Also, I tried to explain in my reply yesterday. The next statement
 (temp[i] = 0) is where I get confused.

temp is a dictionary.  ## earlier code was temp = {}
i is a name from a list called names.
temp[i] is a reference into the dictionary using i as a key.
temp[i] = 0 binds that reference to 0.  Any previous value for temp[i]
is discarded.

temp is simply being used to track distinct names.  Any name from names
will have one and only one occurrence in the list of dictionary keys.

So temp.keys() will contain each name exactly once.  As covered in a
recent thread, the ordering of the names will probably be different from
the original names list.


 Can someone please explain what is happening here. 
 
 I'm confused. %)
 -
 
   NOTICE:  This e-mail transmission and any documents or files attached to
   it contain information for the sole use of the above-identified individual 
 or entity.
 
   Its contents may be privileged, confidential, and exempt from disclosure 
 under the law.
   Any dissemination, distribution, or copying of this communication is 
 strictly prohibited.
 
   Please notify the sender immediately if you are not the intended recipient.
 
 FGNS
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ***SPAM*** List to dictionary

2006-12-06 Thread Bill Campbell
On Wed, Dec 06, 2006, Morpheus wrote:
I'm new to programming, and trying to learn the Python language.  
The following code does what I want it to do, but I have not idea how it
works.  

def scanList(names,temp):
for i in names:
temp[i] = 0
print temp

Names = []
temp = {}

I have a list of names (Names[]) and want to remove duplicate names in
the list.  Here is what I think is happening (please correct me if I'm
wrong, or using the wrong technical terminology):  I'm passing the
variables Names and temp as arguments to the scanList function.  The
statement (for i in names:) is an iteration going through each item in
the list.  The next statement (temp[i] = 0) is where I get confused.
Can someone please explain what is happening here.  

The way I usually do this is something like:

outDict = dict(map(lambda x: (x, 1), inList))
names = outDict.keys()
names.sort()

Bill
--
INTERNET:   [EMAIL PROTECTED]  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
FAX:(206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676

``Find out just what people will submit to, and you have found out the
exact amount of injustice and wrong which will be imposed upon them; and
these will continue until they are resisted with either words or blows, or
both. The limits of tyrants are prescribed by the endurance of those whom
they oppress.'' -- Frederick Douglass.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ***SPAM*** List to dictionary

2006-12-06 Thread Luke Paireepinart

 I have a list of names (Names[]) and want to remove duplicate names in
 the list.  [snip]
 

 The way I usually do this is something like:

 outDict = dict(map(lambda x: (x, 1), inList))
 names = outDict.keys()
 names.sort()
   
How about this :D

# Remove duplicates from a list:
 L = [1,2,2,3,3,3]
 [x for x in L if x not in locals()['_[1]'].__self__]
[1,2,3]

[accessed at 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 ]

Also, why is there now a **SPAM* in the subject heading?

Wonderingly,
-Luke

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor