C# 需要一些关于python数组的解释吗

C# 需要一些关于python数组的解释吗,c#,python,C#,Python,我正在将python算法转换为c#,我需要一些解释。所以我有一份清单: offsets2 = [[(0.1, 0.1), (0.5, 0.1), (0.9, 0.1), (1.1, 0.1)], [(0.1, 0.5), (0.5, 0.5), (0.9, 0.5), (1.1, 0.5)], [(0.1, 0.9), (0.5, 0.9), (0.9, 0.9), (1.1, 0.9)], [(0.1, 1.0), (0.

我正在将python算法转换为c#,我需要一些解释。所以我有一份清单:

offsets2 = [[(0.1, 0.1), (0.5, 0.1), (0.9, 0.1), (1.1, 0.1)],
            [(0.1, 0.5), (0.5, 0.5), (0.9, 0.5), (1.1, 0.5)],
            [(0.1, 0.9), (0.5, 0.9), (0.9, 0.9), (1.1, 0.9)],
            [(0.1, 1.0), (0.5, 1.0), (0.9, 1.0), (1.1, 1.0)]]
这是:

for offset in offsets2:
    offset = [(int(x + lane.width * dx), 
               int(y + self.canvas.row_height * dy))
              for dx, dy in offset]

我想知道dx和dy是什么?我猜是delta x和delta y,但我只是想确定一下,还要问一下如何用c#表示它们。

dx
dy
只是分配给列表中每组值的临时变量。因此,在第一次迭代中,
dx=0.1,dy=0.1
,在第二次迭代中,
dx=0.5,dy=0.1
,依此类推。

您可以放入print语句来找出您想要的内容

for offset in offsets2:
    print offset
    tmp = []
    for dx, dy in offset:# for each pair (dx,dy) of offset
        print dx, dy
        newCoords = (int(x + lane.width * dx), 
               int(y + self.canvas.row_height * dy))
        tmp.append(newCoords)
    offset = tmp[:]

>>> [(0.1, 0.1), (0.5, 0.1), (0.9, 0.1), (1.1, 0.1)]
>>> 0.1, 0.1
>>> 0.5, 0.1
>>> 0.9, 0.1
....
>>> [(0.1, 0.5), (0.5, 0.5), (0.9, 0.5), (1.1, 0.5)]
>>> 0.1, 0.5
>>> 0.5, 0.5
>>> 0.9, 0.5
代码使用了一个所谓的

它大致可以翻译为:

for offset in offsets2:
    _tmp = []
    for dx, dy in offset:
        _tmp.append((int(x + lane.width * dx), 
                     int(y + self.canvas.row_height * dy))
    offset = _tmp
offset
包含两个元组,表达式
用于偏移量中的dx,dy
在对其进行迭代时将其解压缩。这与写作是一样的:

for coord in offset:
    if len(coord) != 2:
        raise ValueError
    dx = coord[0]
    dy = coord[1]
    ...

这个问题对我来说似乎没什么用处。阅读有关列表理解的内容。我觉得这个代码有点不正确。赋值
offset=…
不会更改列表的内容,只会更改
offset
变量。在一次迭代中计算的所有内容都将在下一次迭代开始时删除。只有上一次迭代的结果可以通过
offset
获得,这可能不是您想要的结果。要更改列表的内容,请使用偏移量[:]=…。