Skip to main content

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?

php code:
def print_stats(distro):
    
distro['total']
    
del distro['total']
    
data = [(vk) for kv in distro.items()]
    
data sorted(data)
    
data data.reverse()
    
data data[:10# this returns nonetype is not subscriptable

    
names=[]; numbers=[]
    for 
kv in data:  # this returns nonetype is not iterable
        
names.append(str(int(v)).center(8))
        
numbers.append(k)

    for 
x in range(len(numbers)):
        
= (numbers[x]/l)*100    
        z 
str(round(z2))
        
+"%"
        
numbers[x] = z.center(7
thankyou

quote posted beauxesprits13 view post
php code:
def print_stats(distro):
    
distro['total']
    
del distro['total']
    
data = [(vk) for kv in distro.items()]
    
data sorted(data)
    
data data.reverse()
    
data data[:10# this returns nonetype is not subscriptable

    
names=[]; numbers=[]
    for 
kv in data:  # this returns nonetype is not iterable
        
names.append(str(int(v)).center(8))
        
numbers.append(k)

    for 
x in range(len(numbers)):
        
= (numbers[x]/l)*100    
        z 
str(round(z2))
        
+"%"
        
numbers[x] = z.center(7
the problem line:
code:
data = data.reverse()
the reverse method works "in place" or "destructively", meaning modifies original list forever. makes unnecessary return reversed list, because data reversed list.

<rant>
yes, me, comes lisp used 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

Popular posts from this blog

Warning, the Safe Path is not accessible vm3 - Joomla! Forum - community, help and support

uppercase letters in url - Joomla! Forum - community, help and support

Joomla! Update is not offering Joomla 3 - Joomla! Forum - community, help and support