2015-01-14 85 views
-5

重复的条目有一个文件(input.txt中)用线,如:我如何删除文件

1_306500682 2_315577060 3_315161284 22_315577259 22_315576763 

2_315578866 2_315579020 3_315163106 1_306500983 

2_315579517 3_315162181 1_306502338 2_315578919 

1_306500655 2_315579567 3_315161256 3_315161708 

由此,我只是想保持第一项中的每一行之前有重复的值_。对于上面的例子中,output.txt的应包含:

1_306500682 2_315577060 3_315161284 22_315577259 

2_315578866 3_315163106 1_306500983 

2_315579517 3_315162181 1_306502338 

1_306500655 2_315579567 3_315161256 

PLZ帮助..

+1

StackOverflow的是你张贴关于你有一个问题,而不是要求别人希望做你的工作清单问题的站点。那么你是否试图自己解决这个问题并遇到问题?你得到了什么错误?你能显示一些代码吗? – aberna

+2

是的,这就是你想要做的,这就是它应该看起来的样子 – inspectorG4dget

回答

2

从命令行Perl中,

perl -lane 'my %s;print join " ", grep /^(\d+)_/ && !$s{$1}++, @F' file 

输出

1_306500682 2_315577060 3_315161284 22_315577259 

2_315578866 3_315163106 1_306500983 

2_315579517 3_315162181 1_306502338 

1_306500655 2_315579567 3_315161256 
0
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile: 
    for line in infile: 
     seen = set() 
     nums = line.split() 
     for num in nums: 
      header = num.split("_")[0] 
      if header not in seen: 
       outfile.write(num) 
       outfile.write(" ") 
      seen.add(header) 
     outfile.write('\n') 
0

您可以使用单独的set跟踪到目前为止遇到的单词前缀,并将每行中不重复的单词收集到list中。在以这种方式处理每一行之后,可以很容易地构建一个只包含找到的非重复条目的替换行。注意:这只是inspectorG4dget当前答案的稍微更高效的版本。

with open('input.txt', 'rt') as infile, \ 
    open('non_repetitive_input.txt', 'wt') as outfile: 
    for line in infile: 
     values, prefixes = [], set() 
     for word, prefix in ((entry, entry.partition('_')[0]) 
           for entry in line.split()): 
      if prefix not in prefixes: 
       values.append(word) 
       prefixes.add(prefix) 
     outfile.write(' '.join(values) + '\n') 

输出文件的内容:

1_306500682 2_315577060 3_315161284 22_315577259 
2_315578866 3_315163106 1_306500983 
2_315579517 3_315162181 1_306502338 
1_306500655 2_315579567 3_315161256