我有三個矩形頂點,需要找到第四個頂點,我需要找到N個矩形的缺失頂點。如何從列表中分配for循環中的值
不幸的是,我無法弄清楚如何在第一個矩形之後分配頂點:/。
下面是輸入例如文本文件:
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
使用['itertools'](https://docs.python.org/2/library/itertools.html)文檔頁面上顯示的'grouper'配方,您可以輕鬆(並且高效地)使用大塊文件三個。 –
這是我第一次在這裏註冊併發布我的問題。我真的很感謝你的幫助! – DavidC
什麼是'n'?你在哪裏申報?看來你上面貼的代碼不完整 – Ali