列出到元组RGB python列表

fastmen111 1 rgb list python-3.x

我有这样的清单

[255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255]

什么是转换成3对(rgb)的最佳方法,里面是元组

pixel = [(255,0,0),(255,0,0),(255,0,0),(255,0,0),(127,0,255),(127,0,255),(127,0,255),(127,0,255),(127,0,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255),(0,127,255)]

Juhil Somaiy.. 5

您可以简单地使用iter(),如下所示:

RGB = [255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 127, 0, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255, 0, 127, 255]
it = iter(RGB)
print(list(zip(it,it,it)))

如果要在元组中包含N个元素组,则只需使用zip(it * N),您将在一个元组中获得N个元素。