Removing duplicates from a list

2005-09-14 Thread Rubinho
I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.  

What do you think?  

Cheers, 

Robin

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


Re: Removing duplicates from a list

2005-09-14 Thread Rubinho
Peter Otten wrote:
> Rubinho wrote:
>
> > I've a list with duplicate members and I need to make each entry
> > unique.
> >
> > I've come up with two ways of doing it and I'd like some input on what
> > would be considered more pythonic (or at least best practice).
> >
> > Method 1 (the traditional approach)
> >
> > for x in mylist:
> > if mylist.count(x) > 1:
> > mylist.remove(x)
>
> That would be an odd tradition:

By tradition I wasn't really talking Python tradition; what I meant was
that the above pattern is similar to what would be generated by people
used to traditional programming languages.

>
> >>> mylist = [1, 2, 1, 3, 2, 3]
> >>> for x in mylist:
> ... if mylist.count(x) > 1:
> ... mylist.remove(x)
> ...
> >>> mylist
> [2, 1, 2, 3] # oops!

But you're absolutely right, it doesn't work! Oops indeed :)

I've gone with Thomas's suggestion above of: mylist=list(set(mylist))

Thanks, 

Robin

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


Re: Noobie Starting New Project

2005-09-22 Thread Rubinho
Having looked in these sorts of areas before and having a general
interest in it, I'd like to make some suggestions.

I wouldn't go an implement OBD-II yourself in Python, especially not in
a serial port.  There are in fact (at least) 4 different OBD-II
protocols that I won't rattle off the names of here because I can't
remember them.  You have to do strange things like setting the serial
port to 5 baud, send a start byte, then set it to 10400 baud and wait
an indeterminate amount of time for a reply etc.  More info here
(http://www.andywhittaker.com/ecu/obdii_software.htm).  People have
done it like this with varying degrees of success on specific car
models (VAG-COM for VW and Audi cars for example) but never a generic
solution.

There is an implementation (http://freediag.sourceforge.net/) which
works with various interfaces and protocols but it's not been touched
since 2003.  I could never get this working in my car as it doesn't
support the OBD-II/CAN protocol.  You could wrap this if you think it
will work for you (UNIX only, no Win32 not even under cygwin).

There are devices which plug into OBD-II and have a dongle on the end
which does the OBD-II protocol detection and decoding for you and just
gives you a serial stream out. These are much more reliable (and much
more like what the "real" car industry uses).  There are many but
here's one supplier
(http://www.gendan.co.uk/viewcategory.php?category=117).  Googling for
scantool will find the rest.  Using python to either implement the
proper serial protocol used by these or wrap their existing libraries
(if they come with one) would, I think, be a much quicker and
potentially more successful approach.

As for other sensors, I found a great USB accelerometer
(www.phidgets.com) which I've used in python.  They supply a library
with a COM interface so I used the pythonwin COM module to access it.
They also have a more traditional C library (with source) if you want
to wrap it with python, you'd need to do this if you want to use their
Linux or Mac OSX libraries.  Phidgets make a whole set of cheap sensors
including temps, pressures etc. all accessible through USB.

GPS is easy and there are numerous ways to implement NMEA or Garmin
simple text protocols.  Or you could use GPSD
(http://gpsd.berlios.de/).

I've been about to start a similar project, just using an accelerometer
with temporal speed corrections using GPS for a little while now.  I've
got the maths sorted and the framework I just haven't decided on some
of the finer details I was going to do it in C++ but python seemed a
much better choice.  The only thing that concerns me is a lack of a
good timer but I must admit I've not looked into it too hard.

I hope this gets you started.  

Cheers, 

Robin

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