如何根据组别将元素拆分成不同的行?

时间:2017-01-31 19:15:01

标签: python unix output

如何使用Unix命令或Python / perl将所有元素分成不同的行?

输入文件:

Group1        m1 m2 m3 
Group2        m4 
Group3        m5 m6

输出:

m1 Group1
m2 Group1
m3 Group1
m4 Group2
m5 Group3
m6 Group3

4 个答案:

答案 0 :(得分:2)

你可以读取文件并输出如下:

with open("read.txt", "r") as infile:
    groups = {}
    for i in infile:
        groups[i.split("        ")[0].split("Group")[1]] = i.strip("\n").split("        ")[1].split(" ")

with open("output.txt", "w") as outfile:
    for i in range(len(groups)):
        for j in groups[str(i+1)]:
            if j != "":
                outfile.write("Group"+str(i+1)+" "+j+"\n")

字典组包含组的所有m *元素。写入输出文件时,我们可以迭代这些组中的组和m *,并逐行将它们写入输出文件。

答案 1 :(得分:2)

假设您的输入文件名是“input.txt”。

all_members = dict()
with open('input.txt', 'r') as f:
    for line in f:
        line_tokens = line.split()
        group_members = line_tokens[1:]
        for member in group_members:
            all_members[member] = line_tokens[0]

for member in sorted(all_members.keys()):
    print '{} {}'.format(member, all_members[member])

打印出来:

m1 Group1
m2 Group1
m3 Group1
m4 Group2
m5 Group3
m6 Group3

答案 2 :(得分:2)

保持简单:

awk '{for(i=2;i<=NF;++i)print $i, $1}' input_file 

答案 3 :(得分:1)

您可以拆分字符串,然后使用字典存储它们:

# go through each line in the file, and split into a dict
members = {}
for group, member_string in (x.split('\t', 1) for x in groups_file):
    members.update({member: group for member in member_string.split()})

# print the resulting elements from the dict
for member in sorted(members):
    print(member, members[member])

使用此数据:

groups_file = [x.strip() for x in """
    Group1\tm1 m2 m3
    Group2\tm4
    Group3\tm5 m6
""".split('\n')[1:-1]]

打印此数据:

m1 Group1
m2 Group1
m3 Group1
m4 Group2
m5 Group3
m6 Group3