Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 Python 3.3。Pygame 使用元组作为颜色。我需要将元组中的每个值减半以使颜色更深,但每秒多次。我可以使用我写的这个函数:
def halfTuple(oldTuple): newList = [] for item in oldTuple: newList.append(item * .5) return tuple(newList)
但它可能很慢。有没有更快的方法来做到这一点?
从头开始创建新元组而不是从数组转换可能会有所帮助:
def halfTuple(oldTuple): return tuple(x * 0.5 for x in oldTuple)