如何在Python中检查矢量/矩阵中每个元素的值

时间:2017-06-13 17:45:18

标签: python loops matrix iteration vectorization

这当然很简单,但现在我已经尝试了几个小时。 我想检查一个10x1矩阵中所有值的值,如果它大于它们中的任何值,它应该在大于的元素之前插入。

到目前为止,我已经尝试了以下代码的不同变体但运气不错。 我得到的是以下内容:

我尝试过:

col,col1,col2 = np.zeros((10,1)),np.zeros((10,1)),np.zeros((10,1))

for element in col:             
    if (aggdelay>element):                  
        col[n,0] = aggdelay
        col1[n,0] = flight_num
        col2[n,0] = airline_id
        break               

    n +=1
    if (n>10):
        n=0

我得到的输出如下所示:

[[ 157.]
 [   3.]
 [   6.]
 [   6.]
 [   5.]
 [   9.]
 [   0.]
 [   0.]
 [   0.]
 [   0.]]

输入是:

 19790  1256    124.0
19790   1257    157.0
19790   1258    3.0
19790   1264    6.0
19790   1266    6.0
19790   1280    5.0
19790   1282    9.0

预期输出为:

19790   1258    3.0
19790   1280    5.0
19790   1264    6.0
19790   1266    6.0
19790   1282    9.0
19790   1256    124.0
19790   1257    157.0

我实施了David提供的解决方案,但我发现更新"矩阵"与新元素。 这是我现在的解决方案,但我怀疑它没有正确更新。

 #!/usr/bin/python
import sys
import collections
import numpy as np
from operator import itemgetter

result =np.zeros((3,1))
col,col1,col2 = []*10,[]*10,[]*10
col11,col12,col23 = [],[],[]
old_flight_num, old_airline_id = None, None

lines = sys.stdin.readlines()
sumDelay1, num = 0, 1
n = 0
for line in lines:

    line, line = line.strip(), line.split("\t")

    if len(line) !=3:
        continue

    airline_id, flight_num, aggdelay = line

    try:
        aggdelay = float(aggdelay)
        flight_num= int(flight_num)
        airline_id = int(airline_id)
    except ValueError:
        continue

    if (old_airline_id is not None) and (old_airline_id != airline_id):

        res2.sort(key=itemgetter(2))

        print('                                                      ')     
        print('Here come the results for airline ID: ', (old_airline_id))
        print('                                                      ')
        for row in res2:
            print(row)      

        col,col1,col2 = []*10,[]*10,[]*10

        n=0

    if (n<10):
        col.append(airline_id),col1.append(flight_num),col2.append(aggdelay)

    else:   
        res = zip(col,col1,col2)
        res.sort(key=itemgetter(2))

        if (aggdelay>min(col2)):
            res.remove(res[0])
            col11.append(airline_id), col12.append(flight_num), col23.append(aggdelay)
            res1 = zip(col11,col12,col23)
            res2=res+res1

            res2.sort(key=itemgetter(2))
    col11,col12,col23 = [],[],[]    
    n += 1

    old_airline_id = airline_id

if (old_airline_id is not None):

    res2.sort(key=itemgetter(2))
    print('                                                      ')     
    print('Here come the results for airline ID: ', (old_airline_id))
    print('                                                      ')
    for row in res2:
        print(row)

我非常感谢对此的一些指导。 谢谢!

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助,但我不得不猜测你对输出的期望以及你如何处理输入(假设在3列中给出了航空公司ID,航班号,延误)。

import numpy as np
from operator import itemgetter

ids = [19790,19790,19790,19790,19790,19790,19790,19790]
flight_nums = [1256,1257,1258,1264,1266,1280,1282]
agg_delays = [124.0,157.0,3.,6.,6.,5.,9.]
m = zip(agg_delays,flight_nums,ids)  
m.sort(key=itemgetter(0),reverse=True)  # sorts the zipped list by delay, decreasing

matrix = np.array(m, np.float32)  # dumps the sorted list in to your matrix, an ndarray

这会为您提供一个包含3列的ndarray对象,第一列是您的“延迟”,然后是航班号,然后是航空公司ID,它按第一列排序。

如果您只对前N个感兴趣,那么只需使用上面的内容构建整个矩阵并对其进行切片:

# return the top 10, or however many:
matrix = matrix[:10]

不使用list.sort方法和切片,您可以按相反的顺序对ndarray对象进行排序:

m = zip(agg_delays,flight_nums,ids)
matrix = np.array(m, np.float32)
matrix.sort(0)
# return the top 10, or however many:
matrix = matrix[::-1][:10]