# improving a huge double-for cycle

Tino Wildenhain tino at wildenhain.de
Thu Sep 18 17:53:38 CEST 2008

```Tino Wildenhain wrote:
> Hi,
>
> Alexzive wrote:
>> Hello there :) ,
>>
>> I am a python newbie and need to run following code for a task in an
>> external simulation programm called "Abaqus" which makes use of python
>> to access the mesh (ensamble of nodes with xy coordinates) of a
>> certain geometrical model.
>>
>> [IN is the starting input containing the nodes to be check, there are
>> some double nodes with the same x and y coordinates which need to be
>> removed. SN is the output containing such double nodes]
>>
>> Code: Select all
>>     for i in range(len(IN)): #scan all elements of the list IN
>>       for j in range(len(IN)):
>>         if i <> j:
>>          if IN[i].coordinates == IN[j].coordinates:
>>            if IN[i].coordinates == IN[j].coordinates:
>>               SN.append(IN[i].label)
>>
>
> data=dict()
> for item in IN: # (what a name! ;)
>     data.setdefault(item.coordinates,[]).append(item)
>     # provided coordinates is a tuple
>
> dupes=[items for items in data.values() if len(items)>1]
>
> should give you a resulting list of lists of elements in IN
> which occur more then once per coordinates.
> You can then decide which ones to remove or whatever.
>
> If you want to have the output only containing nodes to remove,
> the following would work:
>
> dupes=[]
> for items in data.values():
>     dupes.extend(items[1:])
>

I did a small test - I don't know if it reflects
the actual problem good enough but it seems it
all runs below 1 sec each so this would be very
fast compared to 15h:

>>> import random
>>> class Node(object):
... 	def __init__(self,x,y):
... 		self.coordinates=(x,y)

>>> IN=[Node(random.randint(0,100),random.randint(0,100)) for i in
xrange(100000)]

>>> data=dict()
>>> for item in IN: data.setdefault(item.coordinates,[]).append(item)
...

>>> dupes=[items for items in data.values() if len(items)>1]
>>> len(dupes)
10190
>>>

Cheers
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20080918/3676fbff/attachment.bin>
```