hello,
this must have come up before, so i am already sorry for asking but a
quick googling did not give me any answer.
i have a list from which i want a simpler list without the duplicates
an easy but somehow contrived solution would be a = [1, 2, 2, 3] d = {}.fromkeys(a) b = d.keys() print b
[1, 2, 3]
there should be an easier or more intuitive solution, maybe with a list
comprehension=
somthing like
b = [x for x in a if x not in b] print b
[]
does not work though.
thanks for any help
chris 22 35463
Hi, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer.
i have a list from which i want a simpler list without the duplicates an easy but somehow contrived solution would be
In python 2.3, this should work:
import sets
l = [1,2,2,3]
s = sets.Set(l)
A set is a container for which the invariant that no object is in it twice
holds. So it suits your needs.
Regards,
Diez
> >>> a = [1, 2, 2, 3] >>> d = {}.fromkeys(a) >>> b = d.keys() >>> print b
[1, 2, 3]
That, or using a Set (python 2.3+). Actually I seem to recall that
"The Python CookBook" still advises building a dict as the fastest
solution - if your elements can be hashed. See the details at http://aspn.activestate.com/ASPN/Coo...n/Recipe/52560
Cheers,
Bernard.
christof hoeke wrote:
... i have a list from which i want a simpler list without the duplicates
Canonical is:
import sets
simplerlist = list(sets.Set(t helist))
if you're allright with destroying order, as your example solution suggests.
But dict.fromkeys(a ).keys() is probably faster. Your assertion:
there should be an easier or more intuitive solution, maybe with a list comprehension=
doesn't seem self-evident to me. A list-comprehension might be, e.g:
[ x for i, x in enumerate(a) if i==a.index(x) ]
and it does have the advantages of (a) keeping order AND (b) not
requiring hashable (nor even inequality-comparable!) elements -- BUT
it has the non-indifferent cost of being O(N*N) while the others
are about O(N). If you really want something similar to your approach: >>> b = [x for x in a if x not in b]
you'll have, o horrors!-), to do a loop, so name b is always bound to
"the result list so far" (in the LC, name b is only bound at the end):
b = []
for x in a:
if x not in b:
b.append(x)
However, this is O(N*N) too. In terms of "easier or more intuitive",
I suspect only this latter solution might qualify.
Alex
Bernard Delmée wrote: >>> a = [1, 2, 2, 3] >>> d = {}.fromkeys(a) >>> b = d.keys() >>> print b [1, 2, 3]
That, or using a Set (python 2.3+). Actually I seem to recall that "The Python CookBook" still advises building a dict as the fastest solution - if your elements can be hashed. See the details at
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52560
Yep, but a Set is roughly as fast as a dict -- it has a small
penalty wrt a dict, but, emphasis on small. Still, if you're
trying to squeeze every last cycle out of a bottleneck, it's
worth measuring, and perhaps moving from Set to dict.
Alex
Diez B. Roggisch wrote: this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer.
i have a list from which i want a simpler list without the duplicates an easy but somehow contrived solution would be
In python 2.3, this should work:
import sets l = [1,2,2,3] s = sets.Set(l)
A set is a container for which the invariant that no object is in it twice holds. So it suits your needs.
....except it's not a LIST, which was part of the specifications given
by the original poster. It may, of course, be that you've read his
mind correctly and that, despite his words, he doesn't really care
whether he gets a list or a very different container:-).
Alex
Hi, ...except it's not a LIST, which was part of the specifications given by the original poster. It may, of course, be that you've read his mind correctly and that, despite his words, he doesn't really care whether he gets a list or a very different container:-).
You're right - mathematically. However, there is no such thing like a set in
computers - you always end up with some sort of list :)
So - he'll have a list anyway. But if it respects the order the list
parameter... <just checking, standby>
.... nope: l = [1,2,3,2] import sets sets.Set(l)
Set([1, 2, 3]) l = [2,1,2,3,2] sets.Set(l)
Set([1, 2, 3])
Which is of course reasonable, as the check for existence in the set might
be performed in O(ln n) instead of O(n).
Regards,
Diez
"Diez B. Roggisch" <de************ @web.de> wrote in message news:<bn******* ******@news.t-online.com>... Hi,
...except it's not a LIST, which was part of the specifications given by the original poster. It may, of course, be that you've read his mind correctly and that, despite his words, he doesn't really care whether he gets a list or a very different container:-). You're right - mathematically. However, there is no such thing like a set in computers - you always end up with some sort of list :)
Those are just implementation details. There could be a group of monkeys
emulating Python under the hood and their implementation of a set
would be a neural network instead of any kind of sequence, but you
still wouldn't care as a programmer. The only thing that matters is,
if the interface stays same. Nope, the items in a set are no longer
accessible by their index (among other differences).
So - he'll have a list anyway.
So I can't agree with this. You don't know if his Python virtual machine
is a group of monkeys. Python is supposed to be a high level language.
But if it respects the order the list parameter... <just checking, standby>
... nope:
l = [1,2,3,2] import sets sets.Set(l) Set([1, 2, 3]) l = [2,1,2,3,2] sets.Set(l)
Set([1, 2, 3])
Which is of course reasonable, as the check for existence in the set might be performed in O(ln n) instead of O(n).
Actually the Set in sets module has an average lookup of O(1), worst
case O(n) (not 100% sure of worst case, but 99% sure). It's been
implemented with dictionaries, which in turn are hash tables.
Alex Martelli <al***@aleax.it > wrote in
news:L7******** ************@ne ws1.tin.it: Your assertion:
there should be an easier or more intuitive solution, maybe with a list comprehension=
doesn't seem self-evident to me. A list-comprehension might be, e.g:
[ x for i, x in enumerate(a) if i==a.index(x) ]
and it does have the advantages of (a) keeping order AND (b) not requiring hashable (nor even inequality-comparable!) elements -- BUT it has the non-indifferent cost of being O(N*N) while the others are about O(N). If you really want something similar to your approach:
>>> b = [x for x in a if x not in b] you'll have, o horrors!-), to do a loop, so name b is always bound to "the result list so far" (in the LC, name b is only bound at the end):
b = [] for x in a: if x not in b: b.append(x)
However, this is O(N*N) too. In terms of "easier or more intuitive", I suspect only this latter solution might qualify.
I dunno about more intuitive, but here's a fairly simple list comprehension
solution which is O(N) and preserves the order. Of course its back to
requiring hashable elements again: startList = [5,1,2,1,3,4,2,5 ,3,4] d = {} [ d.setdefault(x, x) for x in startList if x not in d ]
[5, 1, 2, 3, 4]
And for the 'I must do it on one line' freaks, here's the single expression
variant of the above: :^)
[ d.setdefault(x, x) for d in [{}] for x in startList if x not in d ]
[5, 1, 2, 3, 4]
--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Hannu Kankaanp?? wrote:
... So - he'll have a list anyway. So I can't agree with this. You don't know if his Python virtual machine is a group of monkeys. Python is supposed to be a high level language.
So, in that case those monkeys would surely be perched up on tall trees. Which is of course reasonable, as the check for existence in the set might be performed in O(ln n) instead of O(n).
Actually the Set in sets module has an average lookup of O(1), worst case O(n) (not 100% sure of worst case, but 99% sure). It's been
Hmmm -- could you give an example of that worstcase...? a _full_
hashtable would give such behavior, but Python's dicts always ensure
the underlying hashtables aren't too full...
implemented with dictionaries, which in turn are hash tables.
Yep - check it out...:
[alex@lancelot bo]$ timeit.py -c -s'import sets' -s'x=sets.Set(xr ange(100))'
'7 in x'
100000 loops, best of 3: 2.3 usec per loop
[alex@lancelot bo]$ timeit.py -c -s'import sets'
-s'x=sets.Set(xr ange(1000))' '7 in x'
100000 loops, best of 3: 2.3 usec per loop
[alex@lancelot bo]$ timeit.py -c -s'import sets'
-s'x=sets.Set(xr ange(10000))' '7 in x'
100000 loops, best of 3: 2.2 usec per loop
[alex@lancelot bo]$ timeit.py -c -s'import sets'
-s'x=sets.Set(xr ange(100000))' '7 in x'
100000 loops, best of 3: 2.2 usec per loop
[alex@lancelot bo]$ timeit.py -c -s'import sets'
-s'x=sets.Set(xr ange(1000000))' '7 in x'
100000 loops, best of 3: 2.3 usec per loop
see the pattern...?-)
Same when something is NOT there, BTW:
[alex@lancelot bo]$ timeit.py -c -s'import sets'
-s'x=sets.Set(xr ange(1000000))' 'None in x'
100000 loops, best of 3: 2.3 usec per loop
etc, etc. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Anderson |
last post by:
I have a DELETE statement that deletes duplicate data from a table. It
takes a long time to execute, so I thought I'd seek advice here. The
structure of the table is little funny. The following is NOT the table,
but the representation of the data in the table:
+-----------+
| a | b |
+-----+-----+
| 123 | 234 |
| 345 | 456 |
|
by: EoRaptor013 |
last post by:
I'm having trouble figuring out how to delete some _almost_ duplicate
records in a look-up table. Here's the table:
CREATE TABLE (
(16) NOT NULL ,
(2) NOT NULL ,
(20) NULL ,
(50) NULL ,
NULL ,
CONSTRAINT PRIMARY KEY CLUSTERED
(
|
by: Smythe32 |
last post by:
If anyone could help, I would appreciate it.
I have a table as listed below.
I need to check for duplicates by the OrderItem field and if there are
duplicates, it then needs to keep the OrderItem with the highest
interval and discard all other duplicates for that OrderItem number.
If the OrderItem and the Interval are exactly the same, it just needs
to keep one entry for the OrderItem.
|
by: nitinloml |
last post by:
well i m having a text file which contain time,user name & id, now if i want to modify the time without affecting the id & user name is it possible?
or how i delete that list & enter a new list in that text file
|
by: Mokita |
last post by:
Hello,
I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String).
for (int k=0; k<myLength; k++){
boolean isthere=false;
for (int l=0; l<sizeres; l++){
if (res.equals(my)){
| |
by: allingame |
last post by:
Need help with append and delete duplicates
I have tables namely
1)emp, 2)time and 3)payroll
TABLE emp
ssn text U]PK
name text
|
by: watertraveller |
last post by:
Hi all. My ultimate goal is to return two columns, where no single value appears anywhere twice. This means that not only do I want to check that nothing from column A appears in column B and vice-versa, but I also don't want the same value appearing twice in A and twice in B.
So far I have:
--Diff the columns
INSERT INTO @Table
SELECT One, Two
FROM @Column1 a
FULL OUTER JOIN @Column2 b
|
by: moon24 |
last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7
then the result should be 2 7 1 12
here is what I have:
#include <iostream>
using namespace std;
class NumberList
{
|
by: KimmyG |
last post by:
I'm just starting to use SQL and am much more experienced in Access.
Here is what I do in Access
Copy a table and rename the new table "copytable" also select structure only.
Open "copytable" and highlight columns where I want to delete duplicates if
there are more than one.
Put a primary key on Outreach name and days.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |