我有一个文本文件,其中包含:
xxx_123
XXX_001
xxx_345
YYY_123
ZZZ_789
ZZZ_123
我想返回一个列表:
xxx_345
YYY_123
ZZZ_789
for item in list1:
item = item.split("_")
part1 = item[0]
part2 = item[1]
for line in list1:
line = line.split("_")
Bpart1 = line[0]
Bpart2 = line[1]
if part1 == Bpart1:
if part2>Bpart2:
print item
else:
print "Items do not match"
有什么想法吗?
答案 0 :(得分:1)
你无法一次性完成,所以你需要保留结果:
from collections import defaultdict
cache = defaultdict(int)
for item in list1:
item = item.split("_")
key = item[0]
value = int(item[1])
if cache[key] < value:
cache[key] = value
for k, v in cache.items():
print("{}_{}".format(k, v))
答案 1 :(得分:1)
from collections import OrderedDict
list1 = ['xxx_123', 'xxx_001', 'xxx_345', 'YYY_123', 'ZZZ_789', 'ZZZ_123']
d = OrderedDict()
for item in list1:
part1, part2 = item.split('_', 1)
part2 = int(part2)
if d.setdefault(part1, part2) < part2:
d[part1] = part2
for key, value in d.iteritems():
print '{}_{}'.format(key, value)
# xxx_345
# YYY_123
# ZZZ_789
答案 2 :(得分:0)
我目前有这个:
cache = defaultdict()
for item in list1:
item= item.split('_')
part1 = item[0]
part2 = int(item[1])
if cache.setdefault(part1,part2)<part2:
cache[part1] = part2
for part1,part2 in cache.iteritems():
line = "%s_%s"%(part1,part2)
print line