It looks like what you really need is dictionaries, rather than arrays. If you use a dictionary, this problem becomes a whole lot easier. Converting to dicts couldn't be easier.
It looks like what you really need is dictionaries, rather than arrays. If you use a dictionary, this problem becomes a whole lot easier. Converting to dicts couldn't be easier: dictOne = dict(arrayOne) dictTwo = dict(arrayTwo) From there, you can put them together like this: combined = dict() for name in set(dictOne.keys() + dictTwo.keys()): combinedname = dictOne.
Get(name, 0), dictTwo. Get(name, 0) What this does is create a new dictionary called combined, which we'll put the final data in. Then, we make a set of keys from both original dictionaries.
Using a set ensures we don't do anything twice. Finally, we loop through this set of keys and add each pair of values to the combined dictionary, telling calls to the . Get method to supply 0 if no value is present.
If you need to switch the combined dictionary back to an array, that's pretty easy too: arrayResult = for name in combined: arrayResult. Append( name + combinedname) Supposing you want to add another column to your result dictionary, all you have to do is change the middle code to look like this: combined = dict() for name in set(dictOne.keys() + dictTwo.keys() + dictThree.keys()): combinedname = dictOne. Get(name, 0), dictTwo.
Get(name, 0), dictThree. Get(name, 0) If you wanted to encapsulate all this logic in a function (which is something I would recommend), you could do it like this: def combine(*args): # Create a list of dictionaries from the arrays we passed in, since we are # going to use dictionaries to solve the problem. Dicts = dict(a) for a in args # Create a list of names by looping through all dictionaries, and through all # the names in each dictionary, adding to a master list of names names = for d in dicts: for name in d.keys(): names.
Append(name) # Remove duplicates in our list of names by making it a set names = set(names) # Create a result dict to store results in result = dict() # Loop through all the names, and add a row for each name, pulling data from # each dict we created in the beginning for name in names: resultname = d. Get(name, 0) for d in dicts # Return, secure in the knowledge of a job well done. :-) return result # Use the function: resultDict = combine(arrayOne, arrayTwo, arrayThree).
You can add pretty much anything you want. – Benson Feb 21 '11 at 4:51 I just reread the question, and I think I understand what you mean. I'll edit in an example.
– Benson Feb 21 '11 at 4:53 I mean the result of this would be a 2D array with each inner element made up of 3 element(thekey, match from arrayone, match from arraytwo). If I wanted to add another array to this result, would this work. By the way thanks for the thorough explanation.
– Julio Diaz Feb 21 '11 at 4:58 Yeah, I just added an example of how to do that at the end. Apparently I hosed up the formatting, though, I'll fix that. – Benson Feb 21 '11 at 4:59.
Dict1 = dict(arrayOne) >>> dict2 = dict(arrayTwo) >>> keyset = set(dict1.keys() + dict2.keys()) >>> key, dict1. Get(key, 0), dict2. Get(key, 0) for key in keyset 'james', 35, 36, 'robert', 12, 0, 'charles', 0, 45, 'michael', 28, 17, 'trevor', 0, 24, 'jack', 18, 0, 'steven', 23, 4 This gets a bit more complicated if you want to add multiple columns; a dictionary is then best.
But having 0s in the right places becomes a challenge, because when we add a name to the "master dictionary", we have to make sure it starts with a list of 0s of the right length. I'm tempted to create a new class for this, but first, here's a basic function-based solution: def add_column(masterdict, arr): mdlen = len(masterdictmasterdict.keys()0) newdict = dict(arr) keyset = set(masterdict.keys() + newdict.keys()) for key in keyset: if key not in masterdict: masterdictkey = 0 * mdlen masterdictkey. Append(newdict.
Get(key, 0)) arrayOne = "james", 35, "michael", 28, "steven", 23, "jack", 18, "robert", 12 arrayTwo = "charles", 45, "james", 36, "trevor", 24, "michael", 17, "steven", 4 arrayThree = "olliver", 11, "james", 39, "john", 22, "michael", 13, "steven", 6 masterdict = dict((i0, i1) for I in arrayOne) add_column(masterdict, arrayTwo) print masterdict add_column(masterdict, arrayThree) print masterdict Output: {'james': 35, 36, 'robert': 12, 0, 'charles': 0, 45, 'michael': 28, 17, 'trevor': 0, 24, 'jack': 18, 0, 'steven': 23, 4} {'james': 35, 36, 39, 'robert': 12, 0, 0, 'charles': 0, 45, 0, 'michael': 28, 17, 13, 'trevor': 0, 24, 0, 'olliver': 0, 0, 11, 'jack': 18, 0, 0, 'steven': 23, 4, 6, 'john': 0, 0, 22}.
2 Just to point out, you can do dict1. Get(key, 0), etc to provide a default value of 0 instead of None, as the OP requested. – Joe Kington Feb 21 '11 at 4:41 @Joe, yeah I was doing that just as you posted your comment.
– senderle Feb 21 '11 at 4:43 Thanks guys, I am not very knowledgeable of pythons dictionary properties, but I was wondering if this would work, if I try to add a third array to the result array – Julio Diaz Feb 21 '11 at 4:54 @Julio: There are lots of ways to do that, but the one you pick will depend on what you need. Do all the lists need to be of the same size? I.e.
Are 0s required for previously non-existent names? If so, a custom class might be the best way to go. Let me know and I'll post more code.
– senderle Feb 21 '11 at 5:09 I posted something that does what I think you're looking for. If you need me to explain anything, let me know. – senderle Feb 21 '11 at 5:49.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.