Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中为for循环赋值_Python_Loops_For Loop_Assign - Fatal编程技术网

Python 如何从列表中为for循环赋值

Python 如何从列表中为for循环赋值,python,loops,for-loop,assign,Python,Loops,For Loop,Assign,我有一个矩形的三个顶点,需要找到第四个顶点,我需要找到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 2

我有一个矩形的三个顶点,需要找到第四个顶点,我需要找到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 .....
到您的
计算功能。您可以使用三参数版本的
range

for iLine in range( 1, 3*num-1, 3):
    calculate( iLine)
第三个参数告诉它每次跳过3,您需要从第1行开始,而不是从第0行开始,因为第0行包含您的计数


您还需要获得正确的上限。您想要传递到
计算中的最终值实际上是
3*num-2
,但是请记住
范围
函数不包括上限,因此我们可以在那里使用(最高期望值+1),这就是
3*num-1
的来源。

上面的代码似乎不是您的完整代码,但我认为您应该在实际代码中更正它,如下所示:
您应该编写
item=1+i*3
@Avilar,而不是
item+=1
——这是当
num>2
您的代码建议如下:

item = 1
for i in range(0, num):
   item += i*3
当我们通过循环时

i = 0
item += 0 --> item = 1
然后

然后

然后

然后

您将生成数字

1, 4, 10, 19, 31, 46, 64
当我们想要的时候

1, 4, 7, 10, 13, 16, 19

谢谢你,乔纳森,我试过了,但它给了我一个超出范围的错误。下面是我得到的7 5、30 20、10 10是的,我被循环变量
I
和将变量
item
传递到
calculate
函数中弄糊涂了。如果您循环传递给
calculate
的同一个变量,就像我的
iLine
示例一样,我认为它更清晰。再次感谢您。我按照你的建议试过了,效果很好!我真的很感谢你的帮助!使用文档页面上显示的
grouper
配方,您可以轻松(高效)地将您的文件分成三块使用。这是我第一次在这里注册并发布我的问题。我真的很感谢你的帮助!什么是
'n
?你在哪里申报的?看起来你上面发布的代码并不完全是坏的。是的,你说得对。它应该是'n'而不是'num'!:)谢谢你的帮助@大卫,不客气。在stackoverflow中,如果有人回答了你的问题,而你觉得这很有帮助,请喜欢这个答案。谢谢,好的,我很乐意得到你的帮助。我怎么点击喜欢?我现在点击了向上箭头!:)谢谢。你可以点击我答案左边的向上箭头。我已经这样做了,当我点击它时,它说我需要有超过15个声誉才能公开展示。我不知道这算不算。无论如何,谢谢你的帮助!
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