improving a huge double-for cycle

Alex Ziller zasaconsulting at gmail.com
Fri Sep 19 10:45:10 CEST 2008


Thank you all for replying me!

special thanks to Tino. Your code rocks!!!!!!!!!

I got all the double nodes in couple of seconds!!!!!!!!
and in a useful output with the coupled nodes pro line..

cheers from Germany!




2008/9/18 Tino Wildenhain <tino at wildenhain.de>

> 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[0] == IN[j].coordinates[0]:
>>>           if IN[i].coordinates[1] == IN[j].coordinates[1]:
>>>              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
>



-- 
Ing. Alessandro Zivelonghi Ziller

http://www.tecnopolis.eu

+49 176 83112471
skype: alexzive

"Energy and persistence conquer all things."
Benjamin Franklin (1706 - 1790)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080919/5c48976b/attachment.html>


More information about the Python-list mailing list