如何为app vs permission、拥有app权限的列和拥有application numb的索引创建矩阵

2024-04-24 15:14:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个矩阵,在索引上有apps编号,在列侧有权限。如果应用程序具有该权限,我希望矩阵中有1,否则为0

我有用空格分隔的权限文本文件,我已将其转换为列表。 enter image description here

我希望矩阵由1和0组成。但在“矩阵[i].iloc[app]=1”行中有“索引器:单个位置索引器超出范围”

import numpy as np
import os

def filtering(perm):
    perm_up=[]
    for item in perm:
        if item.isupper():
            perm_up.append(item)
            perm_up.sort()
    return perm_up

with open(r'C:\Users\Vikrant\Desktop\Correct Permissions\MSamples.txt','r') as f1:
    all_perm=list(set(f1.read().split(' ')))
    all_perm=filtering(all_perm)

matrix=pd.DataFrame(columns=all_perm,index=range(200))

app=0
for txtfile in os.listdir(r'C:\Users\Vikrant\Desktop\Correct Permissions\malware single'):
    #print(txtfile)
    txtfile=os.path.join(r'C:\Users\Vikrant\Desktop\Correct Permissions\malware single', txtfile)
    #print(txtfile)
    with open(txtfile) as f2:
        app+=1
        uni_perm=list(set(f2.read().split(' ')))
        uni_perm=filtering(uni_perm)
        #print(uni_perm)
        for i in uni_perm:
            for j in all_perm:
                if i==j:
                    matrix[i].iloc[app]=1

                else:
                    matrix[i].iloc[app]=0

Tags: inapp权限forosas矩阵all