简体   繁体   中英

Python - Reformatting a List of Tuples returned from MySQL

I am pulling a list of tables from MySQL and it returns data in this format:

[('A_one_day_minute',)
('A_one_month_daily',)
('A_one_year_daily',)
('A_one_year_weekly',)
('A_six_month_daily',)
('A_three_day_minute',)
('A_three_month_daily',)
('A_three_year_weekly',)
('A_two_day_minute',)
('A_two_month_daily',)
('A_two_year_daily',)
('A_two_year_weekly',)]

However to use this data in the next portion of my code which is to go through each table and insert data into it, I need it in the format below.

Notice even the last tuple at the end of the code above has a comma that will need to be removed.

Desired Format:

('A_one_day_minute',
'A_one_month_daily',
'A_one_year_daily',
'A_one_year_weekly',
'A_six_month_daily',
'A_three_day_minute',
'A_three_month_daily',
'A_three_year_weekly',
'A_two_day_minute',
'A_two_month_daily',
'A_two_year_daily',
'A_two_year_weekly')

I'm not sure how to go about doing this and any help is appreciated, thank you!

Does this solution good for you?

x = [('A_one_day_minute',),
    ('A_one_month_daily',),
    ('A_one_year_daily',),
    ('A_one_year_weekly',),
    ('A_six_month_daily',),
    ('A_three_day_minute',),
    ('A_three_month_daily',),
    ('A_three_year_weekly',),
    ('A_two_day_minute',),
    ('A_two_month_daily',),
    ('A_two_year_daily',),
    ('A_two_year_weekly',)]

f = []
for i in x:
    for j in i:
        f.append(j)
print(tuple(f))

or

H = tuple(j for j in i for i in x)
print(H)

or

print(tuple(map(lambda v:v[0],x)))

The result:

('A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly', 'A_two_year_weekly')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM