理解 Python 实现的伪代码

Understanding pseudo-code for Python implementation

有人给了我一些伪代码,这些人已经不再质疑了。有人告诉我我需要在 Python 中实现它。在这一点上我不是在找人为我写Python中的代码,而是我试图理解伪代码以便我可以将它写在Python中。

statsOut = open('/shared/succedentsupercedent_stats','w')
supercedents = tuple(open('/path', 'r'))
succedents = tuple(open('ABCD_.raw', 'r'))

for ( cols = 1 to succedents.columns , colg = 1 to supercedents.columns )

  /**** load all users for one succedent/supercedent into an array for future stats
  xy_array = new array ( supercedents.rows , 2 )
  for ( user = 1 to supercedents.rows )
    if succedents[user,cols].isValidDouble and supercedents[user,colg].isValidDouble
      then do
        xy_array [ user , 1 ] = succedents[user,cols].toDouble
        xy_array [ user , 2 ] = supercedents[user,colg].toDouble
      enddo
    endif
  endfor

  /*** do some stats with one succedent:supercedent key for all users
    sumx = sum( of xy_array[1:supercedents.rows][1]
    sumy = sum( of xy_array[1:supercedents.rows][2]

  /*** output succedent,supercedent,sumx,sumy
    append(statsOut,succedent,supercedent,sumx,sumy)
endfor        

如果任何人都可以帮助阐明它在做什么,那将对我有很大帮助。

伪代码是指任何或多或少类似于代码但不是实际定义的语言的术语。所以用 "pseudo-code" 写的任何东西都需要对具体意图进行一些猜测。通常,它的含义应该很明显。

这里猜测伪代码需要什么。目前还不完全清楚需要什么,查看正在读取的实际数据和要生成的样本将非常有帮助。没有示例输入和输出,很难弄清楚代码应该完成什么。

def main():
    succedents = load_succedents('ABCD_reddob_cases.raw')
    supercedents = load_supercedents('/shared/voom_e_plink_cases')
    with open('/shared/succedentsupercedent_stats', 'w') as stats_out:
        for cols in range(len(succedents[0])):
            for colg in range(len(supercedents[0])):
                xy_array = [[0] * 2 for _ in range(len(supercedents))]
                for user in range(len(supercedents)):
                    s1, s2 = succedents[user][cols], supercedents[user][colg]
                    if is_valid_double(s1) and is_valid_double(s2):
                        xy_array[user] = to_double(s1), to_double(d2)
                sumx = sum(row[0] for row in xy_array)
                sumy = sum(row[1] for row in xy_array)
                append(stats_out, cols, colg, sumx, sumy)

def load_supercedents(path):
    with open(path) as file:
        # perform whatever you need to do for loading the supercedents
        # some sort of data conversion may need to take place here
        supercedents = tuple(file)
    return supercedents

def load_succedents(path):
    with open(path) as file:
        # perform whatever you need to do for loading the succedents
        # some sort of data conversion may need to take place here
        succedents = tuple(file)
    return succedents

def is_valid_double(data):
    # determine if the data is a valid double or not
    return True or False

def to_double(data):
    # convert the data into whatever a double happens to be
    return data

def append(file, s, g, x, y):
    # write the data to your file
    print('cols = {s}, colg = {g}, sumx = {x}, sumy = {y}'.format(**locals()),
          file=file, flush=True)

if __name__ == '__main__':
    main()