【问题标题】:Python create new array for different iteration of a for loopPython为for循环的不同迭代创建新数组
【发布时间】:2017-10-23 03:58:10
【问题描述】:

我想为每次迭代创建一个新数组(或列表)。这是我的代码:

import numpy as np
data2 = open('pathways.dat', 'r', errors = 'ignore')
pathways = data2.readlines()

special_line_indexes = []
PWY_ID = []
line_cont = []
L_PRMR = [] #Left primary

#i is the line number (first element of enumerate), while line is the line content (2nd elem of enumerate)
for CUI in just_compound_id:
    for i,line in enumerate(pathways):
        if '//' in line:
            #fint the indexes of the lines containing //
            special_line_indexes = i+1
        elif 'REACTION-LAYOUT -' in line:
            if CUI in line:
                PWY_ID.append(special_line_indexes)

具体来说,我想为 CUI 的不同迭代(第一个 foor 循环)创建一个不同的数组 PWY_ID。我最终得到的是一个包含所有输出的长数组。也许使用字典会更有效,但我不确定如何在 for 循环中实现它......

【问题讨论】:

    标签: python arrays for-loop


    【解决方案1】:

    您可以开始绘制您想要的数据架构。决定什么是关键,什么是价值。

    然后,您可以从创建 dict() 开始

    foo = dict()
    

    然后您将项目插入字典 foo["KEY"] = "VALUE"

    例如, foo["x"] = 12 然后,该值将存储在字典中。

    这里有一个关于如何在 python 中使用字典的教程: https://www.youtube.com/watch?v=2j7ox_zqM4g

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      相关资源
      最近更新 更多