Python: searching methods

7.4.1. Contar cantidad de apariciones elementos

Método: count(elemento)

>>> nombres_masculinos = ["Alvaro", "Miguel", "Edgardo", "David", "Miguel"] 
>>> nombres_masculinos.count("Miguel") 
2
 
>>> nombres_masculinos = ("Alvaro", "Miguel", "Edgardo", "David", "Miguel") 
>>> nombres_masculinos.count("Miguel") 
2

7.4.2. Obtener número de índice

Método: index(elemento[, indice_inicio, indice_fin])

>>> nombres_masculinos.index("Miguel") 
1 
>>> nombres_masculinos.index("Miguel", 2, 5) 
4

SOURCEhttp://librosweb.es/libro/python/capitulo_7/metodos_de_busqueda_2.html

Using ElementTree to find ‘count’ tag and sum them

Some small piece of code:

import urllib
import xml.etree.ElementTree as ET

sum = 0

url = raw_input('Enter location: ')
if len(url) < 1 : exit()

print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
#print data

tree = ET.fromstring(data)
results = tree.findall('.//count')

for i in range(0, len(results)):
 sum = sum + int(results[i].text)
 
print "Count: %i" % len(results)
print "Sum: %i" % sum