简体   繁体   中英

Python beginner, strange output problem

I'm having a weird problem with the following piece of code.

from math import sqrt

def Permute(array):
    result1 = []
    result2 = []
    if len(array) <= 1:
        return array
    for subarray in Permute(array[1:]):
        for i in range(len(array)):
            temp1 = subarray[:i]+array[0]+subarray[i:]
            temp2 = [0]
            for num in range(len(array)-1):
                temp2[0] += (sqrt(pow((temp1[num+1][1][0]-temp1[num][1][0]),2) + pow((temp1[num+1][1][1]-temp1[num][1][1]),2)))
            result1.append(temp1+temp2)
    return result1

a = [['A',[50,1]]]
b = [['B',[1,1]]]
c = [['C',[100,1]]]
array = [a,b,c]

result1 = Permute(array)
for i in range(len(result1)):
    print (result1[i])
print (len(result1))

What it does is find all the permutations of the points abc and then returns them along with the sum of the distances between each ordered point. It does this; however, it also seems to report a strange additional value, 99. I figure that the 99 is coming from the computation of the distance between point a and c but I don't understand why it is appearing in the final output as it does.

The problem is that you recursively call Permute(array[1:]) , then use the recursive result to calculate temp1 . Why is this a problem? Your function outputs an array of arrays, where the last subarray is temp2 , the distance sum. So, every level of recursion, you will add more and more extra distances to your final result.

If you really want to calculate all the permutations and distances in the same function, then I suggest you return a tuple (permutation, distance) . You can then use the first part of the tuple when assigning temp1 , so that you don't accidentally add in extra distances. See this page if you aren't familiar with tuples.

I agree with what Justin Ardini said, but I would also suggest that you learn to use a Python debugger like pdb . Then you can go through functions like this and figure out what is going on for yourself. You'll learn a lot more that way.

I think what you're trying to do is:

from math import sqrt

def Permute(array):
    result1 = []
    result2 = []
    if len(array) <= 1:
        for subarray in array:
            for i in range(len(array)):
                temp1 = subarray[:i]+array[0]+subarray[i:]
                temp2 = [0]
                for num in range(len(array)-1):
                    temp2[0] += (sqrt(pow((temp1[num+1][1][0]-temp1[num][1][0]),2) + pow((temp1[num+1][1][1]-temp1[num][1][1]),2)))
                result1.append(temp1+temp2)
    return result1

a = [['A',[50,1]]]
b = [['B',[1,1]]]
c = [['C',[100,1]]]
array = [a,b,c]

result1 = Permute(array)
for i in range(len(result1)):
    print (result1[i])
print (len(result1))

I changed:

if len(array) <= 1:
    return array
for subarray in Permute(array[1:]):

to:

if len(array) <= 1:
    for subarray in array:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM