![]() |
Organizing the output of 2 for loops - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Organizing the output of 2 for loops (/thread-10398.html) |
Organizing the output of 2 for loops - pythoneer - May-19-2018 I have 2 for loops that fetch data from 2 different data sources, one for loop prints an output of 15 rows and 6 columns and another for loop prints an output of 18 rows and 6 columns like below , sample output For loop 1 output: ![]() For loop 2 output: ![]() i want them to be combined and organised in such a way that they are sorted based on column 6 with the column1 of for loop 1's output is fixed as follows, ![]() How can i organize the output from these 2 for loops to create this output. i am not sure where to start, however i tried creating individual lists by appending the output that i get from the for loop and it throws an error that it can accept only one argument as below io = [] for obj in hearts: rawvalue = obj.raw - readvalue if rawvalue < 0: rawvalue = rawvalue + 440 else: rawvalue obj = str(obj) obj = ((obj.strip('<').split(" ")[0])) io.append(obj, viewdata(rawvalue)[0][3],viewdata(rawvalue)[0][0], viewdata(rawvalue)[0][1], viewdata(rawvalue)[0][2],rawvalue) Quote:TypeError: append() takes exactly one argument (6 given) What should be done to make this work RE: Organizing the output of 2 for loops - ThiefOfTime - May-19-2018 The first question that pops into my mind is: Why isn't that a database? :D But nonetheless, since your last column is somehow a key for your output (both tables share it and it is individual for each table) read in the first file and create a dictionary where the 6th column is the key and the other elements are stored in a list which is the element for the key of the dictionary, so that when you read the second file you can search for the key and extend the list by the given new elements. the only thing left to do is to prettify the output :) RE: Organizing the output of 2 for loops - pythoneer - May-19-2018 (May-19-2018, 08:55 AM)ThiefOfTime Wrote: The first question that pops into my mind is: Why isn't that a database? :D Any code to start with? and also can i create any tempDB to port all these into and do some sorting there and bring it back ? Any code to do that? RE: Organizing the output of 2 for loops - pythoneer - May-20-2018 Any help on this? RE: Organizing the output of 2 for loops - volcano63 - May-20-2018 This will take care of merging - without sorting pandas.merge(source1, source2, how='outer', right_index=True, left_index=True) Talking of your code
RE: Organizing the output of 2 for loops - killerrex - May-20-2018 To create the DB, take a look to the module sqlite3, you can create in-memory DB with: import sqlite3 db = sqlite3.connect(":memory:")but designing the DB is not an easy task... About the error in your code, you are providing 6 arguments to the append function, that only accepts one. The correct line is: record = (obj, viewdata(rawvalue)[0][3],viewdata(rawvalue)[0][0], viewdata(rawvalue)[0][1], viewdata(rawvalue)[0][2],rawvalue) io.append(record) RE: Organizing the output of 2 for loops - volcano63 - May-20-2018 (May-20-2018, 10:15 AM)killerrex Wrote:record = (obj, viewdata(rawvalue)[0][3],viewdata(rawvalue)[0][0], viewdata(rawvalue)[0][1], viewdata(rawvalue)[0][2],rawvalue) io.append(record) Since viewdata(rawvalue)[0] is referenced 4 times, placing it into a variable seems like a good ideaview_values = viewdata(rawvalue)[0] |