我有一个功能,必须同时检查两个列表中的重复。一个列表具有x值,另一个列表具有笛卡尔系统的y值。不能重复单个坐标。目前我的代码如下所示:
for q in range(0, len(prows)-1, 1):
for w in range(0, len(prows)-1, 1):
if prows[q] == prows[w] and pcols[q] == prows[w]:
prows.remove(prows[w])
pcols.remove(pcols[w])
其中prows是y值,而pcols是x值。这是有效的,问题是我的第一个for循环只在第二个for循环循环遍历其所有值后更新prows的长度。因此,我得到一个索引错误,第一个for循环仍然具有原始长度prows,而第二个for循环具有较新的长度和删除的重复。
答案 0 :(得分:1)
使用highlighted
保留其键的插入顺序(在Python 3.7中为will become part of the specification但在3.6中已经为真)的事实,这可以在一个短行中完成:
public void ChangeColor()
{
ColorBlock colorBlock = btn.colors;
colorBlock.normalColor = Color.blue;
colorBlock.highlightedColor = Color.blue;
btn.colors = colorBlock;
}
请注意,与人们可能期望的相反,这不适用于集合 - 一个人必须使用dict(具有虚拟值)。
答案 1 :(得分:0)
xlist = [ 1, 3, 5, 7, 9, 5]
ylist = [11,33,55,77,99,55] # (5,55) dupe'd
coords = list(zip(xlist,ylist)) # list of unique coords
print(coords) # still has dupes
result = [] # empty result list, no dupes allowed
setCoords = set() # remember which coords we already put into result
for c in coords: # go through original coords, one at a time
if c in setCoords: # we already added this, skip it
continue
setCoords.add(c) # add to set for comparison
result.append(c) # add to result
print(result) # no dupes, still in order of list
输出:
[(1, 11), (3, 33), (5, 55), (7, 77), (9, 99), (5, 55)] # coords, with dupes
[(1, 11), (3, 33), (5, 55), (7, 77), (9, 99)] # result, no dupes
您还可以恢复到xlist和ylist:
x2 , y2 = [list(tups) for tups in zip(*result)]
print(x2)
print(y2)
输出:
[1, 3, 5, 7, 9]
[11, 33, 55, 77, 99]