复制后列表更改 Python
List change after copy Python
我正在编写聚类程序的最后一部分,我想解析这样的文件
--
COLOR
-
POINT COLOR
...
哪里
COLOR = (R,G,B)
POINT = (X,Y)
示例:
--
(255,0,4)
-
(0,0) (255,0,4)
(0,1) (255,0,4)
(0,2) (255,0,4)
(0,3) (255,0,4)
--
(32,32,12)
-
(1,0) (156,0,42)
(1,1) (156,0,42)
(1,2) (156,0,42)
(1,3) (156,0,42)
我想将所有这些信息保存在不同的地方类以便更容易地处理数据。
这是我在 python 中的代码:
#!/usr/bin/env python3
import sys
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Color:
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
class Pixel:
def __init__(self, point, color):
self.point = point
self.color = color
class Cluster:
def __init__(self, meanColor, pixels):
self.meanColor = meanColor
self.pixels = pixels
def printCluster(cl):
print("---------------")
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
print("I have " + str(len(pixels)) + " pixels.")
for px in pixels:
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")
def printClusters(clusters):
for cl in clusters:
printCluster(cl)
def getColor(line):
r = int(line.split(',')[0][1:])
g = int(line.split(',')[1])
b = int(line.split(',')[2][:-1])
color = Color(r,g,b)
return (color)
file = open(sys.argv[1], 'r')
meanColor = None
clusters = []
cluster = ""
pixels = []
import copy
count = 0
file.readline()
line = file.readline().rstrip('\n')
meanColor = getColor(line)
file.readline()
while True:
count += 1
line = file.readline().rstrip('\n')
if (line == "--"):
pixelsCopy = list(pixels)
cluster = Cluster(meanColor, pixelsCopy)
printClusters(clusters)
printCluster(cluster)
clusters.append(cluster)
pixels = []
printCluster(cluster)
line = file.readline().rstrip('\n')
meanColor = getColor(line)
file.readline()
elif (line and line != "--"):
print("-------------------------------")
printClusters(clusters)
print("line: ~" + line + "~")
x = int(line.split()[0].split(',')[0][1:])
y = int(line.split()[0].split(',')[1][:-1])
print("x: " + str(x))
print("y: " + str(y))
point = Point(x,y)
r = int(line.split()[1].split(',')[0][1:])
g = int(line.split()[1].split(',')[1])
b = int(line.split()[1].split(',')[2][:-1])
print("r: " + str(r))
print("g: " + str(g))
print("b: " + str(b))
color = Color(r,g,b)
pixels.append(Pixel(point, color))
if not line:
pixelsCopy = list(pixels)
cluster = Cluster(meanColor, pixelsCopy)
clusters.append(cluster)
#printCluster(cluster)
break
printClusters(clusters)
出于某种原因,在 while 循环之后,当我打印所有簇对象时,我发现它们都包含相同的像素列表,我尝试使用 deepcopy、[:] 和 list() 进行复制,但是没有意义。我不知道我的错误在哪里,我也在重置为 [] 后打印集群,我看到它像参考列表一样保存,但我不知道为什么如果我使用 list() 来制作副本。
谢谢!
您需要集群实例的名为 pixels 的属性:
def printCluster(cl):
print("---------------")
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
print("I have " + str(len(pixels)) + " pixels.")
for px in cl.pixels:
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")
我正在编写聚类程序的最后一部分,我想解析这样的文件
--
COLOR
-
POINT COLOR
...
哪里
COLOR = (R,G,B)
POINT = (X,Y)
示例:
--
(255,0,4)
-
(0,0) (255,0,4)
(0,1) (255,0,4)
(0,2) (255,0,4)
(0,3) (255,0,4)
--
(32,32,12)
-
(1,0) (156,0,42)
(1,1) (156,0,42)
(1,2) (156,0,42)
(1,3) (156,0,42)
我想将所有这些信息保存在不同的地方类以便更容易地处理数据。
这是我在 python 中的代码:
#!/usr/bin/env python3
import sys
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Color:
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
class Pixel:
def __init__(self, point, color):
self.point = point
self.color = color
class Cluster:
def __init__(self, meanColor, pixels):
self.meanColor = meanColor
self.pixels = pixels
def printCluster(cl):
print("---------------")
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
print("I have " + str(len(pixels)) + " pixels.")
for px in pixels:
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")
def printClusters(clusters):
for cl in clusters:
printCluster(cl)
def getColor(line):
r = int(line.split(',')[0][1:])
g = int(line.split(',')[1])
b = int(line.split(',')[2][:-1])
color = Color(r,g,b)
return (color)
file = open(sys.argv[1], 'r')
meanColor = None
clusters = []
cluster = ""
pixels = []
import copy
count = 0
file.readline()
line = file.readline().rstrip('\n')
meanColor = getColor(line)
file.readline()
while True:
count += 1
line = file.readline().rstrip('\n')
if (line == "--"):
pixelsCopy = list(pixels)
cluster = Cluster(meanColor, pixelsCopy)
printClusters(clusters)
printCluster(cluster)
clusters.append(cluster)
pixels = []
printCluster(cluster)
line = file.readline().rstrip('\n')
meanColor = getColor(line)
file.readline()
elif (line and line != "--"):
print("-------------------------------")
printClusters(clusters)
print("line: ~" + line + "~")
x = int(line.split()[0].split(',')[0][1:])
y = int(line.split()[0].split(',')[1][:-1])
print("x: " + str(x))
print("y: " + str(y))
point = Point(x,y)
r = int(line.split()[1].split(',')[0][1:])
g = int(line.split()[1].split(',')[1])
b = int(line.split()[1].split(',')[2][:-1])
print("r: " + str(r))
print("g: " + str(g))
print("b: " + str(b))
color = Color(r,g,b)
pixels.append(Pixel(point, color))
if not line:
pixelsCopy = list(pixels)
cluster = Cluster(meanColor, pixelsCopy)
clusters.append(cluster)
#printCluster(cluster)
break
printClusters(clusters)
出于某种原因,在 while 循环之后,当我打印所有簇对象时,我发现它们都包含相同的像素列表,我尝试使用 deepcopy、[:] 和 list() 进行复制,但是没有意义。我不知道我的错误在哪里,我也在重置为 [] 后打印集群,我看到它像参考列表一样保存,但我不知道为什么如果我使用 list() 来制作副本。
谢谢!
您需要集群实例的名为 pixels 的属性:
def printCluster(cl):
print("---------------")
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
print("I have " + str(len(pixels)) + " pixels.")
for px in cl.pixels:
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")