0
def singlelink(list1, list2):
    print(list1)
    print(list2)
    shortest = sys.float_info.max
    dist = 0.0
    for x1,y1 in list1:
        for x2,y2 in list2:
            dist = math.sqrt((float(x1)-float(x2))**2.0 + (float(y1)-float(y2))**2.0)  
            if dist < shortest:
                shortest = dist
    return shortest

I call the preceding function using the following

print(singlelink(['51.5217', '30.1140'], ['27.9698', '27.0568']))

When I do this, I get a ValueError: too many values to unpack (expected 2).

Each list only has two values, so unclear why it doesn't just unpack them into the variables

  • Neither `list1` nor `list2` is a list of pairs. You probably want `single link([(51.5217,30.1140)], [(27.9698,27.0568)])`, though it's not entirely clear if that's the case. What exactly is `singlelink` supposed to compute? – chepner Apr 07 '20 at 20:46
  • `for x1,y1 in list1` expects _each separate item_ in list1 to have exactly two elements. But the first item `'51.5217'` has seven elements. – John Gordon Apr 07 '20 at 20:53
  • Thanks. Both these responses help point me in the right direction of where my issue is. – jdayno22 Apr 07 '20 at 21:12
  • Does this answer your question? [Python ValueError: too many values to unpack](https://stackoverflow.com/questions/7053551/python-valueerror-too-many-values-to-unpack) – AMC Apr 07 '20 at 21:34
  • To elaborate on what I want to accomplish with singlelink function, I want it to accept two lists of x and y coordiantes. It then compares the two lists and returns the distance between the two closest coordinate pairs in the two lists. Example list1 may be [[1.0,1.0],[2.0,2.0]] and list2 may be [[3.0,3.0],[4.0,4.0]]. The function should then return the eudclidean distance between 2.0,2.0 and 3.0,3.0 – jdayno22 Apr 07 '20 at 21:37
  • The function works if my input has more than one list in each list. For example: if list1 = [['51.5217', '30.1140'], ['27.9698', '27.0568']] and list2 = [['10.6233', '52.4207'], ['122.1483', '6.9586']]. However, I need to be able to deal with the edge case when the list only has a single pair of coordinates. – jdayno22 Apr 07 '20 at 22:02

4 Answers4

2

List or tuple unpacking in python works in the following way. For example, list1 contains two elements so you will unpack as a, b = list1. You can solve the question in following way instead of using loops.

def singlelink(list1, list2):
    print(list1)
    print(list2)
    shortest = sys.float_info.max
    dist = 0.0
    x1, y1 = list1
    x2, y2 = list2
    dist = math.sqrt((float(x1)-float(x2))**2.0 + (float(y1)-float(y2))**2.0)  
    if dist < shortest:
        shortest = dist
    return shortest
HTRS
  • 91
  • 3
0

It's not possible to do that in a loop.

If there are only 2 elements in each list, you can unpack them in advance like this.

def singlelink(list1, list2):
    print(list1)
    print(list2)
    shortest = sys.float_info.max
    dist = 0.0
    x1, y1 = list1
    x2, y2 = list2
    dist = math.sqrt((float(x1)-float(x2))**2.0 + (float(y1)-float(y2))**2.0)  
    if dist < shortest:
        shortest = dist
    return shortest
TQA
  • 267
  • 3
  • 12
0

This would be due to the way you are handling the lists:

for x1,y1 in list1:
        for x2,y2 in list2:

This is causing the issue, as you can only unpack a single item of a list at a time. I think what you may be trying to do is to send them as a list of tuples, which would be different logic.

x1,x2 = list1
y1,y2 = list2

You could also just access the elements of the list if you know they will only ever have an index < 2. I would only suggest this if you are always going to use lists, or want to expand this to longer lists.

dist = math.sqrt((float(list1[0])-float(list1[1]))**2.0 + (float(list2[0])-float(list2[1]))**2.0) 
pypalms
  • 461
  • 4
  • 12
0

In order to unpack in the way you want, you don't need any loops, just provide the coordinates as tuples:

def singlelink(tup1, tup2):
    shortest = sys.float_info.max
    dist = 0.0
    x1,y1 = tup1
    x2,y2 = tup2
    dist = math.sqrt((float(x1)-float(x2))**2.0 + (float(y1)-float(y2))**2.0)  
    if dist < shortest:
        shortest = dist
    return shortest

print(singlelink(('51.5217', '30.1140'), ('27.9698', '27.0568')))
Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Thanks. Here is my dilemma. I want to use the singlelink function to compare two lists of coordinates and find the shortest distance two sets of coordinates between those two lists. So the example I showed was if the list only had one item. Moving forward, the list could have multiple sets of coordinates. – jdayno22 Apr 07 '20 at 21:06
  • Gotcha! So you can loop twice on list of tuples and unpack them as I suggested... – Gabio Apr 07 '20 at 21:22