我需要帮助从数组中删除元组。我已经尝试过.delete,.remove和.pop以不占优势。请帮忙!
删除(红色,绿色,蓝色):从色彩图中删除给定的RGB颜色。颜色必须包含在颜色图中才能被删除。
project.pbxproj
这是我们使用python数组的数组模块。
EZARRAYS
from ezarrays import Array
class Colormap :
def __init__ (self, k) :
self._theColors = Array(k)
self._capacity = k
self._numItems = 0
def __len__(self) :
return self._numItems
def contains(self, red, green, blue) :
color = Color(red, green, blue)
for i in range(self._numItems) :
if self._theColors[i].red == red :
if self._theColors[i].green == green :
if self._theColors[i].blue == blue :
return True
return False
def add(self, red, green, blue) :
if self._numItems == self._capacity :
self._expandArray()
color = Color(red, green, blue)
index = self._numItems
self._theColors[index] = color
self._numItems = self._numItems + 1
def remove(self, red, green, blue) :
color = Color(red, green, blue)
for i in range(len(self)) :
if self._theColors[i].red == red :
if self._theColors[i].green == green :
if self._theColors[i].blue == blue :
self._theColors.remove(red)
self._theColors.delete(color.green)
self._theColors.delete(color.blue)
self._numItems = self._numItems - 1
else :
return -1
def map(self, red, green, blue) :
pass
def itBegin(self) :
pass
def itNext(self) :
pass
# Helper method to extend the length of the array.
def _expandArray(self) :
tempArray = Array( self._numItems * 2 )
self._capacity = self._capacity * 2
for i in range(len(self._theColors)) :
tempArray[i] = self._theColors[i]
self._theColors = tempArray
# Storage class holding the color values.
class Color :
def __init__ (self, red, green, blue) :
if red < 0 :
red = 0
elif red > 255 :
red = 255
self.red = red
if green < 0 :
green = 0
elif green > 255 :
green = 255
self.green = green
if blue < 0 :
blue = 0
elif blue > 255 :
blue = 255
self.blue = blue
答案 0 :(得分:0)
问题似乎出现在Colormap.remove()
中。当我想要尝试从数组中删除元素时,您似乎正在尝试删除Color
对象中的字段,该字段存储为数组中的元素。一个解决方案是创建一个新的Array并用所需的元素填充它(除了你想要删除的元素之外的所有元素)。
def remove(self, red, green, blue) :
color = Color(red, green, blue)
for i in range(len(self)) :
if self._theColors[i].red == red :
if self._theColors[i].green == green :
if self._theColors[i].blue == blue :
# Changes made below this line
self._numItems = self._numItems - 1
tempArray = Array( self._capacity )
ind = 0
for j in range(len(self)) :
if j is not i:
tempArray[ind] = self._theColors[ind]
ind = ind + 1
self._theColors = tempArray
else :
return -1
之后,看起来这样可以正常工作,除了Colormap.contains()返回None而不是False。
cmap = Colormap(1)
cmap.add(100,100,100)
print(cmap.contains(100,100,100)) # Prints "True"
cmap.remove(100,100,100)
print(cmap.contains(100,100,100)) # Prints "None"
我对ctypes库不太熟悉,但是如果你能找到一种方法来删除元素self._theColors[i]
,那就会产生同样的效果。