如何在 for 循环中从列表中赋值
How to assign values in a for loop from a list
我有一个矩形的三个顶点,需要找到第四个顶点,我需要为 N 个矩形找到缺失的顶点。
遗憾的是,我不知道如何在第一个矩形之后分配顶点:/.
这是用于输入的示例文本文件:
2 # '2' is the number of rectangles.
5 5 # (x1, y1)
5 7 # (x2, y2)
7 5 # (x3, y3)
30 20 # (x1, y1)
10 10 # (x2, y2)
10 20 # (x3, y3)
# (there could be more '**vertices**' and more than '**2**' cases)
这是我的方法:
import sys
def calculate(num):
x1 = lines[n].split()[0]
y1 = lines[n].split()[1]
x2 = lines[n+1].split()[0]
y2 = lines[n+1].split()[1]
x3 = lines[n+2].split()[0]
y3 = lines[n+2].split()[1]
print x1, y1
print x2, y2
print x3, y3
#Planning to write codes for calculation & results below inside this function.
readlines = sys.stdin.readlines() # reads
num = int(lines[0]) # assigns the number of cases
for i in range(0, num):
item += 1
calculate(item) # Calls the above function
当我 运行 这段代码时,我得到以下信息:
5 5
5 7
7 5
5 7
7 5
30 20
我想得到的是:
5 5
5 7
7 5
30 20
10 10
10 20
你想要
item += 3
在你的循环中。
再看一遍,这还不足以使它起作用。你想传递行号
1, 4, 7, 10 .....
到您的 calculate
函数。您可以使用 range
的 3 参数版本来执行此操作
for iLine in range( 1, 3*num-1, 3):
calculate( iLine)
第三个参数告诉它每次跳过 3,你需要从第 1 行开始,而不是第 0 行,因为第 0 行包含你的计数。
您还需要正确设置上限。最后要传入calculate
的值其实是3*num-2
,但是记住range
这个函数是不包含上限的,所以我们可以用(highest desired value + 1) ,这是 3*num-1
的来源。
上面的代码似乎不是你的完整代码,但我认为你应该在你的真实代码中更正如下:
而不是 item +=1
你应该写 item = 1+ i*3
.
@Avilar - 这就是当 num > 2
时代码中发生的情况
你的代码建议是这样的:
item = 1
for i in range(0, num):
item += i*3
当我们进行循环时
i = 0
item += 0 --> item = 1
然后
i = 1
item += 3 --> item = 4
然后
i = 2
item += 2*3 --> item = 10
然后
i = 3
item += 3*3 --> item = 19
然后
i = 4
item += 3*4 --> item = 31
您将生成数字
1, 4, 10, 19, 31, 46, 64
当我们想要
1, 4, 7, 10, 13, 16, 19
我有一个矩形的三个顶点,需要找到第四个顶点,我需要为 N 个矩形找到缺失的顶点。
遗憾的是,我不知道如何在第一个矩形之后分配顶点:/.
这是用于输入的示例文本文件:
2 # '2' is the number of rectangles.
5 5 # (x1, y1)
5 7 # (x2, y2)
7 5 # (x3, y3)
30 20 # (x1, y1)
10 10 # (x2, y2)
10 20 # (x3, y3)
# (there could be more '**vertices**' and more than '**2**' cases)
这是我的方法:
import sys
def calculate(num):
x1 = lines[n].split()[0]
y1 = lines[n].split()[1]
x2 = lines[n+1].split()[0]
y2 = lines[n+1].split()[1]
x3 = lines[n+2].split()[0]
y3 = lines[n+2].split()[1]
print x1, y1
print x2, y2
print x3, y3
#Planning to write codes for calculation & results below inside this function.
readlines = sys.stdin.readlines() # reads
num = int(lines[0]) # assigns the number of cases
for i in range(0, num):
item += 1
calculate(item) # Calls the above function
当我 运行 这段代码时,我得到以下信息:
5 5
5 7
7 5
5 7
7 5
30 20
我想得到的是:
5 5
5 7
7 5
30 20
10 10
10 20
你想要
item += 3
在你的循环中。
再看一遍,这还不足以使它起作用。你想传递行号
1, 4, 7, 10 .....
到您的 calculate
函数。您可以使用 range
for iLine in range( 1, 3*num-1, 3):
calculate( iLine)
第三个参数告诉它每次跳过 3,你需要从第 1 行开始,而不是第 0 行,因为第 0 行包含你的计数。
您还需要正确设置上限。最后要传入calculate
的值其实是3*num-2
,但是记住range
这个函数是不包含上限的,所以我们可以用(highest desired value + 1) ,这是 3*num-1
的来源。
上面的代码似乎不是你的完整代码,但我认为你应该在你的真实代码中更正如下:
而不是 item +=1
你应该写 item = 1+ i*3
.
@Avilar - 这就是当 num > 2
时代码中发生的情况
你的代码建议是这样的:
item = 1
for i in range(0, num):
item += i*3
当我们进行循环时
i = 0
item += 0 --> item = 1
然后
i = 1
item += 3 --> item = 4
然后
i = 2
item += 2*3 --> item = 10
然后
i = 3
item += 3*3 --> item = 19
然后
i = 4
item += 3*4 --> item = 31
您将生成数字
1, 4, 10, 19, 31, 46, 64
当我们想要
1, 4, 7, 10, 13, 16, 19