我有两个清单。一个是普通列表,另一个是二维列表。我需要从列表1中取出4个条目并将它们(最好按照某种顺序添加它们)添加到第二个2d列表中每个条目的前面。
我需要下面的前四个列表
['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', '20', '0:00', '', '1', '0', '0', '0', '0', '0', '1']
添加到第二列中的每个“值”条目......
(0, ['Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1'])
(1, ['Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12'])
(2, ['Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3'])
(3, ['Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])
我已经在列表2中进行迭代,以便在“真实数据”结束后删除导致1-4个元素组的几个选项卡,如下所示:
for element, value in enumerate(agentdata1):
if len(value) != 8:
agentdata1.pop(element)
我的想法仍然是在for循环中但在if语句之外我可以将这些值“推”到列表中。我已经搞乱了list.insert和list.append,但还没有任何快乐。提前谢谢。
所需的输出如下:
(0, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0','Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1'])
(1, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0','Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12'])
(2, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0','Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3'])
(3, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0','Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])
答案 0 :(得分:0)
你可以试试这个:
l1 = ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', '20', '0:00', '', '1', '0', '0', '0', '0', '0', '1']
l2 = [(0, ['Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1']), (1, ['Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12']), (2, ['Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3']), (3, ['Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])]
final_list = [(a, l1[:4]+b) for a, b in l2]
输出:
[(0, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', 'Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1']),
(1, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', 'Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12']),
(2, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', 'Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3']),
(3, ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', 'Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])]
答案 1 :(得分:0)
假设您的第二个清单是
agentdata1 = [
(0, ['Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1']),
(1, ['Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12']),
(2, ['Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3']),
(3, ['Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])
]
你的第一个清单是
list1 = ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0',
'20', '0:00', '', '1', '0', '0', '0', '0', '0', '1']
首先,我们将列表1的前四个元素分开:
four_elements_list1 = list1[:4]
现在,有两种可能的方法:
更直观的是:
result = []
for element, value in agentdata1:
result.append((element, four_elements_list1 + value))
更紧凑的是:
result = map(lambda (element, value): (element, four_elements_list1 + value), agentdata1)
您可以找到有关lambda functions的更多信息,以便更好地了解它。
答案 2 :(得分:0)
如果要就地修改列表,只需使用list-slicing。首先,设置数据:
l2 = [(0, ['Check In', '', '1607-584-4547', 'Avail', '10:48', '3', '1', '1']),
(1, ['Check In', '', '1607-584-4514', 'Avail', '10:49', '2', '1', '12']),
(2, ['Check Out', '', '1607-584-4580', 'Avail', '10:46', '2', '0', '3']),
(3, ['Check Out', '', '1607-115-4068', 'AUX', '10:49', '0', '0', '8'])]
l1 = ['15', '15:09 WED JAN 10 2018', 'Endcrinology', '0', '20', '0:00', '', '1', '0', '0', '0', '0', '0', '1']
现在,一个简单的for循环:
for _, sub in l2:
sub[:0] = l1[:4]