这段 Python 代码怎么能没有一行 for 循环呢?
How can this Python code be written without the one line for loop?
我看到一个将十六进制颜色代码转换为 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))
谢谢。
它所做的只是从十六进制中获取红色、绿色和蓝色值,将其转换为整数并 return 它们作为元组
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)
我看到一个将十六进制颜色代码转换为 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))
谢谢。
它所做的只是从十六进制中获取红色、绿色和蓝色值,将其转换为整数并 return 它们作为元组 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)