有没有人可以把下面的函数转换成一个列表压缩!
def something(x,y):
result = []
for i in x:
for j in y:
if i['username'] == j['username']:
result.append(j)
if i['username'] != result[len(result)-1]['username']:
result.append(i)
return result
这是我能想到的最好的,但它是不正确的。
result = [user for user in users for contact in contacts if contact['username'] == user['username']]
谢谢你的帮助。
https://stackoverflow.com/questions/51548633
复制相似问题