I have a list: test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Need to increase each list element in all possible ways using Python standard library. It’s a puzzle, and I’ve already done this:
test = [elem + 1 for elem in test]
for i in range(len(test)): test[i] += 1
test = map(lambda x : x + 1, test)
test = [sum(elem) for elem in zip(test, [1]*len(test))]
Any others ideas?
You could use recursion, (and you could use
TrueandFalseinstead of1and0, but that’s just crazy talk):Instead of that
+you could use[test[0] + 1].extend(recurseadd(test[1:])).You coud use
operator.addinstead of+, withfunctools.partialanditertools.imapif you wanted:You could use
itertools‘sizipandrepeat:Another method with
sum, inspired by Eren’s comment to GWW’s answer:Yyou could use
xrangeinstead ofrange, you could useitertools.count(1).next()to generate the1s…There are an infinite number of minor variations, but your three plus the recursive version seem to cover the basic ones. Eren’s reduce version is nice too.