Python: Finding the Number of Items in Combinations of Categories
Using Python, reading data from an xml file, I am trying to find how many items exist in different combinations of "usage" cagegories.
30
20
20
What I'd like Python to do is go through the XML, extract all the usage names and generate all the different combinations of possibilities, but it's not the XML I am having trouble with, it's the Python.
Some usage categories, may not be in the same order. They might be [office, school, town] instead of [town, office school]. They might contain other words, too, like military, police, village, etc. I can get Python to build me a list of all the possible usage names, but I can't figure out how to build a list of all the combinations of usage names.
Each would be and example of a list or a list within a list:
[Hunting, Coast]
[Coast, Hunting, Village]
[Industrial, Farm]
[Village, Farm, Town, Hunting]
This gets the usage categories, but I don't know how to put them in a unique list and ensure they aren't duplicated.
import xml.etree.ElementTree as ET
types = ET.parse('types.xml')
rootTypes = types.getroot()
useList = []
for itemTypes in types.findall('./type'):
for usage in itemTypes.findall("usage"):
use = (usage.get("name"))
if use in useList:
continue
else:
useList.append(usage.get("name"))
0 comments:
Post a Comment
Thanks