#python #python-3.x #list #tuples
Вопрос:
Я новичок в Python ..
Я могу сгенерировать следующий вывод, сравнив два словаря с dictdiffer
, и он находится в oo
переменной:
oo = ('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
Я хотел бы получить доступ к спискам внутри этого кортежа с выводом типа:
['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']
['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']
['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']
......
......
То, что я делал до сих пор, — это просто looping by index value
:
for tuple in enumerate(oo):
for list in tuple[1]:
for activity in list:
print(activity[0])
Однако вывод состоит из случайных букв, и не каждый список (начиная с даты) существует!:
r
e
m
o
v
e
w
['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']
(
['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']
Y
['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']
Как я могу упорядочить списки и получить их из этой переменной с несколькими кортежами?
Ответ №1:
Структура данных, по-видимому, состоит из кортежей кортежей и списков.
Нижеприведенное, по-видимому, работает для получения желаемых результатов.
oo = ('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
('remove', '', [('we are here to help you improve your skills', [['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']]), ('(1st meeting) here is our recorded presentation skills webinar', [['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']]), ('YOU ARE INVITED TO THIS PROGRAMMING EVENT', [['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered'], ['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']])])
for x in oo[2]:
for y in x[1]:
print(y)
ВОЗВРАТ
['2021-04-09', 'email@example.com', 'we are here to help you improve your skills', 'Delivered']
['2021-04-12', 'email@example.com', '(1st meeting) here is our recorded presentation skills webinar', 'Delivered']
['2021-04-13', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']
['2021-04-16', 'email@example.com', 'YOU ARE INVITED TO THIS PROGRAMMING EVENT', 'Delivered']