[Tutor] Flatten a list in tuples and remove doubles
Steven D'Aprano
steve at pearwood.info
Fri Jul 20 11:00:31 CEST 2012
PyProg PyProg wrote:
> Hi all,
>
> I would get a new list as:
>
> [(0, '3eA', 'Dupont', 'Juliette', '11.0/10.0', '4.0/5.0', '17.5/30.0',
> '3.0/5.0', '4.5/10.0', '35.5/60.0'), (1, '3eA', 'Pop', 'Iggy',
> '12.0/10.0', '3.5/5.0', '11.5/30.0', '4.0/5.0', '5.5/10.0',
> '7.5/10.0', '40.5/60.0')]
>
> ... from this one:
>
> [(0, '3eA', 'Dupont', 'Juliette', 0, 11.0, 10.0), (0, '3eA', 'Dupont',
> 'Juliette', 1, 4.0, 5.0), (0, '3eA', 'Dupont', 'Juliette', 2, 17.5,
> 30.0), (0, '3eA', 'Dupont', 'Juliette', 3, 3.0, 5.0), (0, '3eA',
> 'Dupont', 'Juliette', 4, 4.5, 10.0), (0, '3eA', 'Dupont', 'Juliette',
> 5, 35.5, 60.0), (1, '3eA', 'Pop', 'Iggy', 0, 12.0, 10.0), (1, '3eA',
> 'Pop', 'Iggy', 1, 3.5, 5.0), (1, '3eA', 'Pop', 'Iggy', 2, 11.5, 30.0),
> (1, '3eA', 'Pop', 'Iggy', 3, 4.0, 5.0), (1, '3eA', 'Pop', 'Iggy', 4,
> 5.5, 10.0), (1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0)]
>
> How to make that ? I'm looking for but for now I can't do it.
Good grief! Please take the time to come up with a SIMPLE example! What parts
of the information there are important and what parts can we ignore?
I can't see anything being flattened there. All I see is duplicates being
removed. Can you explain what you actually expect?
The best way to remove duplicates will depend on whether or not the items are
hashable, and whether you care about the order they appear in. Here is a
simple example of removing duplicates while still keeping the original order:
def remove_dups(sequence):
new = []
for item in sequence:
if item not in new:
new.append(item)
return new
And in use:
py> old = [1, 2, 3, 2, 1, 4, 1, 1, 1, 5, 1, 5, 2]
py> remove_dups(old)
[1, 2, 3, 4, 5]
If you don't care about order, a set will be MUCH faster:
py> set(old)
{1, 2, 3, 4, 5}
--
Steven
More information about the Tutor
mailing list