将列表列表中的多个值与另一个列表列表和 return 值匹配的 Pythonic 方法
Pythonic way to match multiple values in a list of lists to another list of lists and return a value
我正在尝试将一个列表列表中的两个或多个值与另一个列表列表进行匹配,并且 return 一个列表中的一个值。很像 SQL 的 on 子句 - on x.field = y.field 和 x.field = y.field.
想象一下您的亚马逊账户的交易清单。 ID 是唯一的,但名称会更改(该死的亚马逊!)。我想根据最大日期使用最后一个 name/title。我可能可以用初始数据集做下面的事情,但想不出怎么做。我正在以列表列表的形式阅读行。
我正在从事一个梳理亚马逊购买的个人项目,但我认为这在未来会非常有用。我有一个解决方案,但我认为它会 运行 很长,具体取决于数据的大小。我看到人们呼唤 Pandas' dataframe 作为解决方案,但我试图先学习 Python 的标准库。这是我关于 Stack 的第一个问题,在此先向您表示歉意和感谢。
#Example data set comes from a csv I've read into different list of lists
#Fields in order are ID, date (max date from csv to id) -- data set is unique row count 140
X = [['b12', 8/1/2019], ['c34', 7/25/2018],..]
#Fields in order are ID, date, Name -- data set is unique, due to date, row count 1,231
Y = [['b12', 6/23/19, 'item 1'], ['b12', 7/21/19, 'item 1.0'], ['b12', 8/1/19, 'item 1.1'],..]
#Code that works, but I'm sure is 'expensive'
for i in X:
for n in Y:
if i[0] == n[0] and i[1] == n[1]:
i.append(x[2])
else: continue
#Result is either I append to X (like I have) or create a new list of lists all together
X
[['b12', 8/1/2019, 'item 1.1'], ['c34', 7/25/2019, 'item 2.8'],...]
您可以从列表 Y
创建映射字典,其中 (id, date)
作为键,name
作为值。然后使用列表理解从列表 X
中创建一个新列表,并使用映射字典
中的映射值
>>> X = [['b12', '8/1/2019'], ['c34', '7/25/2018']]
>>> Y = [['b12', '6/23/19', 'item 1'], ['b12', '7/21/19', 'item 1.0'], ['b12', '8/1/19', 'item 1.1'], ['c34', '7/25/18', 'item2.1']]
>>>
>>> mapping = {(id, date):name for id,date,name in Y}
>>> res = [[id, date, mapping[(id, date.replace('/20', '/'))]] for id,date in X]
>>>
>>> print (res)
[['b12', '8/1/2019', 'item 1.1'], ['c34', '7/25/2018', 'item2.1']]
我正在尝试将一个列表列表中的两个或多个值与另一个列表列表进行匹配,并且 return 一个列表中的一个值。很像 SQL 的 on 子句 - on x.field = y.field 和 x.field = y.field.
想象一下您的亚马逊账户的交易清单。 ID 是唯一的,但名称会更改(该死的亚马逊!)。我想根据最大日期使用最后一个 name/title。我可能可以用初始数据集做下面的事情,但想不出怎么做。我正在以列表列表的形式阅读行。
我正在从事一个梳理亚马逊购买的个人项目,但我认为这在未来会非常有用。我有一个解决方案,但我认为它会 运行 很长,具体取决于数据的大小。我看到人们呼唤 Pandas' dataframe 作为解决方案,但我试图先学习 Python 的标准库。这是我关于 Stack 的第一个问题,在此先向您表示歉意和感谢。
#Example data set comes from a csv I've read into different list of lists
#Fields in order are ID, date (max date from csv to id) -- data set is unique row count 140
X = [['b12', 8/1/2019], ['c34', 7/25/2018],..]
#Fields in order are ID, date, Name -- data set is unique, due to date, row count 1,231
Y = [['b12', 6/23/19, 'item 1'], ['b12', 7/21/19, 'item 1.0'], ['b12', 8/1/19, 'item 1.1'],..]
#Code that works, but I'm sure is 'expensive'
for i in X:
for n in Y:
if i[0] == n[0] and i[1] == n[1]:
i.append(x[2])
else: continue
#Result is either I append to X (like I have) or create a new list of lists all together
X
[['b12', 8/1/2019, 'item 1.1'], ['c34', 7/25/2019, 'item 2.8'],...]
您可以从列表 Y
创建映射字典,其中 (id, date)
作为键,name
作为值。然后使用列表理解从列表 X
中创建一个新列表,并使用映射字典
>>> X = [['b12', '8/1/2019'], ['c34', '7/25/2018']]
>>> Y = [['b12', '6/23/19', 'item 1'], ['b12', '7/21/19', 'item 1.0'], ['b12', '8/1/19', 'item 1.1'], ['c34', '7/25/18', 'item2.1']]
>>>
>>> mapping = {(id, date):name for id,date,name in Y}
>>> res = [[id, date, mapping[(id, date.replace('/20', '/'))]] for id,date in X]
>>>
>>> print (res)
[['b12', '8/1/2019', 'item 1.1'], ['c34', '7/25/2018', 'item2.1']]