如果没有一行for循环,如何编写Python代码



我看到了一个将十六进制颜色代码转换为RGB颜色代码的函数。但我不太明白。怎么可能用多行for循环来写呢?还有这条线在做什么:hex[i:i + 2], 16

def hex_to_rgb(hex):
return tuple(int(hex[i:i + 2], 16) for i in (0, 2 ,4))

谢谢。

它所做的只是获得红色、绿色和amp;来自十六进制的蓝色值将其转换为integer&将它们作为元组返回https://www.rapidtables.com/convert/color/how-hex-to-rgb.html

def hex_to_rgb(hex):
rgb_lst = []
for i in (0, 2, 4):
hex_int = int(hex[i: i + 2], 16) # convert to base 16 int
rgb_lst.append(hex_int)
return tuple(rgb_lst)

相关内容

最新更新