简体   繁体   中英

How to get needed datetime from tuple list?

i have the variable:

results3 = [
    (
        'CP - 2615', 
        23652, 
        datetime.datetime(2014, 10, 31, 19, 21, 56), 
        'custom-simulation:pre-processing-cleanup', 
        5, 
        datetime.datetime(2014, 10, 31, 19, 21, 59), 
        datetime.datetime(2014, 10, 31, 19, 22, 4), 
        259, 
        262
    ), (
        'CP - 2615', 
        23652, 
        datetime.datetime(2014, 10, 31, 19, 21, 56), 
        'custom-cleanup:pre-processing-cleanup', 
        1, 
        datetime.datetime(2014, 10, 31, 19, 22, 5), 
        datetime.datetime(2014, 10, 31, 19, 22, 6), 
        259, 
        262
    )
]

how i can get needed date time (datetime.datetime(2014, 10, 31, 19, 22, 5)) from list of tuples, when i try:

actualEndTime = []
for i in range(len(results3)):
    actualEndTime.append(results3[i][2][1])

i got the:

TypeError: 'datetime.datetime' object has no attribute '__getitem__'

You went one level too deep with your indexing, the [1] is unneeded. Also, if all you want to do is pull the datetime out into a list, your code will be more readable if you use a list comprehension.

actualEndTime = [x[2] for x in results3]

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