有一个列表,其中可能包含无项目。我想删除这些项目,但前提是它们出现在列表的末尾,所以:
[None, "Hello", None, "World", None, None]
# Would become:
[None, "Hello", None, "World"]
Run Code Online (Sandbox Code Playgroud)
我已经编写了一个函数,但是我不确定这是否是在python中进行处理的正确方法?:
def shrink(lst):
# Start from the end of the list.
i = len(lst) -1
while i >= 0:
if lst[i] is None:
# Remove the item if it is None.
lst.pop(i)
else:
# We want to preserve 'None' items in the middle of the list, so stop as soon as we hit something not None.
break
# Move through the list backwards.
i -= 1 …Run Code Online (Sandbox Code Playgroud) 在阅读一本关于 3D 图形的书的一部分时,我遇到了以下作业:
const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};
Run Code Online (Sandbox Code Playgroud)
为什么需要 f 后缀?不能从变量的类型确定文字的类型吗?我相信没有 f 浮点文字被解释为双打,但为什么当数组显然是 float 类型时?