Python - 比较嵌套列表并将匹配项附加到新列表?
我希望与不等长度的嵌套列表进行比较。我只对每个子列表的第一个元素之间的匹配感兴趣。如果存在匹配项,我希望将匹配项添加到另一个列表中,以便随后转换为制表符分隔文件。这是我正在处理的示例:
x = [['1', 'a', 'b'], ['2', 'c', 'd']]
y = [['1', 'z', 'x'], ['4', 'z', 'x']]
match = []
def find_match():
for i in x:
for j in y:
if i[0] == j[0]:
match.append(j)
return match
这返回:
[['1', 'x'], ['1', 'y'], ['1', 'x'], ['1', 'y'], ['1', 'z', 'x']]
重新处理列表以删除重复项是一个好的做法还是可以以更简单的方式完成?
另外,出于比较的目的,使用元组和/或元组的元组是否更好?
非常感谢任何帮助。
问候, 海鲜。
I wish to compare to nested lists of unequal length. I am interested only in a match between the first element of each sub list. Should a match exist, I wish to add the match to another list for subsequent transformation into a tab delimited file. Here is an example of what I am working with:
x = [['1', 'a', 'b'], ['2', 'c', 'd']]
y = [['1', 'z', 'x'], ['4', 'z', 'x']]
match = []
def find_match():
for i in x:
for j in y:
if i[0] == j[0]:
match.append(j)
return match
This returns:
[['1', 'x'], ['1', 'y'], ['1', 'x'], ['1', 'y'], ['1', 'z', 'x']]
Would it be good practise to reprocess the list to remove duplicates or can this be done in a simpler fashion?
Also, is it better to use tuples and/or tuples of tuples for the purposes of comparison?
Any help is greatly appreciated.
Regards,
Seafoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用集合来获取没有重复项的集合。
您发布的代码似乎没有生成您发布的输出。我不知道你应该如何从该输入生成该输出。例如,输出具有
'y'
而输入则没有。我认为你的功能设计还有待改进。目前,您将
x
、y
和match
定义为模块级别,并显式读取和更改它们。这不是你想要的函数设计方式——一般来说,函数不应该在全局层面上改变某些东西。它应该显式地传递它需要的所有内容并返回结果,而不是隐式地接收信息并更改其自身之外的某些内容。我会改变
至
为了将最后的更改提升到下一个级别,我通常会替换模式
<前><代码>def f(...):
返回值 = []
为了...
return_value.append(foo)
返回return_value
使用类似的生成器
<前><代码>def f(...):
为了...
产量富
这将实现上述功能
表达此生成器效果的另一种方法是使用生成器表达式
(j for i in x for j in y if i[0] == j[0])
。Use sets to obtain collections with no duplicates.
The code you posted doesn't seem to generate the output you posted. I do not have any idea how you are supposed to generate that output from that input. For example, the output has
'y'
and the input does not.I think the design of your function could be much improved. Currently you define
x
,y
, andmatch
as the module level and read and mutate them explicitly. This is not how you want to design functions—as a general rule, a function shouldn't mutate something at the global level. It should be explicitly passed everything it needs and return a result, not implicitly receive information and change something outside itself.I would change
to
To take that last change to the next level, I usually replace the pattern
with the similar generator
which would make the above function
another way to express this generator's effect is with the generator expression
(j for i in x for j in y if i[0] == j[0])
.我不知道我是否正确解释了您的问题,但鉴于您的示例,您似乎可能使用了错误的索引:
更改
为
I don't know if I interpret your question correctly, but given your example it seems that you might be using a wrong index:
change
into
通过使用集合,您可以更简单地做到这一点。
You can do this a lot more simply by using sets.
检查数组的第二个元素是否相同。您需要
if i[0] == j[0]
。否则,我发现您的代码非常可读,并且不一定会更改它。
checks whether the second elements of the arrays are identical. You want
if i[0] == j[0]
.Otherwise, I find your code quite readable and wouldn't necessarily change it.
更简单的表达式也应该在这里工作:
A simplier expression should work here too: