I want to write this code in a more pythonic way using map, reduce, filter. Can someone help me with that.
This is a simple piece of code which assigns a total value to a string based on it's position in the string.
For example, for the string abaacab
a b a a c a b
1 2 3 4 occurrence of a
1 2 occurrence of b
1 occurrence of c
1+1+2+3+1+4+2 = 14
import sys
check, total = {}, 0
for i, v in enumerate(sys.argv[1]):
if v in check:
check[v] += 1
else:
check[v] = 1
total += check[v]
print(total)
check = collections.Counter(sys.argv[1])andtotal = len(sys.argv[1])(if I'm understanding what you're trying to do correctly, which is not necessarily the case). I'd never usemap,reduceorfilterhere (nor in most Pythonic code), so I have no idea why you're asking about them.enumeratereally necessary? The indexiis never used.