简体   繁体   中英

Python list comprehension is flattening out the lists

I am pulling data from SQL server using pyodbc and want to convert the datetime.datetime column values to epoch before inserting to another database(table). When I am doing that, I see that my list of lists is getting flattened out. How to avoid this please.

   >>> scur.execute("select top 2 * from database.dbo.table")
<pyodbc.Cursor object at 0x1f2d930>
>>> cnt = scur.fetchall()
>>> rowlist = [list(l) for l in cnt]

>>> rowlist

[[404458, 348, datetime.datetime(2015, 10, 9, 14, 13), datetime.datetime(2015, 10, 9, 0, 0), u'ded1598f-eed1-4edb-bfe4-f866592e9a51', u'el ong', u'', 1, 0, 0.091499999999999998, 0.0, -999999.0, -999999.0, 0.0, 0.25, -999999.0, -999999.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, None, None, 1127, None, u'150610007', u'10', u'CMMQ0056', None, u'0', None, None, None, None, None, False, None, None], [404459, 349, datetime.datetime(2015, 10, 9, 14, 13), datetime.datetime(2015, 10, 9, 0, 0), u'174b2d32-e71d-40b0-9c1d-cab7274c9b40', u'el ong', u'', 1, 0, 0.055399999999999998, 0.0, -999999.0, -999999.0, 0.0, 0.40000000000000002, -999999.0, -999999.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, None, None, 1127, None, u'150610007', u'10', u'CMMQ0056', None, u'0', None, None, None, None, None, False, None, None]]

>>> [row[i].strftime('%s') if isinstance(row[i], datetime.date) else row[i] for row in rowlist for i in range(len(row))]

[404458, 348, '1444414380', '1444363200', u'ded1598f-eed1-4edb-bfe4-f866592e9a51', u'el ong', u'', 1, 0, 0.091499999999999998, 0.0, -999999.0, -999999.0, 0.0, 0.25, -999999.0, -999999.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, None, None, 1127, None, u'150610007', u'10', u'CMMQ0056', None, u'0', None, None, None, None, None, False, None, None, 404459, 349, '1444414380', '1444363200', u'174b2d32-e71d-40b0-9c1d-cab7274c9b40', u'el ong', u'', 1, 0, 0.055399999999999998, 0.0, -999999.0, -999999.0, 0.0, 0.40000000000000002, -999999.0, -999999.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, None, None, 1127, None, u'150610007', u'10', u'CMMQ0056', None, u'0', None, None, None, None, None, False, None, None]

Suppose you have a list of lists (a simpler example of your):

>>> LoL=[[datetime.datetime(2015, 10, 9, 14, 13),'a','b', 1,2], [datetime.datetime(2016, 11, 9, 14, 13),'c','d', 3,4]]

You can then have a nested list comprehension to preserve the nested lists:

>>> [[e.strftime('%s') if isinstance(e, datetime.date) else e for e in rowlist] for rowlist in LoL]
[['1444425180', 'a', 'b', 1, 2], ['1478729580', 'c', 'd', 3, 4]]

This should do it. This will retain the list structure you want while still performing the conversions:

[[v.strftime('%s') if isinstance(v, datetime.date) else v for v in row] for row in rowlist]

您需要使用内部列表comp进行迭代以便保留行,您还可以简单地遍历每行中的元素而不需要范围。

[int(ele.strftime("%s"))  if isinstance(ele, datetime.datetime) else ele for ele in sub] for sub in l]

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