with open("022_names.txt") as f: for line in f: names=sorted([w[1:-1] for w in line.split(',')]) print sum(sum(ord(c)-64 for c in w)*(i+1) for i,w in enumerate(names))