在某些元素不等于零的字符串数组中查找字符串

时间:2019-01-23 09:39:32

标签: python arrays list

我有一个字符串数组。如何找到不等于零的元素并打印这些字符串?我该怎么做呢?

这是我想看到的:

Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards "
Gi0/16              0         116           0         116          0           56
Gi0/3               0           0           23          0          0       315887
Po1                 0      232464           0      232464          0     21331230

我的数组:

array = [
        "Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards ", 
        "Gi0/1               0           0           0           0          0       212086 ", 
        "Gi0/2               0           0           0           0          0       184832 ", 
        "Gi0/3               0           0           23          0          0       315887 ", 
        "Gi0/4               0           0           0           0          0       253439 ", 
        "Gi0/5               0           0           0           0          0       135034 ", 
        "Gi0/6               0           0           0           0          0       132617 ", 
        "Gi0/7               0           0           0           0          0         5816 ", 
        "Gi0/8               0           0           0           0          0        12266 ", 
        "Gi0/9               0           0           0           0          0       573217 ", 
        "Gi0/10              0           0           0           0          0       844880 ", 
        "Gi0/11              0           0           0           0          0       264766 ", 
        "Gi0/12              0           0           0           0          0       197496 ", 
        "Gi0/13              0           0           0           0          0            0 ", 
        "Gi0/14              0           0           0           0          0       372725 ", 
        "Gi0/15              0           0           0           0          0     14260434 ", 
        "Gi0/16              0         116           0         116          0           56 ", 
        "Gi0/17              0           0           0           0          0      6905901 ", 
        "Gi0/18              0           0           0           0          0       333387 ", 
        "Po1                 0      232464           0      232464          0     21331230 ", 
]

4 个答案:

答案 0 :(得分:1)

ll= [ x.split() for x in array]
df = pd.DataFrame(ll)
df.columns = df.iloc[0]
df = df.reindex(df.index.drop(0))
df[['Align-Err', 'FCS-Err', 'Xmit-Err', 'Rcv-Err', 'UnderSize', 'OutDiscards']] = df[['Align-Err', 'FCS-Err', 'Xmit-Err', 'Rcv-Err', 'UnderSize', 'OutDiscards']].astype(int)

我找不到想要查找不等于0的元素的部分。什么元素?什么列?您每行都有零。

df = df[df != 0].dropna()

答案 1 :(得分:1)

# your code goes here
array = [
        'Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards ', 
        'Gi0/1               0           0           0           0          0       212086 ', 
        'Gi0/2               0           0           0           0          0       184832 ', 
        'Gi0/3               0           0           23          0          0       315887 ', 
        'Gi0/4               0           0           0           0          0       253439 ', 
        'Gi0/5               0           0           0           0          0       135034 ', 
        'Gi0/6               0           0           0           0          0       132617 ', 
        'Gi0/7               0           0           0           0          0         5816 ', 
        'Gi0/8               0           0           0           0          0        12266 ', 
        'Gi0/9               0           0           0           0          0       573217 ', 
        'Gi0/10              0           0           0           0          0       844880 ', 
        'Gi0/11              0           0           0           0          0       264766 ', 
        'Gi0/12              0           0           0           0          0       197496 ', 
        'Gi0/13              0           0           0           0          0            0 ', 
        'Gi0/14              0           0           0           0          0       372725 ', 
        'Gi0/15              0           0           0           0          0     14260434 ', 
        'Gi0/16              0         116           0         116          0           56 ', 
        'Gi0/17              0           0           0           0          0      6905901 ', 
        'Gi0/18              0           0           0           0          0       333387 ', 
        'Po1                 0      232464           0      232464          0     21331230 ', 
]

# Splits at space 
for row in array:
    rowData = row.split()
    if(rowData[2] != '0'):
        print(row)

产生

Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards   
Gi0/16              0         116           0         116          0           56   
Po1                 0      232464           0      232464          0     21331230 

答案 2 :(得分:1)

请修改您的问题,不清楚。

我假设您希望一个或多个'something-Err'列的值不为零

您可以执行以下操作:

def f(arr):
    yield arr[0] # labels
    for elem in arr[1:]:
        _, align_err, fcs_err, xmit_err, rcv_err, *_ = elem.split()
        if any(map(lambda s: int(s) != 0, (align_err, fcs_err, xmit_err, rcv_err))):
            yield elem

然后像这样使用它:


>>> for elem in f(array):
...     print(elem)
...
Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards
Gi0/3               0           0           23          0          0       315887
Gi0/16              0         116           0         116          0           56
Po1                 0      232464           0      232464          0     21331230

答案 3 :(得分:1)

下面是我可以建议的两种方法:

1。简单的for循环,这里我忽略了第一个和最后一个元素:(我想这就是您所需要的)

for i in array:
    if any(x!="0" for x in i.split()[1:-1]): # here 1:-1 are the range where we are checking the values, you can edit it as per your need
        print (i)

2。同样使用列表理解

print ([i for i in array if any(x!="0" for x in i.split()[1:-1])])

输出:

Port        Align-Err     FCS-Err    Xmit-Err     Rcv-Err  UnderSize  OutDiscards 
Gi0/3               0           0           23          0          0       315887 
Gi0/16              0         116           0         116          0           56 
Po1                 0      232464           0      232464          0     21331230 

按要求添加功能的方法:

def getErrorList(array):
    return [i for i in array if any(x!="0" for x in i.split()[1:-2])] #check the range [1:-2] carefully. Use it as per your need.

print (getErrorList(array)) #take it in a variable for further processing