Search code examples
python-3.xmaxcounting

Start counting indexes from 1 instead of 0 of a list


I created a program to get the the max value of a list and the position of its occurrences (list starting at indexing with 1 not 0) but I can't manage to find any useful solutions. The input is always a string of numbers divided by zero.

This is my code:

inp = list(map(int,input().split()))
m = max(inp)
count = inp.count(m)
print(m)
def maxelements(seq): # @SilentGhost
    return [i for i, j in enumerate(seq) if j == m]
print(maxelements(inp))

I expect to output the maximum value and then all the positions of its occurrences. (also is it possible to do without brackets as in the example below?)

Input: 4 56 43 45 2 56 8

Output: 56

2 6


Solution

  • If you want to shift index values, you could just do

    return [i + 1 for i, j in enumerate(seq) if j == m]
    

    more generally any transformation of i or j!

    def f(i, j):
        # do whatever you want, and return something
        return i + 1
    
    return [f(i, j) for i, j in enumerate(seq) if j == m]
    

    Without brackets, as a string:

    return " ".join(str(i + 1) for i, j in enumerate(seq) if j==m)