You are filling some_list
with None
values in your function gen
. Change the line
return [some_list.append(random.randint(0, b)) for x in xrange(number)]
to:
return [(random.randint(0, b)) for x in xrange(number)]
سؤال
I have problem with understand how to resolve error in my code. Here it comes:
def counting_sort(some_list, max=100000):
licznik = [0] * (max+1)
for x in some_list:
licznik[x] = licznik[x]+1
i=0
for x in range(max+1):
for y in range(licznik[x]):
some_list[i]=x
i=i+1
return some_list.reverse()
def gen(number, b=100000):
some_list = []
return [some_list.append(random.randint(0, b)) for x in xrange(number)]
domain = [10000, 25000, 50000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000]
for element in domain:
print 'Results for: ' + str(element) + ' elements:'
for j in range(0, 10):
temp_list = gen(element)
start = time.time()
counting_sort(temp_list)
end = time.time() - start
print end
This is problem with line:
for x in some_list:
licznik[x] = licznik[x]+1
I guess that it should be resolved by list comprehension, but I have no idea how to transform it this way. Any helping advice? Thanks for advance.
المحلول
You are filling some_list
with None
values in your function gen
. Change the line
return [some_list.append(random.randint(0, b)) for x in xrange(number)]
to:
return [(random.randint(0, b)) for x in xrange(number)]
نصائح أخرى
Your gen
function is wrong... list.append
returns None
and modifies somelist
in place, change to:
def gen(number, b=100000):
return [random.randint(0, b) for x in xrange(number)]
To demonstrate:
some_list = []
print [some_list.append(10) for _ in xrange(5)]
# [None, None, None, None, None]
print some_list
# [10, 10, 10, 10, 10]
And you're returning the list of None
s.
You are trying to use a None
value as a key to the list licznik
on line 5.
If you modify your Gen
function to this, it should work.
def gen(number, b=100000):
return [(random.randint(0, b)) for x in xrange(number)]