Mailing List Archive

comparing two lists
Hi,
what would be the most efficient way to do following?

I have a list of dictionaries taken from DB e.g.
dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]

and list of object instances in memory(it's just for example)
which are looping within itself and testing particular hosts

memlist = [<instance 1>,<instance 2>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 9 and memlist[1].host is msn.com etc.

Now I want to add a new instance to memlist since id=3(in dblist) is not
in memlist.
How would you iterate through it and insert a new instance?

The result should be:
memlist = [<instance 1>,<instance 2>, <instance 3>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
memlist[2].id is 9 and memlist[2].host is msn.com etc.

Furthermore, I can have the opposite situation.
This time I need to remove from memlist a host which is not in dblist.
How would you do this?

The way how it works is that DBlist is loaded every 10 minutes and
compares with memlist.
The memlist should be the same as dblist.

Could you help me, please?
I'm working on my version of this but somebody might be quicker than me.
In case I have it done I will post it.

Thanks,
Lada






There is



and wt
--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On Aug 23, 11:27 am, Ladislav Andel <lad...@iptel.org> wrote:
> Hi,
> what would be the most efficient way to do following?
>
> I have a list of dictionaries taken from DB e.g.
> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>
> and list of object instances in memory(it's just for example)
> which are looping within itself and testing particular hosts
>
> memlist = [<instance 1>,<instance 2>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>
> Now I want to add a new instance to memlist since id=3(in dblist) is not
> in memlist.
> How would you iterate through it and insert a new instance?
>
> The result should be:
> memlist = [<instance 1>,<instance 2>, <instance 3>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>
> Furthermore, I can have the opposite situation.
> This time I need to remove from memlist a host which is not in dblist.
> How would you do this?
>
> The way how it works is that DBlist is loaded every 10 minutes and
> compares with memlist.
> The memlist should be the same as dblist.
>
> Could you help me, please?
> I'm working on my version of this but somebody might be quicker than me.
> In case I have it done I will post it.
>
> Thanks,
> Lada
>
> There is
>
> and wt

On lists that change in memory, I use the following looping structure:

for i in range(len(in_memory_list)-1,-1,-1)

This loops over the list backwards. I found this method here:
http://mail.python.org/pipermail/python-list/1999-September/012451.html

Hopefully this will fulfill your needs. I've used it to great effect!

Mike

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
kyosohma@gmail.com wrote:
> On Aug 23, 11:27 am, Ladislav Andel <lad...@iptel.org> wrote:
>
>> Hi,
>> what would be the most efficient way to do following?
>>
>> I have a list of dictionaries taken from DB e.g.
>> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
>> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>
>> and list of object instances in memory(it's just for example)
>> which are looping within itself and testing particular hosts
>>
>> memlist = [<instance 1>,<instance 2>]
>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>>
>> Now I want to add a new instance to memlist since id=3(in dblist) is not
>> in memlist.
>> How would you iterate through it and insert a new instance?
>>
>> The result should be:
>> memlist = [<instance 1>,<instance 2>, <instance 3>]
>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
>> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>>
>> Furthermore, I can have the opposite situation.
>> This time I need to remove from memlist a host which is not in dblist.
>> How would you do this?
>>
>> The way how it works is that DBlist is loaded every 10 minutes and
>> compares with memlist.
>> The memlist should be the same as dblist.
>>
>> Could you help me, please?
>> I'm working on my version of this but somebody might be quicker than me.
>> In case I have it done I will post it.
>>
>> Thanks,
>> Lada
>>
>> There is
>>
>> and wt
>>
>
> On lists that change in memory, I use the following looping structure:
>
> for i in range(len(in_memory_list)-1,-1,-1)
>
>
well, actually I will need to iterate over 2 sequences which are not the
same length
and synchronize it against the dblist i have.
But I will look at your post. Thanks a lot.

Lada
> This loops over the list backwards. I found this method here:
> http://mail.python.org/pipermail/python-list/1999-September/012451.html
>
> Hopefully this will fulfill your needs. I've used it to great effect!
>
> Mike
>
>

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Ladislav Andel wrote:

> what would be the most efficient way to do following?
>
> I have a list of dictionaries taken from DB e.g.
> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>
> and list of object instances in memory(it's just for example)
> which are looping within itself and testing particular hosts
>
> memlist = [<instance 1>,<instance 2>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>
> Now I want to add a new instance to memlist since id=3(in dblist) is not
> in memlist.
> How would you iterate through it and insert a new instance?
>
> The result should be:
> memlist = [<instance 1>,<instance 2>, <instance 3>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
> memlist[2].id is 9 and memlist[2].host is msn.com etc.

You should replace the memlist with a dictionary using (host, id) tuples as
the keys. Here's an example that uses a set but requires you to modify the
<instance N> class:

dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]

class Item(object):
def __init__(self, id, host, **discarded):
self._tuple = (id, host)
def __hash__(self):
return hash(self._tuple)
def __eq__(self, other):
return self._tuple == other._tuple
def __repr__(self):
return "Item(id=%r, host=%r)" % self._tuple

items = set([Item(1, "google.com")])
for d in dblist:
item = Item(**d)
if item not in items:
print "adding", item
items.add(item)
else:
print item, "already there"

Peter
--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On Aug 23, 2007, at 11:27 AM, Ladislav Andel wrote:

> Hi,
> what would be the most efficient way to do following?
>
> I have a list of dictionaries taken from DB e.g.
> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>
> and list of object instances in memory(it's just for example)
> which are looping within itself and testing particular hosts
>
> memlist = [<instance 1>,<instance 2>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>
> Now I want to add a new instance to memlist since id=3(in dblist)
> is not
> in memlist.
> How would you iterate through it and insert a new instance?
>
> The result should be:
> memlist = [<instance 1>,<instance 2>, <instance 3>]
> memlist[0].id is 1 and memlist[0].host is google.com etc.
> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
> memlist[2].id is 9 and memlist[2].host is msn.com etc.

Well, if you add a constructor for object instances that can take
those dictionaries you could do something like:

for row in dblist.iteritems():
obj = Obj(row) # where Obj is your classname
if obj is not in memlist:
memlist.append(obj)
>
> Furthermore, I can have the opposite situation.
> This time I need to remove from memlist a host which is not in dblist.
> How would you do this?

Similarly, if you add an toHash() method to you object class you
could do something like (using the reverse iteration solution
mentioned in another reply):

for i in range(len(memlist) -1, -1, -1):
if memlist[[i].toHash() not in dblist:
del memlist[i]

Erik Jones

Software Developer | Emma®
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Peter Otten wrote:
> Ladislav Andel wrote:
>
>
>> what would be the most efficient way to do following?
>>
>> I have a list of dictionaries taken from DB e.g.
>> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
>> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>
>> and list of object instances in memory(it's just for example)
>> which are looping within itself and testing particular hosts
>>
>> memlist = [<instance 1>,<instance 2>]
>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>>
>> Now I want to add a new instance to memlist since id=3(in dblist) is not
>> in memlist.
>> How would you iterate through it and insert a new instance?
>>
>> The result should be:
>> memlist = [<instance 1>,<instance 2>, <instance 3>]
>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
>> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>>
>
> You should replace the memlist with a dictionary using (host, id) tuples as
> the keys. Here's an example that uses a set but requires you to modify the
> <instance N> class:
>
> dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
> {'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
> {'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>
> class Item(object):
> def __init__(self, id, host, **discarded):
> self._tuple = (id, host)
> def __hash__(self):
> return hash(self._tuple)
> def __eq__(self, other):
> return self._tuple == other._tuple
> def __repr__(self):
> return "Item(id=%r, host=%r)" % self._tuple
>
> items = set([Item(1, "google.com")])
> for d in dblist:
> item = Item(**d)
> if item not in items:
> print "adding", item
> items.add(item)
> else:
> print item, "already there"
>
>
Thank you for this nice solution. I wouldn't be able to write it this
way at all
but what about removing from memlist if there is less items in dblist
than in items (instances)?
I will have to iterate over items(instances) and remove that one which
is not in dblist I guess.

Lada

> Peter
>

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Well, I know it's quite ugly what I did to the code, but any
improvements are welcome.
Actually, it doesn't work as it should yet.
The items should stay the same as at the beginning.
I say in one sentence what I want to achieve:
Synchronized items with dblist.
It should remove hosts from items which are not dblist
or it should add a new host from dblist which is not in items.
I'm sorry I'm not very skilled programmer.

What O notations(algorithm complexity) I introduced to it?
I know made it much slower this way.


dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id':3, 'host':'msn.com','ip_address':'11.3.2.3'},
{'id':9, 'host':'yahoo.com','ip_address':'5.6.7.8'}]


class Item(object):
def __init__(self, id, host, **discarded):
self._tuple = (id, host)
def __hash__(self):
return hash(self._tuple)
def __eq__(self, other):
return self._tuple == other._tuple
def __repr__(self):
return "Item(id=%r, host=%r)" % self._tuple

def syncMemAndDBlist_1():
for d in dblist:
item = Item(**d)
if item not in items:
print "adding", item
items.add(item)
else:
print item, "already there"

def syncMemAndDBlist_2(a):
while a > 0:
for i in items:
if i not in dblistitems:
items.remove(i)
break
a -= 1
if len(items) == 0:
syncMemAndDBlist_1()

items = set([Item(1, "google.com"), Item(3, "yahoo.com"), Item(9,
"msn.com")])

dblistitems = set()
for d in dblist:
dblistitems.add(Item(**d))

a = len(items)
b = len(dblistitems)

if a < b:
syncMemAndDBlist_1()
else:
syncMemAndDBlist_2(a)

print 'final items are' ,items

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Ladislav Andel wrote:

> Peter Otten wrote:
>> Ladislav Andel wrote:
>>
>>
>>> what would be the most efficient way to do following?
>>>
>>> I have a list of dictionaries taken from DB e.g.
>>> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
>>> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>>> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>>
>>> and list of object instances in memory(it's just for example)
>>> which are looping within itself and testing particular hosts
>>>
>>> memlist = [<instance 1>,<instance 2>]
>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>>>
>>> Now I want to add a new instance to memlist since id=3(in dblist) is not
>>> in memlist.
>>> How would you iterate through it and insert a new instance?
>>>
>>> The result should be:
>>> memlist = [<instance 1>,<instance 2>, <instance 3>]
>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
>>> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>>>
>>
>> You should replace the memlist with a dictionary using (host, id) tuples
>> as the keys. Here's an example that uses a set but requires you to modify
>> the <instance N> class:
>>
>> dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
>> {'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>> {'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>
>> class Item(object):
>> def __init__(self, id, host, **discarded):
>> self._tuple = (id, host)
>> def __hash__(self):
>> return hash(self._tuple)
>> def __eq__(self, other):
>> return self._tuple == other._tuple
>> def __repr__(self):
>> return "Item(id=%r, host=%r)" % self._tuple
>>
>> items = set([Item(1, "google.com")])
>> for d in dblist:
>> item = Item(**d)
>> if item not in items:
>> print "adding", item
>> items.add(item)
>> else:
>> print item, "already there"
>>
>>
> Thank you for this nice solution. I wouldn't be able to write it this
> way at all

Then think twice before you use it. The dictionary approach should be
straightforward.

> but what about removing from memlist if there is less items in dblist
> than in items (instances)?
> I will have to iterate over items(instances) and remove that one which
> is not in dblist I guess.

Yes, but again, if you use a dictionary instead of a list the lookup will be
efficient.

To follow up on my previous post: with sets there is a concise spelling:

items &= set(Item(**d) for d in dblist)

or even

items.intersection_update(Item(**d) for d in dblist)

if you don't mind object identity.

Peter

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Peter Otten wrote:
> Ladislav Andel wrote:
>
>
>> Peter Otten wrote:
>>
>>> Ladislav Andel wrote:
>>>
>>>
>>>
>>>> what would be the most efficient way to do following?
>>>>
>>>> I have a list of dictionaries taken from DB e.g.
>>>> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
>>>> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>>>> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>>>
>>>> and list of object instances in memory(it's just for example)
>>>> which are looping within itself and testing particular hosts
>>>>
>>>> memlist = [<instance 1>,<instance 2>]
>>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>>> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>>>>
>>>> Now I want to add a new instance to memlist since id=3(in dblist) is not
>>>> in memlist.
>>>> How would you iterate through it and insert a new instance?
>>>>
>>>> The result should be:
>>>> memlist = [<instance 1>,<instance 2>, <instance 3>]
>>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>>> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
>>>> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>>>>
>>>>
>>> You should replace the memlist with a dictionary using (host, id) tuples
>>> as the keys. Here's an example that uses a set but requires you to modify
>>> the <instance N> class:
>>>
>>> dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
>>> {'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>>> {'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>>
>>> class Item(object):
>>> def __init__(self, id, host, **discarded):
>>> self._tuple = (id, host)
>>> def __hash__(self):
>>> return hash(self._tuple)
>>> def __eq__(self, other):
>>> return self._tuple == other._tuple
>>> def __repr__(self):
>>> return "Item(id=%r, host=%r)" % self._tuple
>>>
>>> items = set([Item(1, "google.com")])
>>> for d in dblist:
>>> item = Item(**d)
>>> if item not in items:
>>> print "adding", item
>>> items.add(item)
>>> else:
>>> print item, "already there"
>>>
>>>
>>>
>> Thank you for this nice solution. I wouldn't be able to write it this
>> way at all
>>
>
> Then think twice before you use it. The dictionary approach should be
> straightforward.
>
>
>> but what about removing from memlist if there is less items in dblist
>> than in items (instances)?
>> I will have to iterate over items(instances) and remove that one which
>> is not in dblist I guess.
>>
>
> Yes, but again, if you use a dictionary instead of a list the lookup will be
> efficient.
>
> To follow up on my previous post: with sets there is a concise spelling:
>
> items &= set(Item(**d) for d in dblist)
>
> or even
>
> items.intersection_update(Item(**d) for d in dblist)
>
> if you don't mind object identity.
>
> Peter
>
>

Thank you very much for this. I think everybody should ignore my
previous email.. Shame on me.
--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Peter Otten wrote:
> Ladislav Andel wrote:
>
>
>> Peter Otten wrote:
>>
>>> Ladislav Andel wrote:
>>>
>>>
>>>
>>>> what would be the most efficient way to do following?
>>>>
>>>> I have a list of dictionaries taken from DB e.g.
>>>> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
>>>> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>>>> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>>>
>>>> and list of object instances in memory(it's just for example)
>>>> which are looping within itself and testing particular hosts
>>>>
>>>> memlist = [<instance 1>,<instance 2>]
>>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>>> memlist[1].id is 9 and memlist[1].host is msn.com etc.
>>>>
>>>> Now I want to add a new instance to memlist since id=3(in dblist) is not
>>>> in memlist.
>>>> How would you iterate through it and insert a new instance?
>>>>
>>>> The result should be:
>>>> memlist = [<instance 1>,<instance 2>, <instance 3>]
>>>> memlist[0].id is 1 and memlist[0].host is google.com etc.
>>>> memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
>>>> memlist[2].id is 9 and memlist[2].host is msn.com etc.
>>>>
>>>>
>>> You should replace the memlist with a dictionary using (host, id) tuples
>>> as the keys. Here's an example that uses a set but requires you to modify
>>> the <instance N> class:
>>>
>>> dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
>>> {'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
>>> {'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>>>
>>> class Item(object):
>>> def __init__(self, id, host, **discarded):
>>> self._tuple = (id, host)
>>> def __hash__(self):
>>> return hash(self._tuple)
>>> def __eq__(self, other):
>>> return self._tuple == other._tuple
>>> def __repr__(self):
>>> return "Item(id=%r, host=%r)" % self._tuple
>>>
>>> items = set([Item(1, "google.com")])
>>> for d in dblist:
>>> item = Item(**d)
>>> if item not in items:
>>> print "adding", item
>>> items.add(item)
>>> else:
>>> print item, "already there"
>>>
>>>
>>>
>> Thank you for this nice solution. I wouldn't be able to write it this
>> way at all
>>
>
> Then think twice before you use it. The dictionary approach should be
> straightforward.
>
>
>> but what about removing from memlist if there is less items in dblist
>> than in items (instances)?
>> I will have to iterate over items(instances) and remove that one which
>> is not in dblist I guess.
>>
>
> Yes, but again, if you use a dictionary instead of a list the lookup will be
> efficient.
>
> To follow up on my previous post: with sets there is a concise spelling:
>
> items &= set(Item(**d) for d in dblist)
>
> or even
>
> items.intersection_update(Item(**d) for d in dblist)
>
> if you don't mind object identity.
>
> Peter
>
>
Thanks again for this solution, but I forgot to say that in the instance
is a looping call which
need to be stopped before deleting any instance from items.
So I need to call stopLoop method in the given item in items before it
gets removed.
If there is any addition to items it's quite easy to call
item.startLoop() method.


my class would look like (I use twisted but it should not make any
difference):

class Item(object):
def __init__(self, id, host, interval, **discarded):
self._tuple = (id, host)
self.interval = interval
def __hash__(self):
return hash(self._tuple)
def __eq__(self, other):
return self._tuple == other._tuple
def __repr__(self):
return "Item(id=%r, host=%r)" % self._tuple
def startLoop(self):
self.l = task.LoopingCall(self.serverTest)
self.l.start(self.interval, False)
def serverTest(self):
return getReply()
# this is just for example. It returns something on each start of the loop
# but because it sends packets within network, it takes some time before it gets a reply
# so if I remove this object instance before I get reply it gets broken somewhere else
# and I need to stop the loop before removing. Hopefully, this make sence.

def stopLoop(self):
self.l.stop()







--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
Ladislav Andel wrote:

> need to be stopped before deleting any instance from items.
> So I need to call stopLoop method in the given item in items before it
> gets removed.
> If there is any addition to items it's quite easy to call
> item.startLoop() method.

Unless you want to rely on the __del__ method or introduce another
complication (weak references) you have to be explicit:

# untested
# removing items
db_items = set(Item(**d) for d in dblist)
delenda = items - db_items
for item in delenda:
item.stopLoop()
items.remove(item)

> (I use twisted but it should not make any difference):

I've not worked with twisted, so I can't confirm that.
I can imagine that they provide their own way to spell a finalizer...

Peter
--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
I have learnt a lot from your example and used it for my purpose.
Thank you, it very helped me.
Lada

Peter Otten wrote:
> Ladislav Andel wrote:
>
>
>> need to be stopped before deleting any instance from items.
>> So I need to call stopLoop method in the given item in items before it
>> gets removed.
>> If there is any addition to items it's quite easy to call
>> item.startLoop() method.
>>
>
> Unless you want to rely on the __del__ method or introduce another
> complication (weak references) you have to be explicit:
>
> # untested
> # removing items
> db_items = set(Item(**d) for d in dblist)
> delenda = items - db_items
> for item in delenda:
> item.stopLoop()
> items.remove(item)
>
>
>> (I use twisted but it should not make any difference):
>>
>
> I've not worked with twisted, so I can't confirm that.
> I can imagine that they provide their own way to spell a finalizer...
>
> Peter
>

--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
2010/3/5 jimgardener <jimgardener@gmail.com>:
> hi
> I have two lists of names.I need to find the difference between these
> two lists.I tried to do it using sets.But I am wondering if there is a
> better way to do it.Please tell me if there is a more elegant way.
> thanks,
> jim
>
> my code snippet follows..
>
> oldlst=['jon','arya','ned','bran']
> newlst=['jaime','jon','cersei']
>
> newlyadded=set(newlst)-set(oldlst)
> removed=set(oldlst)-set(newlst)
> unchanged=set(oldlst)& set(newlst)
> print '%d were newly added= %s'%(len(newlyadded),list(newlyadded))
> print '%d were removed=%s'%(len(removed),list(removed))
> print '%d were unchanged=%s'%(len(unchanged),list(unchanged))
>
> this produces the output
> --------------
> 2 were newly added= ['jaime', 'cersei']
> 3 were removed=['ned', 'arya', 'bran']
> 1 were unchanged=['jon']
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
I guess for lists with unique items and order insignificant this is
just fine; you may also check difflib, if it suits your needs; there
are multiple comparing and displaying possibilities, e.g.:

>>> import difflib
>>> print "\n".join(difflib.unified_diff(['jon','arya','ned','bran'], ['jaime','jon','cersei']))
---

+++

@@ -1,4 +1,3 @@

+jaime
jon
-arya
-ned
-bran
+cersei
>>>

hth,
vbr
--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On 3/5/2010 3:05 AM, jimgardener wrote:
> hi
> I have two lists of names.I need to find the difference between these
> two lists.I tried to do it using sets.But I am wondering if there is a
> better way to do it.Please tell me if there is a more elegant way.
> thanks,
> jim
>
> my code snippet follows..
>
> oldlst=['jon','arya','ned','bran']
> newlst=['jaime','jon','cersei']
>
> newlyadded=set(newlst)-set(oldlst)
> removed=set(oldlst)-set(newlst)
> unchanged=set(oldlst)& set(newlst)

Except for the duplicate calls to set, this is fine. If order and
duplication are irrelevant (and hashability is guaranteed), use sets to
start with.

> print '%d were newly added= %s'%(len(newlyadded),list(newlyadded))
> print '%d were removed=%s'%(len(removed),list(removed))
> print '%d were unchanged=%s'%(len(unchanged),list(unchanged))
>
> this produces the output
> --------------
> 2 were newly added= ['jaime', 'cersei']
> 3 were removed=['ned', 'arya', 'bran']
> 1 were unchanged=['jon']


--
http://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On Wed, Feb 24, 2021 at 4:45 PM Davor Levicki <dlevicki@gmail.com> wrote:
>
> i have two lists
>
> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>
> and when I loop through the list
>
>
> list_difference = []
> for item in list1:
>
> if item not in list2:
> list_difference.append(item)
>
>
> and I managed to get the difference, but I need time as well
> because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries.
> I tried to convert it to the dictionary, but it overwrites with last key:value {'01:15' : 'def'}

Instead of a list you could have a set of tuples and then just subtract them.
--
https://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On 2021-02-25 00:42, Davor Levicki wrote:
> i have two lists
>
> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>
> and when I loop through the list
>
>
> list_difference = []
> for item in list1:
>
> if item not in list2:
> list_difference.append(item)
>
>
> and I managed to get the difference, but I need time as well
> because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries.
> I tried to convert it to the dictionary, but it overwrites with last key:value {'01:15' : 'def'}
>
If the items belong in pairs, try making the pairs first:

>>> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
>>> list(zip(list1[0 : : 2], list1[1 : : 2]))
[('01:15', 'abc'), ('01:15', 'def'), ('01:45', 'ghi')]

and then work from there.
--
https://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On 25/02/2021 01:42, Davor Levicki wrote:
> i have two lists
>
> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>
> and when I loop through the list
>
>
> list_difference = []
> for item in list1:
>
> if item not in list2:
> list_difference.append(item)
>
>
> and I managed to get the difference, but I need time as well
> because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries.
> I tried to convert it to the dictionary, but it overwrites with last key:value {'01:15' : 'def'}

The problem is underspecified, but here's my guess:

Are the even items unique? If so you can build the dicts with keys and
values swapped:

>>> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
>>> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>>> d1 = dict(zip(list1[1::2], list1[::2]))
>>> d2 = dict(zip(list2[1::2], list2[::2]))
>>> d1
{'abc': '01:15', 'def': '01:15', 'ghi': '01:45'}
>>> d2
{'abc': '01:15', 'uvz': '01:15', 'ghi': '01:45'}

To calculate the difference:

>>> d1.keys() - d2
{'def'}
>>> {d1[k]: k for k in d1.keys() - d2}
{'01:15': 'def'}

--
https://mail.python.org/mailman/listinfo/python-list
Re: comparing two lists [ In reply to ]
On 25/02/2021 01:42, Davor Levicki wrote:
> i have two lists
>
> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>
> and when I loop through the list
>
>
> list_difference = []
> for item in list1:
>
> if item not in list2:
> list_difference.append(item)
>
>
> and I managed to get the difference, but I need time as well
> because it is a separate item and 'uvz' does not mean to me anything in the list with a few thousand entries.
> I tried to convert it to the dictionary, but it overwrites with last key:value {'01:15' : 'def'}

The problem is underspecified, but here's my guess:

Are the even items unique? If so you can build the dicts with keys and
values swapped:

>>> list1 = ['01:15', 'abc', '01:15', 'def', '01:45', 'ghi' ]
>>> list2 = ['01:15', 'abc', '01:15', 'uvz', '01:45', 'ghi' ]
>>> d1 = dict(zip(list1[1::2], list1[::2]))
>>> d2 = dict(zip(list2[1::2], list2[::2]))
>>> d1
{'abc': '01:15', 'def': '01:15', 'ghi': '01:45'}
>>> d2
{'abc': '01:15', 'uvz': '01:15', 'ghi': '01:45'}

To calculate the difference:

>>> d1.keys() - d2
{'def'}
>>> {d1[k]: k for k in d1.keys() - d2}
{'01:15': 'def'}
--
https://mail.python.org/mailman/listinfo/python-list