Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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 - Fatal编程技术网

Python 正在尝试在for中打印表

Python 正在尝试在for中打印表,python,Python,我试图在for中创建一个列表,我是这样做的: for i in PSOE_data: if i[0] + i[1] + i[2] != 0: newList.append(i) PSOE_list = PrettyTable(["Digital Chanel", "State", "Hour", "Minute", "Second"]) PSOE_list.align["Digital Chanel"] = "c" # Alinhamento pela esquerda

我试图在for中创建一个列表,我是这样做的:

for i in PSOE_data:
   if i[0] + i[1] + i[2] != 0:
        newList.append(i)

PSOE_list = PrettyTable(["Digital Chanel", "State", "Hour", "Minute", "Second"])
PSOE_list.align["Digital Chanel"] = "c" # Alinhamento pela esquerda
PSOE_list.padding_width = 1 # Espaçamento entre colunas (default)
PSOE_list.add_row([newList[i][3], newList[i][4], newList[i][0], newList[i][1], newList[i][2]])
print PSOE_list
但有回溯说:

    PSOE_list.add_row([newList[i][3], newList[i][4], newList[i][0], newList[i][1], newList[i][2]])
TypeError: list indices must be integers, not list
我该怎么处理呢? 顺便说一下,我用的是PrettyTable软件包


谢谢大家!

你的问题缺少一些信息。。。您想打印您的列表吗?如果是这样的话,也许我的printMatrix函数可以帮助你

def printMatrix(matrix):
     for row in matrix:
             for element in row:
                     print str(element) + '\t',
             print

some_list = [[0, 0, 0.0, 17, 2], [0, 0, 0.0, 25, 2],\
             [0, 1, 0.017646, 11, 1], [0, 1, 0.028056, 10, 1],\
             [0, 1, 0.028056, 12, 1], [0, 1, 1.032746, 10, 0],\
             [0, 1, 1.032746, 12, 0], [0, 1, 1.091776, 11, 0]]
another_list = ["Digital Chanel", "State", "Hour", "Minute", "Second"]

printMatrix(some_list.insert(0,another_list))

Digital Chanel  State   Hour    Minute  Second  
0       0       0.0     17      2   
0       0       0.0     25      2   
0       1       0.017646        11      1   
0       1       0.028056        10      1   
0       1       0.028056        12      1   
0       1       1.032746        10      0   
0       1       1.032746        12      0   
0       1       1.091776        11      0
我已经安装了一张漂亮的桌子,让我们试一试吧。最后,我仍然不确定我们是否在同一页上,请检查我的代码:

import prettytable
newList = []       # empty list, to be filled with ROWS from PSOE_data
PSOE_data = some_list # see code above...

for row in PSOE_data: # loop through every row in PSOE_data
   if sum(row[0:3]) != 0: # if sum three first elements != 0
        #my note: why doesn't python care about type "int" or "float"?
        newList.append(row) # appends the entire row to newList

# at this stage "newList" is a list of lists: (is this what you want?)
# [[0, 1, 0.017646, 11, 1], 
#  [0, 1, 0.028056, 10, 1], 
#  [0, 1, 0.028056, 12, 1], 
#  [0, 1, 1.032746, 10, 0], 
#  [0, 1, 1.032746, 12, 0], 
#  [0, 1, 1.091776, 11, 0]]
PSOE_list = prettytable.PrettyTable(\
               ["Digital Chanel","State", "Hour", "Minute", "Second"])
PSOE_list.align["Digital Chanel"] = "c" 
PSOE_list.padding_width = 1 

# Note how it is useful to avoid "i,j,k" notation when looping through
# rows, i.e. when the counter is not an integer number. 

# this is what you are trying to do and it is doomed to fail because:
# row = [0, 1, 1.091776, 11, 0]
# PSOE_list.add_row([ newList[row][3], newList[row][4], newList[row][0], newList[row][1], newList[row][2] ])
# But if you do:
for row in newList:
    PSOE_list.add_row([ row[3], row[4], row[0], row[1], row[2] ])
# You get:
print PSOE_list
+----------------+-------+------+--------+----------+
| Digital Chanel | State | Hour | Minute |  Second  |
+----------------+-------+------+--------+----------+
|       11       |   1   |  0   |   1    | 0.017646 |
|       10       |   1   |  0   |   1    | 0.028056 |
|       12       |   1   |  0   |   1    | 0.028056 |
|       10       |   0   |  0   |   1    | 1.032746 |
|       12       |   0   |  0   |   1    | 1.032746 |
|       11       |   0   |  0   |   1    | 1.091776 |
+----------------+-------+------+--------+----------+

什么是姿势数据,什么是姿势列表?PSOE数据-->[[0,0,0.0,17,2],[0,0,0.0,25,2],[0,1,0.017646,11,1],[0,1,0.028056,10,1],[0,1,0.028056,12,1],[0,1,1.032746,12,0],[0,1,1,1,1,1.032746,12,0],[0,1,1,1],[0,1,1,1,1,1]首先,抱歉信息的缺乏!我想要这样的东西,但主要的一点是,我不知道some_列表中存在多少元素,这就是我的for循环的原因。根据您的想法,我的一些列表由newList中的每个元素组成。我不知道我是否清楚,抱歉!