2

within my program I have a list, and I would like a loop until all of a column of the list equals 2. All of the items in the column are numbers but some are formatted as strings and some are formatted as integers, because they are changed in other parts of the program. Below are 3 solutions I have tried, I am running python 2.7.

while (int(newlist[0][1]) != 2) and (int(newlist[1][1]) != 2) etc... != 2:

The problem I am having is that the loop is ending when only one of the list items (rather than the whole column) equals 2

while int(newlist[0-8][1]) != 2:


while int(newlist[0 and 1 and 2 and 3 and 4 and 5 and 6 and 7 and 8][1]) != 2:

If anybody could tell me what I'm doing wrong or a better way of doing it, I would really appreciate the help

4

2 に答える 2

5

括弧内の式は単一の値に評価されます:newlist[0-8]は と同じnewlist[-8]であり、newlist[0 and 1 and 2 and 3]は と同じ newlist[0]です。あなたがしたいany

while any(int(newsublist[1]) != 2 for newsublist in newlist):
于 2013-03-31T22:26:26.983 に答える
0

for ループを使用してみてください。Lあなたのリストだとしましょう。これで、次のように、それぞれがリスト自体である行を反復処理できます。

column = 1
for row in L:
  while int(row[column]) != 2:
    do_this()
    increase_column()

最終的に while テストに失敗するように、row[column] を増やして while ループが終了することを確認します。

于 2013-03-31T22:52:09.243 に答える