Simple exercise

Thomas 'PointedEars' Lahn PointedEars at web.de
Thu Mar 10 11:05:49 EST 2016


Thomas 'PointedEars' Lahn wrote:
[
> key = m.group(1)
> value = int(m.group(2))
>> if key not in od:
> od[key] = value
> else:
> od[key] += value
>> But there is probably an even more pythonic way to do this.
]
For example, based on the original code:
 recs = int(input())
 od = OrderedDict()
 items = []
 for _ in range(recs):
 file_input = sys.stdin.readline().strip()
 m = re.search(r"(\w.+)\s+(\d+)", file_input)
 if m: items.append(m.group(1, 2))
 od = OrderedDict(map(lambda item: (item[0], 0), items))
 for item in items: od[item[0]] += item[1]
-- 
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /