Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > removing duplicates, or, converting Set() to string

Reply
Thread Tools

removing duplicates, or, converting Set() to string

 
 
maphew@gmail.com
Guest
Posts: n/a
 
      07-26-2006
Hello,

I have some lists for which I need to remove duplicates. I found the
sets.Sets() module which does exactly this, but how do I get the set
back out again?

# existing input: A,B,B,C,D
# desired result: A,B,C,D

import sets
dupes = ['A','B','B','C','D']
clean = sets.Set(dupes)

out = open('clean-list.txt','w')
out.write(clean)
out.close

---
out.write(clean) fails with "TypeError: argument 1 must be string or
read-only character buffer, not Set" and out.write( str(clean) )
creates "Set(['A', 'C', 'B', 'D'])" instead of just A,B,C,D.

thanks in advance for your time,

-matt

 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      07-26-2006
The write accepts strings only, so you may do:

out.write( repr(list(clean)) )

Notes:
- If you need the strings in a nice order, you may sort them before
saving them:
out.write( repr(sorted(clean)) )
- If you need them in the original order you need a stable method, you
can extract the relevant code from this large blob:
http://aspn.activestate.com/ASPN/Coo.../Recipe/438599

Bye,
bearophile

 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      07-27-2006

wrote:
> Hello,
>
> I have some lists for which I need to remove duplicates. I found the
> sets.Sets() module which does exactly this, but how do I get the set
> back out again?
>
> # existing input: A,B,B,C,D
> # desired result: A,B,C,D
>
> import sets
> dupes = ['A','B','B','C','D']
> clean = sets.Set(dupes)
>
> out = open('clean-list.txt','w')
> out.write(clean)
> out.close
>
> ---
> out.write(clean) fails with "TypeError: argument 1 must be string or
> read-only character buffer, not Set" and out.write( str(clean) )
> creates "Set(['A', 'C', 'B', 'D'])" instead of just A,B,C,D.
>
> thanks in advance for your time,
>
> -matt


Do ','.join(clean) to make a single string with commas between the
items in the set. (If the items aren't all strings, you'll need to
convert them to strings first.)

Peace,
~Simon

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      07-27-2006
wrote:
> Hello,
>
> I have some lists for which I need to remove duplicates. I found the
> sets.Sets() module which does exactly this


I think you mean that you found the sets.Set() constructor in the set
module.
If you are using Python 2.4, use the built-in set() function instead.
If you are using Python 2.3, consider upgrading if you can.

> but how do I get the set
> back out again?



>
> # existing input: A,B,B,C,D
> # desired result: A,B,C,D
>
> import sets
> dupes = ['A','B','B','C','D']
> clean = sets.Set(dupes)
>
> out = open('clean-list.txt','w')
> out.write(clean)
> out.close
>
> ---
> out.write(clean) fails with "TypeError: argument 1 must be string or
> read-only character buffer, not Set"


as expected

> and out.write( str(clean) )
> creates "Set(['A', 'C', 'B', 'D'])" instead of just A,B,C,D.


again as expected.

BTW, in practice you'd probably want to append '\n' to the string that
you're writing.

You should be able to get a (possibly unsorted) list of the contents of
*any* container like this (but note that dictionaries divulge only
their keys):

>>> dupes = ['A','B','B','C','D']
>>> clean = set(dupes) # the Python 2.4+ way
>>> clean

set(['A', 'C', 'B', 'D'])
>>> [x for x in clean]

['A', 'C', 'B', 'D']
>>>


list(clean) would work as well.

If you want the output sorted, then use the list.sort() method. Details
in the manual.

HTH,
John

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      07-27-2006
Simon Forman wrote:

>
> Do ','.join(clean) to make a single string with commas between the
> items in the set. (If the items aren't all strings, you'll need to
> convert them to strings first.)
>


And if the items themselves could contain commas, or quote characters,
you might like to look at the csv module.

 
Reply With Quote
 
maphew
Guest
Posts: n/a
 
      07-27-2006
thank you everybody for your help! That worked perfectly. I really
appreciate the time you spent answering what is probably a pretty basic
question for you. It's nice not to be ignored.

be well,

-matt

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing GPO setting from XP machine after removing from Domain Piet Slaghekke Computer Support 4 01-02-2007 08:58 PM
removing a namespace prefix and removing all attributes not in that same prefix Chris Chiasson XML 6 11-14-2006 05:08 PM
Removing the " Web Form Designer Generated Code " region after converting from ASP.NET 1.1 to ASP.NET 2.0 Nathan Sokalski ASP .Net 6 08-29-2006 12:28 AM
String Concatenation & Removing Space Sparky Arbuckle ASP .Net 5 09-01-2005 10:47 PM
STL: Converting a string into a list of string blrmaani C++ 4 04-27-2004 09:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57