2

I have 2 lists: first is list that I need to work with and the second is list of -1. First of all I need to find the remainder of the divison of the number (number is len(lst)) and put it to the position (remainder of the divisor) in the list of -1. If there is already element, then to the next position in the list (if the next position is not empty, to the next and so on until it finds a position). How to realize the part that is in bold?

# -*- coding: utf-8 -*-
def fun(lst):
    count = [-1] * (len(lst) + 1)
    jar = []
    for i in range(len(lst)):
        jar.append(lst[i]%(len(lst) + 1))
        if count[jar[i]] == -1:
            count[jar[i]] = jar[i]
        else:
            arv[jar[i] + 1] = jar[i] # problem starts here
    print jar 

lst = [26, 53, 12, 65, 39, 6]
lst = fun(lst)
2
  • what is arv? (where problem starts?) Commented Apr 7, 2011 at 17:42
  • Why are you adding 1 to lst[i] % len(lst)? If you are potentially getting floats in the input list, I assume math.ceil would be better. Commented Apr 7, 2011 at 17:45

2 Answers 2

3

You introduce arv, but you never assigned it a dict. Since it is a dict, assign arv as:

arv = {}

I would also use:

for i,elem in enumerate(lst):

You can now iterate over lst, while also knowing its position.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't declare variables in Python. What you're doing is assigning an empty dict to the variable.
1

The line you indicated with a comment refers to a list that does not exist (arv) and I cannot tell what you mean by that.

2 Comments

but how then to fix the problem?
No, numbers are immutable. So, there is no problem with [-1] * len(lst).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.