Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Created May 30, 2012 02:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanpabloaj/2832821 to your computer and use it in GitHub Desktop.
Save juanpabloaj/2832821 to your computer and use it in GitHub Desktop.
ejemplo python: moda y mediana
#!/usr/bin/python
# -*- coding: utf-8 -*-
# http://docs.python.org/tutorial/datastructures.html
l = [ 1, 10, 4, 2, 4, 3, 3, 1, 1, 3]
print l
promedio = sum(l)/len(l)
print "largo ",len(l), ", promedio:",promedio
# moda
repeticiones = 0
for i in l:
apariciones = l.count(i)
if apariciones > repeticiones:
repeticiones = apariciones
modas = []
for i in l:
apariciones = l.count(i)
if apariciones == repeticiones and i not in modas:
modas.append(i)
print "moda:", modas
# mediana
l.sort()
print l
if len(l) % 2 == 0:
n = len(l)
mediana = (l[n/2-1]+ l[n/2] )/2
else:
mediana =l[len(l)/2]
print 'mediana:',mediana
@JoaquinHN
Copy link

Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment