When I Try to use the sorted() function in python it only sorts the elements within each array alphabetically as the first 3 outputs are:
[u'A', u'a', u'a', u'f', u'g', u'h', u'i', u'n', u'n', u's', u't']
[u'N', u'a', u'e', u'g', u'i', u'i', u'r']
[u'C', u'a', u'e', u'm', u'n', u'o', u'o', u'r']
These should be Afghanistan, Nigeria and Cameroon respectively but instead they are only sorted within their own array.
Where have I went wrong in my code?
import urllib2
import csv
from bs4 import BeautifulSoup
url = "http://en.wikipedia.org/wiki/List_of_ongoing_armed_conflicts"
soup = BeautifulSoup(urllib2.urlopen(url))
#f= csv.writer(open("test.csv","w"))
#f.writerow(["location"])
def unique(countries):
seen = set()
for country in countries:
l = country.lower()
if l in seen:
continue
seen.add(l)
yield country
for row in soup.select('table.wikitable tr'):
cells = row.find_all('td')
if cells:
for location in cells[3].find_all(text=True):
location = location.split()
for locations in unique(location):
print sorted(locations)
#f.writerow([location])