简体   繁体   English

相同的“ for”循环中的不同变量?

[英]python - different variables in same “for” loop?

i have some data (ID = Name), and i want to put the names of the given people IDs in a QTableWidget, but i'm having problems with a for loop... 我有一些数据(ID =名称),我想将给定人员ID的名称放在QTableWidget中,但是我在for循环中遇到问题...

here's my code: 这是我的代码:

for x in people:
  for y in range(len(people)):
   view.setItem(y, 0, QtGui.QTableWidgetItem(name[x][:-1][1:]))

where y in view.setItem(y, 0, QtGui.QTableWidgetItem(name[x][:-1][1:])) is the row index on table, i want to make a table with the names of the given IDs. 其中yview.setItem(y, 0, QtGui.QTableWidgetItem(name[x][:-1][1:]))是上表中的行索引,i想使一个表具有给定的ID的名称。

this is the result with 3 IDs given: 这是给出3个ID的结果:

|  Jhon   |
|  Jhon   |
|  Jhon   |

and this is what i'd like it to display: 这就是我想要显示的内容:

|  Sally  |
|  kate   |
|  Jhon   |

any help? 有什么帮助吗?

I think you want enumerate here. 我想你想在这里enumerate一下。

for idx,x in enumerate(people):
    view.setItem(idx, 0, QtGui.QTableWidgetItem(name[x][:-1][1:]))

You are looping through all the people and setting them for all the rows. 您正在遍历所有人员,并为所有行设置它们。 The person you encounter last will naturally be set for all the rows since nothing is overwriting it. 您自然会为所有行设置最后遇到的人,因为没有东西会覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM