Bob
Bob

Reputation: 10815

list problem in Python

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)

Upvotes: 2

Views: 181

Answers (2)

TheDude
TheDude

Reputation: 3952

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.

Upvotes: 3

Aaa
Aaa

Reputation: 1854

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.

Upvotes: 1

Related Questions