【问题标题】:python: Numbering each entry respective to the kindpython:为每个条目分别编号
【发布时间】:2014-06-06 23:12:58
【问题描述】:

我的数据看起来像这样:

car    trans  +  1,4,6,8
plane  trans  +  3,5,7,9,4,3
train  trans  -  2,4,6,7
bus    trans  -  1,3,4,5,6,7,8

这需要按以下格式组织。我基本上想从第 4 列中获取“第 7 个”数字并将其放在第 4 列,它是“+”,如果它是“-”,则放在第 5 列。然后,如果它的“+”我想将​​其值加 1 并将其放在第 5 列中。如果是“-”,我想减 1 放在第 4 列

car.1    trans  +  4  5
car.2    trans  +  8  9
plane.1  trans  +  5  6
plane.2  trans  +  9  10
plane.3  trans  +  3  4
train.1  trans  -  3  4
train.2  trans  -  6  7
bus.1    trans  -  2  3
bus.2    trans  -  4  5
bus.3    trans  -  6  7

以下是我现在拥有的代码。这给出了我想要的输出,但唯一的问题是第一列上的名称没有按我想要的顺序排列。 (car.1,car.2) 我知道我必须将它指向 output.write() 行,但我不确定如何创建一个字符串,该字符串将对原始数据中逗号分隔值的元素进行编号。请帮帮我!

import sys
import string
infileName = sys.argv[1]
outfileName = sys.argv[2]

def getGenes(infile, outfile):

    infile = open(infileName,"r")
    outfile = open(outfileName, "w")

    while 1:
       line = infile.readline()
       if not line: break
       wrds = string.split(line)
       comma = string.split(wrds[3], ",")
       fivess = comma[1::2]


    if len(wrds) >= 2:
        name = wrds[0]
        chr = wrds[1]
        type = wrds[2]
        print(type)
    if type == "+":
        for jj in fivess:
            start = jj
            stop = string.atoi(jj)+1
            outfile.write('%s%s\t%s\t%s\t%s\t%s\n' %(name, , chr, type, start, stop))           
    elif type == "-":
        for jj in fivess:
            stop = jj
            start= string.atoi(jj)-1
            outfile.write('%s%s\t%s\t%s\t%s\t%s\n' %(name, ,chr, type, start, stop))   




getGenes(infileName, outfileName)

【问题讨论】:

  • 您是否考虑过将处理后的数据放入list,然后使用list.sort() 给出您想要的顺序,然后再将其写回?
  • @jonrsharpe 我只是想提出完全相同的建议。如果默认的文本顺序排序不符合标准(乍一看似乎应该如此),您可以使用key 参数传入一个函数进行比较

标签: python sorting file-io formatting


【解决方案1】:

您实际上不必在output.write() 行上这样做。理想情况下,您应该使用输入来完成,因此您可以先正确排序,然后再进行处理,而无需考虑顺序。这是我编写的代码,使用你的作为框架,但澄清/防错了一些事情:

import sys

infileName_s = sys.argv[1]
outfileName_s = sys.argv[2]

def getGenes(infileName, outfileName):

    infile = open(infileName,"r")
    outfile = open(outfileName, "w")

    x = infile.read()
    infile.close()   # make sure to close infile and outfile
    data = x.split('\n')
    alldata = []
    for line in data:
        alldata.append(line.split())
        alldata[-1][-1] = alldata[-1][-1].split(',')

    alldata = sorted(alldata) # sort

    mod_alldata = []

    for line in alldata: # create data structures
        for i in range(1, len(line[-1]), 2):
            if line[2] == '+':
                mod_alldata.append([line[0]+'.'+str(i/2+1), line[1], line[2], line[3][i], int(line[3][i])+1])
            else:
                mod_alldata.append([line[0]+'.'+str(i/2+1), line[1], line[2], int(line[3][i])-1, line[3][i]])

    for line in mod_alldata: # write to file
        outfile.write(line[0] + '\t' + line[1]+ '\t' + line[2] + '\t' + str(line[3]) + '\t' + str(line[4]) + '\n')
    outfile.close()

getGenes(infileName_s, outfileName_s)

注意事项:

  • 总是关闭您打开的文件。
  • 注意变量范围 - 您在函数内部和外部以不同方式使用了 infileName/infileoutfileName/outfile
  • 使用步长为 2 的 range(就像我在这里所做的那样:range(1, len(line[-1]), 2))对于遍历偶数索引非常有帮助,并且在奇数/空列表的情况下也很强大。李>
  • 我使用sorted() 按字母顺序排序,因为我不知道您希望它们如何排序。如果您希望它们以不同的方式排序,请在评论中告诉我。

这是指定文本文件的输出:

bus.1   trans   -   2   3
bus.2   trans   -   4   5
bus.3   trans   -   6   7
car.1   trans   +   4   5
car.2   trans   +   8   9
plane.1 trans   +   5   6
plane.2 trans   +   9   10
plane.3 trans   +   3   4
train.1 trans   -   3   4
train.2 trans   -   6   7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2017-02-11
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多