python中的简单列表删除不起作用

2024-09-30 01:27:30 发布

您现在位置:Python中文网/ 问答频道 /正文

试图从元组列表中删除元组。从输出看来它可以删除。。。但它却给出了ValueError:列表.删除(x) :x不在列表中

oldPosition = 0
newPosition = 0
counter = -1
newPartition = copy.deepcopy(partitions)
# Find which cluster the current point is in and where the move-to cluster is
for clusters in partitions:
    counter += 1
    if (inCluster(point,clusters)):
        oldPosition = counter
    if (cluster == clusters):
        newPosition = counter

# Do the removal and appending
print "Old: " + str(oldPosition) + "New: " + str(newPosition)
print "Remove From =  " + str(newPartition[oldPosition])
print "Remove = " + str(point)
newPartition[oldPosition].remove(point)

输出是

^{pr2}$

但它说(2.64,3.3,5.24)不在名单中?在

编辑:完整代码

# Return whether a point is in a cluster
def inCluster(point, cluster):
    for points in cluster:
        if (point == points):
            return True

    return False                    # Default return: will only reach here if no points match

# Given a cluster, return the coordinates corresponding to its center of gravity
def centerOfGravity(cluster):
    total = (0,0,0)
    count = 0
    for points in cluster:
        total = tuple(map(operator.add, total, points))
        count += 1
    return (total[0] / count, total[1] / count, total[2] / count)

# Returns new partition after moving specified point from its old cluster to the new cluster
def moveToNewCluster(point, cluster, partitions):
    oldPosition = 0
    newPosition = 0
    counter = -1
    newPartition = copy.deepcopy(partitions)
    # Find which cluster the current point is in and where the move-to cluster is
    for clusters in partitions:
        counter += 1
        if (inCluster(point,clusters)):
            oldPosition = counter
        if (cluster == clusters):
            newPosition = counter

    # Do the removal and appending
    print "Old: " + str(oldPosition) + "New: " + str(newPosition)
    print "Remove From =  " + str(newPartition[oldPosition])
    print "Remove = " + str(point)
    newPartition[oldPosition].remove(point)
    newPartition[newPosition].append(point)

    # Clean up the new partition (we created at most one empty partition)
    newPartition.remove([])

    return newPartition

# Return a list of centers of gravity given the new partitions
def allCentersOfGravity(partitions):
    partitionsCoG = []
    for clusters in partitions:
        partitionsCoG.append(centerOfGravity(clusters))
    return partitionsCoG

# Find the euclidean distance in 3 space between two points
def distance(a, b):
    return math.sqrt(pow(a[0]-b[0], 2) + pow(a[1]-b[1], 2) + pow(a[2]-b[2], 2))

# Find the smallest absolute value distance from a point to a center of gravity
def minCenterDistance(point, centerOfGravities):
    minimumDistance = 10000000000
    dist = 0
    for center in centerOfGravities:
        dist = abs(distance(point, center))
        if (dist < minimumDistance):
            minimumDistance = dist
    return minimumDistance

# Given a set of points and partitions of those points, calculate the suqard error distortion
def squaredErrorDistortion(points, centerOfGravities):
    total = 0
    for point in points:
        total += pow(minCenterDistance(point, centerOfGravities), 2)
    total = total / len(points)
    return total

# Calculate the cost of moving point into cluster
# Value of a move is calculated as the old squared error distortion minus the new squared eror distortion
def moveValue(point, cluster, partitions, points):
    # Create the new partition formed by the swap
    newPartition = moveToNewCluster(point, cluster, partitions)
    # Get the centers of gravity for the clusters
    oldCoG = allCentersOfGravity(partitions)
    newCoG = allCentersOfGravity(newPartition)

    oldError = squaredErrorDistortion(points, oldCoG)
    newError = squaredErrorDistortion(points, newCoG)

    print "Old Partition: " + str(partitions)
    print "Old CoG: " + str(oldCoG)
    print "Old Error: " + str(oldError)
    print "New Partition: " + str(newPartition)
    print "New CoG: " + str(newCoG)
    print "New Error: " + str(newError)

    # Define the value as old - new.  If expecting the other way around results could be different
    moveErrorValue = oldError - newError

    return moveErrorValue


if __name__ == "__main__":
    # The points
    points = [(0.39, 3.3, 2.06), (0.09, 4.5, 1.63), (2.64, 3.3, 5.24), (1.64, 1.2, 3.82), (3.59, 7.5, 6.58),
              (1.94, 5.6, 4.25), (3.09, 6.5, 5.87), (0.54, 4.6, 2.27), (2.04, 4.5, 4.39), (-0.16, 3.8, 1.28)]

    # Initial Partition
    partitions = [[points[0], points[1]],
                  [points[2], points[3], points[4], points[5], points[6], points[7], points[8], points[9]]]
    '''
    # Center of Gravity test
    print centerOfGravity(partitions[0])

    # InCluster test
    print inCluster(points[0], partitions[1])
    print inCluster(points[0], partitions[0])
    '''
    # moveValue(points[2], partitions[0], partitions, points

    bestPartition = []
    tempPartition = copy.deepcopy(partitions)
    while (1==1):
        bestChangeValue = 0                                                                        # The higher the change value the better since we are doing old-new
        for cluster in tempPartition:
            for point in points:
                valueOfMove = moveValue(point, cluster, tempPartition, points)                 # Have to send partition over in order to find the cluster the point is currently in
                if (valueOfMove > bestChangeValue):
                    bestChangevalue = valueOfMove
                    bestPartition = moveToNewCluster(point, cluster, tempPartition)            # Create the new partition
        if (bestChangeValue > 0):
            tempPartition = bestPartition
        else:
            break
    # At the end, tempPartition holds the best k-means partition
    print "Best partition is: " + str(tempPartition)
    print "Error Value: " + str(squaredErrorDistortion(points, allCentersOfGravity(tempPartition)))

Tags: oftheinforreturnifpointspoint

热门问题