improving a huge double-for cycle

Tino Wildenhain tino at wildenhain.de
Thu Sep 18 17:44:21 CEST 2008


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:])

HTH
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/4169d39b/attachment.bin>


More information about the Python-list mailing list