有序集列表操作.. [英] ordered sets operations on lists..

查看:65
本文介绍了有序集列表操作..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,是否有*直接*方式在列表上执行设置操作

保留输入列表的顺序?

For Ex。 l1 = [1,5,3,2,4,7]

l2 = [3,5,10]

和(l1与l2相交)返回[5,3] ....(和(l2与l1相交)

返回[3,5])


提前感谢,

amit。


-

----

Amit Khemka - onyomo.com

无尽的世界轮回,无尽的太阳旋转,无尽的追求;

我再次转身,回到我自己的开始,在这里,找到休息。

Hello, Is there a *direct* way of doing set operations on lists which
preserve the order of the input lists ?
For Ex. l1 = [1, 5, 3, 2, 4, 7]
l2 = [3, 5, 10]

and (l1 intersect l2) returns [5, 3] .... (and (l2 intersect l1)
returns [3, 5])

thanks in advance,
amit.

--
----
Amit Khemka -- onyomo.com
Endless the world''s turn, endless the sun''s Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.

推荐答案



Amit Khemka写道:

Amit Khemka wrote:
你好,有*直接*在列表上进行设置操作的方法是什么?
保留输入列表的顺序?
Ex。 l1 = [1,5,3,2,4,7]
l2 = [3,5,10]
和(l1与l2相交)返回[5,3] .. ..(和(l2与l1相交)
返回[3,5])
Hello, Is there a *direct* way of doing set operations on lists which
preserve the order of the input lists ?
For Ex. l1 = [1, 5, 3, 2, 4, 7]
l2 = [3, 5, 10]

and (l1 intersect l2) returns [5, 3] .... (and (l2 intersect l1)
returns [3, 5])



你是什么意思直接办法 ?丑陋(有些人说)一个班轮?


过滤器(set(l1).intersection(set(l2)).__ contains _,l1)

filter(set (l1).intersection(set(l2)).__ contains _,l2)


what do you mean by "direct" way ? ugly(some said) one liner ?

filter(set(l1).intersection(set(l2)).__contains__, l1)
filter(set(l1).intersection(set(l2)).__contains__, l2)


Amit Khemka写道:
Amit Khemka wrote:
你好,是有一个*直接*方式在列表上进行设置操作,以保持输入列表的顺序?
Nope

Ex。 l1 = [1,5,3,2,4,7]
l2 = [3,5,10]
和(l1与l2相交)返回[5,3] .. ..(和(l2交叉l1)
返回[3,5])
Hello, Is there a *direct* way of doing set operations on lists which
preserve the order of the input lists ? Nope
For Ex. l1 = [1, 5, 3, 2, 4, 7]
l2 = [3, 5, 10]

and (l1 intersect l2) returns [5, 3] .... (and (l2 intersect l1)
returns [3, 5])




但是:

intersection = set( list1)& set(list2)

[如果元素在交集中则为list1中元素的元素]



[如果元素在交集中则为list2中元素的元素]

给你想要的结果。


- 斯科特大卫丹尼尔斯
sc *********** @ acm.org


[Amit Khemka]
[Amit Khemka]
您好,是否有*直接*方式对列表进行集合操作,以保持输入列表的顺序?
对于Ex。 l1 = [1,5,3,2,4,7]
l2 = [3,5,10]
和(l1与l2相交)返回[5,3] .. ..(和(l2交叉l1)


[bonono]
Hello, Is there a *direct* way of doing set operations on lists which
preserve the order of the input lists ?
For Ex. l1 = [1, 5, 3, 2, 4, 7]
l2 = [3, 5, 10]

and (l1 intersect l2) returns [5, 3] .... (and (l2 intersect l1)
[bonono]
你的意思是什么?直接方式?丑陋(有人说)一个班轮过滤器(set(l1).Intersection(set(l2)).__ contains _,l1)
filter(set(l1).intersection(set(l2)).__ contains _,l2 )
what do you mean by "direct" way ? ugly(some said) one liner ?

filter(set(l1).intersection(set(l2)).__contains__, l1)
filter(set(l1).intersection(set(l2)).__contains__, l2)




交叉步骤是不必要的,所以答案可以简化为

位:



The intersection step is unnecessary, so the answer can be simplified a
bit:

filter(set(l2).__ contains _,l1)
[5,3] filter(set(l1).__ contains _,l2)
filter(set(l2).__contains__, l1) [5, 3] filter(set(l1).__contains__, l2)



[3,5]


[3, 5]


这篇关于有序集列表操作..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆