Python Forum
Organizing the output of 2 for loops
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Organizing the output of 2 for loops
#1
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:
[Image: for1.png]
For loop 2 output:
[Image: for_2.png]

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,
[Image: expected.png]

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
Reply
#2
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 :)
Reply
#3
(May-19-2018, 08:55 AM)ThiefOfTime Wrote: 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 :)

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?
Reply
#4
Any help on this?
Reply
#5
This will take care of merging - without sorting
pandas.merge(source1, source2, how='outer', right_index=True, left_index=True)


Talking of your code
  1. io is a name of a standard module (and 2-letter names are not much better than 1 letter)
  2. The error message explains exactly what your problem is. You may look at list.extend
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
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)
Reply
#7
(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 idea
view_values = viewdata(rawvalue)[0]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020