列出复制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()来制作副本。
谢谢!
I'm coding the final part of a clustering program, I want to parse a file like
--
COLOR
-
POINT COLOR
...
where
COLOR = (R,G,B)
POINT = (X,Y)
Example:
--
(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)
I want to save all this information in different classes to process the data easier.
Here is my code in 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)
For some reason, after the while loop, when I print all clusters objects, I see that all contains the same pixels list, I try to do a copy with deepcopy, with [:] and with list() but makes no sense. I don't know where my error is, also I print the cluster after reset to [] and I see that is save like a reference list but I don't know why if I use list() to make a copy.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要群集实例的属性名为Pixels:
You need Cluster instance's attribute named pixels: