""" Makes a fake data. Simply used to generate dummy data. """ def MakeFakeData(clean, pace, hosp, food, value, recommend, director, week): return { "columns": [clean, pace, hosp, food, value], "director": director, "week": week } """ Generate fake data (probably you can load in your own stuff here with tweaks. """ my_data = [MakeFakeData(1, 1, 1, 1, 1, 1, "Bao", "Week 1"), MakeFakeData(1, 1, 1, 1, 1, 1, "Bao", "Week 1"), MakeFakeData(1, 1, 1, 1, 1, 1, "Bao", "Week 1"), MakeFakeData(1, 1, 1, 1, 1, 1, "Bao", "Week 1"), MakeFakeData(1, 1, 1, 1, 1, 1, "Bao", "Week 1"), MakeFakeData(2, 1, 1, 1, 1, 1, "Bao", "Week 2"), MakeFakeData(3, 1, 1, 1, 1, 1, "Bao", "Week 3"), MakeFakeData(4, 1, 1, 1, 1, 1, "Bao", "Week 4"), MakeFakeData(5, 4, 4, 8, 1, 1, "Lee", "Week 1"), MakeFakeData(6, 4, 4, 8, 1, 1, "Lee", "Week 2"), MakeFakeData(7, 7, 4, 7, 1, 1, "Lee", "Week 3"), MakeFakeData(8, 8, 4, 6, 1, 1, "Lee", "Week 4"), MakeFakeData(9, 3, 7, 6, 7, 1, "Fang", "Week 1"), MakeFakeData(10, 9, 7, 7, 6, 1, "Fang", "Week 2"), MakeFakeData(11, 3, 7, 8, 4, 1, "Fang", "Week 3"), MakeFakeData(12, 6, 7, 9, 9, 1, "Fang", "Week 4") ] cleaned_data = {} for row in my_data: """First set a key. Here I set it as 'Director:Week''""" key = row["director"] + " : " + row["week"] """Because it's python, we need to initialize values if it does not exist.""" if key not in cleaned_data: cleaned_data[key] = [0, 0, 0, 0, 0] """Enumerate basically takes in a list and outputs a pair which gives you the index.""" for column_number, relevant_column in enumerate(row["columns"]): if relevant_column >= 6: cleaned_data[key][column_number] += 1 if relevant_column <= 4: cleaned_data[key][column_number] -= 1 """Let's print the data out!""" print "Dir:Week Clean Pace Hosp Food Value" for key, data in sorted(cleaned_data.iteritems()): """The '<string>.join(list)' joins it given the delimiter into a single string'""" """Python is picky about int to string, so conver the data list into string before joining""" print key + " " + " ".join(str(x) for x in data)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question