0

pyTony i learned lots from this code you used in resoponce to thread

http://www.daniweb.com/software-development/python/threads/384191

i first mixed up the input and was supprised at the result which only printed last occurence of the key in the dictionary list...

i did work out how to sort it first and reproduce your results.

changed the input to

rawdata='''
man1 car
man2 jacket
man2 house
man1 wallet
man1 shirt
man1 car
man2 truck
man2 jacjet
man2 computer
man1 car
man1 shirt
man2 jacket
'''.splitlines()

then added a steps to sort it and output was exactly as yours ... thanks again great work...

# pair up data
data = [d.split() for d in rawdata if d]
# group common data without sorting 
data = [(key, [b for a,b in g]) for key, g in groupby(data, itemgetter(0))]
# sort data 
data.sort()
print(data)

i am missing some basic language idea and not seeing why the original you made did not take my input and create these results

{'man1': {'car': 1}, 'man2': {'jacket': 1,'house': 1},'man1': {'wallet': 1,'car': 1, 'shirt': 1}, 'man2': {'truck': 1,'jacket': 1,'computer': 1},'man1': {'car': 1, 'shirt': 1}, 'man2': {'jacket': 1}}

what should be added to your code to get this result ... thanks in advance for all the help breaking down this language into valuable tools ..

BTW i think i get how the dictionary idea is a lambda or lisp style code and will hope to explore the idea of creating some AI similar to my lisp days ... o the canoe problem and Lisa program ....

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Cireyoretihw
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.