[英]how to go through the loop and print the cyclic values
I have the following Array: 我有以下数组:
Nod= ['338', '299', '299', '300', '300', '335', '336', '314', '335', '336', '337', '338', '309', '337', '314', '309']
Now i have to write a code such that it Loops through Nod[] and assign certain variables to first two numbers, like the folowing: 现在,我必须编写一个代码,使其循环通过Nod []并将某些变量分配给前两个数字,如下所示:
Continue the loop untill Nod= = 0 继续循环,直到Nod = = 0
and the result of the code should look like 和代码的结果应该看起来像
First loop 第一循环
Node1= 338
Node2= 299
Node3= 300
In Second loop 在第二循环
Node1= 300
Node2=335
Node3=336
In Third loop 在第三循环
Node1=336
Node2=314
Node3=309
4th 第四名
Node1=309
Node2=337
Node3=338
and Loop Ends here 和循环在这里结束
I am very new to coding and python, pls help me with writing the code. 我是编码和python的新手,请帮助我编写代码。 I have tried with different Loops but couldnt be able to figure out the logic and code.
我尝试使用不同的Loop,但是无法弄清楚逻辑和代码。 Thanks in advance.
提前致谢。
node= ['338', '299', '299', '300', '300', '335', '336', '314', '335', '336', '337', '338', '309', '337', '314', '309']
while(len(node)!=3):
fst_element = node[0]
snd_element = node[1]
third_element = node[2:][node[2:].index(snd_element)+1]
print "fst: %s\t2nd: %s\t3rd: %s"%(fst_element,snd_element,third_element)
node = [x for x in node[2:] if x != fst_element]
node = [x for x in node if x != snd_element]
node = [x for x in node if x != third_element]
node.insert(0,third_element)
print "last result: " + str(node)
The above is what you want, I suppose. 我想以上就是你想要的。 I think the result of the last iteration is:
我认为最后一次迭代的结果是:
Node1=309
Node2=337
Node3=337
According to your algorithm the number 338 was already removed from the first iteration. 根据您的算法,数字338已从第一次迭代中删除。
Happy learning python. 快乐学习python。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.