防止列表元素在算法中被重用

时间:2018-11-11 19:07:07

标签: python

我试图基于一种贪婪算法对rgb值进行排序,该算法的工作原理是获取一种随机颜色,检查自身与另一种颜色之间的距离,然后在找到距离最短的一种(欧几里得)本身,将其附加到新的数组/列表上。

我遇到了与coloringArray中没有的col [l]行有关的错误。该行的目的是确保不再使用附加到新列表中的颜色。

python

def greedy(cols):

    for i in range(len(cols)):
        col = [eval(j) for j in cols[i]]
        cols[i] = np.array(col)

    greedyResult = [] #what will contain the result
    colourArray =[]

    resultIn = []

    randomColour = random.choice(cols) #make a random choice of colours
    colourArray.append(randomColour)

    greedyResult.append(colourArray)

    colouringArray = greedyResult

    resultIn.append(randomColour)

    print(colouringArray)

    for i in range(25 - 1):
        shortestDist = 25
        closestColour = ''


        for l in range(0,25):
            currPosition = colouringArray[-1]
            checkPosition = cols[l]


            distance = euc_distance(currPosition,checkPosition)
            print(colouringArray)
            print(colours[l])
            if cols[l] not in colouringArray:

                if distance < shortestDist:
                    shortestDist = distance
                    closestColour = cols[l]
        colouringArray.append(closestColour)

    print(colouringArray)
    return colouringArray


def euc_distance(x, y):
    maths = np.sum((x-y)**2)**0.5
    print('x', x)
    print('y', y)
    return maths

错误

    if cols[l] not in colouringArray:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

仅从.txt文件加载RGB值

0.9664535356921388 0.4407325991753527 0.007491470058587191
0.9109759624491242 0.939268997363764 0.5822275730589491
0.6715634814879851 0.08393822683708396 0.7664809327917963
0.23680977536311776 0.030814021726609964 0.7887727172362835
0.3460889655971231 0.6232814750391685 0.6158156951036152
0.14855463870828756 0.18309064740993164 0.11441296968868764
0.014618780486909122 0.48675154060475834 0.9649015609162157
0.06456228097718608 0.5410881855511303 0.46589855900830957
0.6014634495610515 0.08892882999066232 0.5790026861873665
0.26958550381944824 0.5564325605562156 0.6446342341782827
0.48103637136651844 0.35523914744298335 0.249152121361209
0.9335154980423467 0.45338801947649354 0.5301612069115903
0.019299566309716853 0.5081019257797922 0.005780237417743139
0.14376842759559538 0.47282692534740633 0.3773474407725964
0.05417519864614284 0.5875285081310694 0.1640032237419612
0.5573302374414681 0.1442457216019083 0.9373070846962247
0.7709799715197749 0.9569331223494054 0.14122776441649953
0.3053927082876986 0.03958962422796164 0.27678369479169207
0.8065125051156038 0.177343035278254 0.15457051471078964
0.9547186557023949 0.154551400089043 0.8338892941512318
0.04106280627180747 0.38618352833582825 0.3495923019636925
0.3417077927504538 0.8164630567142304 0.47593490813172823
0.7828902895224036 0.4708401253088451 0.8173440068581418
0.8815675986556064 0.43959639767963965 0.781063597792481
0.8147411602576974 0.29567891564250737 0.12387667430690597

0 个答案:

没有答案