在我的列表中找到重复项,稍后在代码中使用它
Find the duplicate in my list, and use it later in the code
您好,我正在尝试编写一个代码来检查列表中的重复项。我想稍后在程序中使用这个副本(它是一个谜题,其中有两个是谜题的正确答案)。
我希望将“重复”存储在“i”中 - 这样我就可以做类似的事情
如果我 = RGB_orange - 做这个或那个。举个例子
我尝试了很多不同的东西。这是我得到的最接近的。感谢任何帮助。
顺便说一句 - 该列表的内容是不同的 RGB 值。 coordiantes 是另一个列表,它是我的程序获取其 RGB 值的地方。
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
# rgblist = [(94, 197, 189), ## just an example
# (127, 176, 254),
# (233, 131, 78),
# (239, 106, 253),
# (233, 131, 78),
# (225, 81, 99),
# (126, 230, 129),
# (139, 33, 200)]
rgblist = []
rgblist.append(rgbvalue)
print(rgblist)
for i in rgblist:
if rgblist.count(i) == 2:
print(i)
print("Counted")
print(rgblist)
break
编辑:有效:
def tap_the_pair():
if pyautogui.locateCenterOnScreen('TapPair.png', confidence=0.9, region=(689, 250, 640, 1000)) != None:
Tappair = pyautogui.screenshot(region=(734, 429, 540, 540))
Tappair.save(r"C:\Users\Andreas\Desktop\pythonProject\WholeTapPair.png")
print("tap pair game found")
rgblist = []
# rgblist.append(rgbvalue)
rgb_dict = {}
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
if rgbvalue in rgb_dict:
print(rgbvalue)
return rgbvalue
else:
rgb_dict[rgbvalue] = True
您可以通过以下方式找到列表中的重复项:
import collections
rgblist = [(94, 197, 189), (127, 176, 254), (233, 131, 78), (239, 106, 253), (233, 131, 78), (225, 81, 99), (126, 230, 129), (139, 33, 200)]
duplicates = [item for item, count in collections.Counter(rgblist).items() if count > 1]
print(duplicates)
for i in duplicates:
# Do something with each duplicate
输出:
[(233, 131, 78)]
题中提供的解法也可以,但时间复杂度为O(n^2)。具有更好时间复杂度的更好方法可以是使用字典:
rgb_dict = {}
for rgb in rgblist:
if rgb in rgb_dict:
print(rgb)
break
else:
rgb_dict[rgb] = True
循环结束后,rgb
将包含重复项。此解决方案的时间复杂度为 O(n)。
编辑:试试这个特定于您的示例的代码。可以使用单个 for 循环完成
rgb_dict = {}
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
if rgbvalue in rgb_dict:
print(rgbvalue)
return rgbvalue
else:
rgb_dict[rgbvalue] = True
您好,我正在尝试编写一个代码来检查列表中的重复项。我想稍后在程序中使用这个副本(它是一个谜题,其中有两个是谜题的正确答案)。 我希望将“重复”存储在“i”中 - 这样我就可以做类似的事情 如果我 = RGB_orange - 做这个或那个。举个例子
我尝试了很多不同的东西。这是我得到的最接近的。感谢任何帮助。
顺便说一句 - 该列表的内容是不同的 RGB 值。 coordiantes 是另一个列表,它是我的程序获取其 RGB 值的地方。
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
# rgblist = [(94, 197, 189), ## just an example
# (127, 176, 254),
# (233, 131, 78),
# (239, 106, 253),
# (233, 131, 78),
# (225, 81, 99),
# (126, 230, 129),
# (139, 33, 200)]
rgblist = []
rgblist.append(rgbvalue)
print(rgblist)
for i in rgblist:
if rgblist.count(i) == 2:
print(i)
print("Counted")
print(rgblist)
break
编辑:有效:
def tap_the_pair():
if pyautogui.locateCenterOnScreen('TapPair.png', confidence=0.9, region=(689, 250, 640, 1000)) != None:
Tappair = pyautogui.screenshot(region=(734, 429, 540, 540))
Tappair.save(r"C:\Users\Andreas\Desktop\pythonProject\WholeTapPair.png")
print("tap pair game found")
rgblist = []
# rgblist.append(rgbvalue)
rgb_dict = {}
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
if rgbvalue in rgb_dict:
print(rgbvalue)
return rgbvalue
else:
rgb_dict[rgbvalue] = True
您可以通过以下方式找到列表中的重复项:
import collections
rgblist = [(94, 197, 189), (127, 176, 254), (233, 131, 78), (239, 106, 253), (233, 131, 78), (225, 81, 99), (126, 230, 129), (139, 33, 200)]
duplicates = [item for item, count in collections.Counter(rgblist).items() if count > 1]
print(duplicates)
for i in duplicates:
# Do something with each duplicate
输出:
[(233, 131, 78)]
题中提供的解法也可以,但时间复杂度为O(n^2)。具有更好时间复杂度的更好方法可以是使用字典:
rgb_dict = {}
for rgb in rgblist:
if rgb in rgb_dict:
print(rgb)
break
else:
rgb_dict[rgb] = True
循环结束后,rgb
将包含重复项。此解决方案的时间复杂度为 O(n)。
编辑:试试这个特定于您的示例的代码。可以使用单个 for 循环完成
rgb_dict = {}
for x in coordinates:
rgbvalue = Tappair.getpixel(x)
if rgbvalue in rgb_dict:
print(rgbvalue)
return rgbvalue
else:
rgb_dict[rgbvalue] = True