Thread: help with some basic python
i'm relatively new python , haven't got hang of yet. below function code have been writing. snagged on problem ii can't fathom remedy , hoping guide me how might negate issue.
context of code calculator of % frequency of values. "distro" large dictionary of integers , frequencies.
output insists "data" object created nonetype.
nonetype? why there 1 here? can around them?
thankyouphp code:
def print_stats(distro):
l = distro['total']
del distro['total']
data = [(v, k) for k, v in distro.items()]
data = sorted(data)
data = data.reverse()
data = data[:10] # this returns nonetype is not subscriptable
names=[]; numbers=[]
for k, v in data: # this returns nonetype is not iterable
names.append(str(int(v)).center(8))
numbers.append(k)
for x in range(len(numbers)):
z = (numbers[x]/l)*100
z = str(round(z, 2))
z = z +"%"
numbers[x] = z.center(7)
the problem line:
the reverse method works "in place" or "destructively", meaning modifies original list forever. makes unnecessary return reversed list, because data reversed list.code:data = data.reverse()
<rant>
yes, me,comes lispused lisp , functional programming, having destructive list reversal function makes me mad. there should functional, non-destructive version worked wanted.
</rant>
nonetype come from? well, data.reverse doesn't return anything, data becomes none... none python's way has no value nor type , it's when value function returns nothing.
so, don't reassign data @ line. data.reverse() , that's it.
edit: reason, wrote came lisp. no, that's not true: learned lisp after learning python. corrected, factual accuracy
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk help with some basic python
Ubuntu
Comments
Post a Comment