0

I have 3 lists named

schema_list, table_list, column_list

The schema, table, and column have a 1:1 match meaning schema_list[0][0] corresponds to table_list[0][0] corresponds to column_list[0][0]. For example, it might look like:

x = schema_list[0][0]+'.'+table_list[0][0]+'.'+column_list[0][0]
print x

Output:

'account_schema.account_table.account_column'

How would I iterate through all the lists so I can produce an output like the one above but for the entire list.

I tried something like this:

for schemas in schema_list:
    for tables in table_list:
        for columns in column_list:
            x = """
                SELECT
                    count({2})
                FROM {0}.{1}
                """.format(schemas,tables,columns)
        print x

The output ran through the entirety of the list but I had several duplicates so I must be missing something.

  1. I also tried:

    for a,b,c in zip(schema_list, table_list, column_list): y = """ SELECT count({2}) FROM {0}.{1} """.format(a,b,c) print y

and got an output:

SELECT
                count(0              account_id
         employer_id
         search_date

            FROM 0    schema1
         schema1
         schema1

         table1
         table1
         table

It looks like the for loop is dumping the entire list into the first iteration?

2.If I do something like this:

for a,b,c in zip(schema_list[0], table_list[0], column_list[0]):
    y = """
             SELECT
                count({2})
            FROM {0}.{1}
            """.format(a,b,c)
    print y

The output prints a single element but does not iterate over all the tables in table_list. It does iterate through all the columns in the column_list though.

This isn't a duplicate post as this is a 2-D array. Here's the solution:

sql_queries=[]
    for x in xrange(len(schema_list)):
        for a,b,c in zip(schema_list[x], table_list[x], column_list[x]):
             y = """
                SELECT
                    count({2})
                FROM {0}.{1}
                """.format(a,b,c)
             sql_queries.append(y)
nlr25
  • 1,605
  • 5
  • 17
  • 27

0 Answers0