简体   繁体   English

我如何从 python 中的文本文件中读取我的数据集并在其上应用矩阵

[英]how I read from text file in python my dataset and applying the matrix on it

I want to apply adjacent matrix that take data from the file text and apply matrix then the output will be zeros and ones我想应用从文件文本中获取数据并应用矩阵的相邻矩阵,然后 output 将是零和一

this is my code of the dataset stored in file text这是我存储在文件文本中的数据集代码

mylist= ['T','C','A','G']

with open("codon2.txt", "w") as f_out:
    for i in range(0,len(mylist)):
         for j in range(0,len(mylist)):
            f_out.write('\n')
            for k in range(0,len(mylist)):
             f_out.write(mylist[i]+mylist[k]+mylist[j])
             f_out.write(' ')

I totally assumed everything, hope this is what you are looking for.我完全假设了一切,希望这就是你要找的。 Following code, will set adjacency matrix (firstly containing only 0's), then convert 0's to 1's for adjacent nodes.以下代码将设置邻接矩阵(首先只包含 0),然后将相邻节点的 0 转换为 1。

class Graph:
    def __init__(self, numNodes):
        self.adjacencyMatrix = []
        for i in range(numNodes): 
            self.adjacencyMatrix.append([0 for i in range(numNodes)])
        self.numNodes = numNodes

    def addEdge(self, start, end):
        self.adjacencyMatrix[start][end] = 1


# random example
edges_list = [[1, 2], [0, 1], [2, 3]]

# assuming number of nodes is 4
graph = Graph(4)

# converts 0's to 1's for adjacent nodes
for i in edges_list:
    graph.addEdge(i[0], i[1])

with open("codon2.txt", "w") as f_out:
    for i in graph.adjacencyMatrix:
        for j in i:
            f_out.write(str(j))
        f_out.write('\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM