# Python Code Refuses to find closest elements

**URL:** <https://forum.dynamobim.com/t/python-code-refuses-to-find-closest-elements/96956>\
**Category:** DesignScript\
**Created:** [January 9, 2024, 3:30am UTC](https://forum.dynamobim.com/t/python-code-refuses-to-find-closest-elements/96956 "2024-01-09T03:30:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahahmad](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@shahahmad](https://forum.dynamobim.com/u/shahahmad)\
**Post date:** [January 9, 2024, 3:30am UTC](https://forum.dynamobim.com/t/python-code-refuses-to-find-closest-elements/96956/1 "2024-01-09T03:30:40Z")

</div>

I wrote this code a couple of days ago and have been struggling to get it to recognize the closest elements. IN[1] is a list of elements that needs to find the closest element in IN[0]. The elements should not repeat itself in any other list other than the one it shares the shortest distance with. The elements also need to be separated by level and can not be more than 300 feet away. I have managed to achieve the data structure I want and It works fine if the elements in IN[0] are far apart but when they get too close the code gets confused. Please find the dynamo script attached and an image highlighting the problem.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/b/2b5ce4c1f2307f8001677f484c59f04a300f5fad.png)

[Test.dyn](https://forum.dynamobim.com/uploads/short-url/n7KRLrFMT71FV0hCjgrGN1O2pxD.dyn) (13.1 KB)

Thank You in advance. I am losing sleep over this.

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [January 9, 2024, 6:17am UTC](https://forum.dynamobim.com/t/python-code-refuses-to-find-closest-elements/96956/2 "2024-01-09T06:17:13Z")

</div>

This seems to be working for me with that type of dataset:

```auto
# Separate lists of device, then lists of code/distance
data1 = ["A", ["1", 100], ["2", 350], ["3", 200]]
data2 = ["B", ["4", 250], ["5", 50], ["6", 10]]
data3 = ["C", ["7", 200], ["8", 1], ["9", 999]]

# Total data set
data = [data1, data2, data3]

# Max distance of 300
maxDistance = 300

# Closest devices
devicesClosest, sources = [],[]

# For each sub-list of data...
for d in data:
    # Extract source item
    source = d[0]
    sources.append(source)
    # Get targets (all but source)
    targets = d[1:]
    # Arbitrarily large distance, no device by default
    minDistance = 999999999
    minDevice = None
    # For each target for that device...
    for t in targets:
        # Get the device and distance
        device = t[0]
        distance = t[1]
        # If the distance is less than minimum and max...
        if distance < minDistance and distance < maxDistance:
            # It becomes the new minimum device/distance
            minDevice = device
            minDistance = distance
    # Append the device if found (otherwise None)
    devicesClosest.append(minDevice)

# Print the outcome
print(sources)
print(devicesClosest)

```

If you want to exclude devices as you go (only use once per item) it is much harder, you would need to build a list of ‘used’ devices to ensure you skip any that have already been used so far. Something more like this…

```auto
# Separate lists of device, then lists of code/distance
data1 = ["A", ["1", 100], ["2", 350], ["3", 10]]
data2 = ["B", ["1", 250], ["2", 50], ["3", 10]]
data3 = ["C", ["1", 200], ["2", 1], ["3", 10]]

# Total data set
data = [data1, data2, data3]

# Max distance of 300
maxDistance = 300

# Closest devices
devicesClosest, sources, devicesUsed = [],[],[]

# For each sub-list of data...
for d in data:
    # Extract source item
    source = d[0]
    sources.append(source)
    # Get targets (all but source)
    targets = d[1:]
    # Arbitrarily large distance, no device by default
    minDistance = 999999999
    minDevice = None
    # For each target for that device...
    for t in targets:
        # Get the device and distance
        device = t[0]
        distance = t[1]
        # If the distance is less than minimum and max...
        if distance < minDistance and distance < maxDistance:
            if device not in devicesUsed:
                # It becomes the new minimum device/distance
                minDevice = device
                minDistance = distance
    # Append the device if found (otherwise None)
    devicesClosest.append(minDevice)
    devicesUsed.append(minDevice)

# Print the outcome
print(sources)
print(devicesClosest)
print(devicesUsed)

```

You will run into the classic issue of having to determine how to fairly ‘pair’ devices in a suitable order. This might require instead working across all devices per source within 300 that have not been used yet instead of in sub-sets per source.

Maybe look into bird tools Sparrow package which has a point pairing algorithm in it of this nature.

---

<div class="post-metadata">

**Author:** ![shahahmad](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@shahahmad](https://forum.dynamobim.com/u/shahahmad)\
**Post date:** [January 11, 2024, 12:15am UTC](https://forum.dynamobim.com/t/python-code-refuses-to-find-closest-elements/96956/3 "2024-01-11T00:15:10Z")

</div>

Thank You!
