如何将下面的这个“for-loop”转换为“list-comprehension?我想从重复元素列表中创建一个非重复元素的列表。
many_colors = ['red', 'red', 'blue', 'black', 'blue']
colors = []
for c in many_colors:
if not c in colors:
colors.append(c)
# colors = ['red', 'blue', 'black']
我尝试了这个(下),但发生了未定义颜色的错误。
colors = [c for c in many_colors if not c in colors]