[Tutor] String Attribute
Alan Gauld
alan.gauld at btinternet.com
Fri Jul 31 11:00:50 CEST 2015
On 31/07/15 01:25, ltc.hotspot at gmail.com wrote:
> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt"
> fh = open(fname)
> count = 0
> for line in fh:
> if not line.startswith('From'): continue
> line2 = line.strip()
> line3 = line2.split()
> line4 = line3[1]
> print line4
> count = count + 1
> print "There were", count, "lines in the file with From as the first word"
>
> Question: How do I remove the duplicates:
OK, You now have the original code working, well done.
To remove the duplicates you need to collect the addresses
rather than printing them. Since you want the addresses
to be unique you can use a set.
You do that by first creating an empty set above
the loop, let's call it addresses:
addresses = set()
Then replace your print statement with the set add()
method:
addresses.add(line4)
This means that at the end of your loop you will have
a set containing all of the unique addresses you found.
You now print the set. You can do that directly or for
more control over layout you can write another for
loop that prints each address individually.
print addresses
or
for address in addresses:
print address # plus any formatting you want
You can also sort the addresses by calling the
sorted() function before printing:
print sorted(addresses)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list