37,741
社区成员
发帖
与我相关
我的任务
分享
import os
aDict = {}
if os.access('a.txt', os.R_OK):
with open('a.txt', 'rt') as fin:
for line in fin:
values = line.split(';')
if len(values) >= 2:
value = values[1]
aDict[value] = aDict.setdefault(value, 0) + 1
for (k,v) in aDict.items():
print '%s:%d' % (k, v)
e70a23e:2
efdghjra:1
abcdefg:2
print len(set(l.split(';')[1] for l in open('a.txt')))
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import re
>>> s = '''111;abcdefg;17
132;efdghjra;18
113;e70a23e;16
134;e70a23e;18
115;abcdefg;19'''
>>> res = r';(.*?);'
>>> m = re.findall(res,s)
>>> set(m)
set(['e70a23e', 'efdghjra', 'abcdefg'])
>>> print len(set(m))
3
>>> aDict = {};
fd = open('test.txt')
for line in fd:
x, y, z = line.split(';')
aDict[y] = 1
fd.close()
print aDict
#!/usr/bin/env perl
use strict;
use warnings;
my $text = join '', <DATA>;
my %names = $text =~ /^\d+;(\w+);(\d+)$/gm;
print "$_\n" for keys %names;
__DATA__
111;abcdefg;17
132;efdghjra;18
113;e70a23e;16
134;e70a23e;18
115;abcdefg;19