我有这个双重名单:
doubleL = [['123', 'user1', 'its.zip', '05-19-17'],
['123', 'user1', 'zippedtto.zip', '05-24-17'],
['123', 'user1', 'zippedtto.zip', '05-19-17'],
['123', 'user2', 'Iam.zip', '05-19-17'],
['abcd', 'Letsee.zip', '05-22-17']]
我想使用python并检查每个子列表中的前两个元素是否相同,并返回带有最后一个元素的双列表。例如,在这种情况下,我的输出将是:
output = [ ['05-19-17', '05-24-17', '05-19-17' ], ['05-19-17'], ['05-22-17']]
获得此输出的最pythonic方式是什么?
谢谢!
答案 0 :(得分:0)
tmp = {} # A dictionary
for x in doubleL: # Iterate over outer list
# Set default as empty list if key not present
# Else/And just append the last value
tmp.setdefault((x[0], x[1]), []).append(x[-1])
print(list(tmp.values()) # Print values as list
from collections import defaultdict
for x in doubleL: # Iterate over outer list
# Just append the last value
tmp[(x[0], x[1])].append(x[-1])
print(list(tmp.values()) # Print values as list
答案 1 :(得分:0)
您可以使用字典,但如果您想保留订单,则应使用模块集合和OrderedDict,如下所示:
LET date = 1495616898128
FOR invoice IN data_invoices
FILTER invoice.tenantId IN ['xxx', 'yyy']
FILTER invoice.createdOn<=date
LET invoiceData = (invoice.data[* FILTER CURRENT.createdOn<=date LIMIT 1])[0]
FILTER invoiceData.values.amount>1000
LET jobs = (
FOR job, link IN 1 OUTBOUND invoice data_links
FILTER link.createdOn<=date AND (link.replacedOn == NULL
OR link.replacedOn>date)
LET jobData = (job.data[* FILTER CURRENT.createdOn<=date LIMIT 1])[0]
FILTER CONTAINS(jobData.values.name, 'a')
COLLECT WITH COUNT INTO count
RETURN {
count
}
)[0]
FILTER jobs.count>0
SORT invoiceData.values.amount ASC
LIMIT 0,8
RETURN invoice
答案 2 :(得分:0)
非词典路线将使用itertools.groupby
和sorted
以正确的顺序获取所有项目
from itertools import groupby
groups = []
for g, l in groupby(sorted(doubleL, key=lambda o: o[:2]), key=lambda o: o[:2]):
groups.append([i[-1] for i in l])
print groups
>>> [['05-19-17', '05-24-17', '05-19-17'], ['05-19-17'], ['05-22-17']]