Python,使用部分匹配测试集合中成员资格的简洁方法

发布于 2024-08-29 14:02:45 字数 239 浏览 5 评论 0原文

测试集合中是否存在以另一个元组开头的元组的Pythonic方法是什么?实际上,我真的在寻找匹配的索引,但我可能可以从测试示例中找出

答案:

c = ((0,1),(2,3))
# (0,) should match first element, (3,)should match no element

我应该添加我的 python 是 2.4 和/或 2.5

谢谢

What is the pythonic way to test if there is a tuple starting with another tuple in collection? actually, I am really after the index of match, but I can probably figure out from test example

for example:

c = ((0,1),(2,3))
# (0,) should match first element, (3,)should match no element

I should add my python is 2.4 and/or 2.5

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨敛清荷 2024-09-05 14:02:45

编辑:
感谢OP对问题的补充解释。
S.Mark 的嵌套列表推导式 非常邪恶;检查一下。

我可能会选择使用辅助功能:

def tup_cmp(mytup, mytups):
    return any(x for x in mytups if mytup == x[:len(mytup)])

>>> c = ((0, 1, 2, 3), (2, 3, 4, 5))
>>> tup_cmp((0,2),c)
False
>>> tup_cmp((0,1),c)
True
>>> tup_cmp((0,1,2,3),c)
True
>>> tup_cmp((0,1,2),c)
True
>>> tup_cmp((2,3,),c)
True
>>> tup_cmp((2,4,),c)
False

原始答案:
使用 列表理解 对您有用吗?:

c = ((0,1),(2,3))

[i for i in c if i[0] == 0]
# result: [(0, 1)]

[i for i in c if i[0] == 3]
# result: []

列表比较是在 2.0 中引入

Edit:
Thanks to the OP for the addition explanation of the problem.
S.Mark's nested list comprehensions are pretty wicked; check 'em out.

I might opt to use an auxiliary function:

def tup_cmp(mytup, mytups):
    return any(x for x in mytups if mytup == x[:len(mytup)])

>>> c = ((0, 1, 2, 3), (2, 3, 4, 5))
>>> tup_cmp((0,2),c)
False
>>> tup_cmp((0,1),c)
True
>>> tup_cmp((0,1,2,3),c)
True
>>> tup_cmp((0,1,2),c)
True
>>> tup_cmp((2,3,),c)
True
>>> tup_cmp((2,4,),c)
False

Original answer:
Does using a list-comprehension work for you?:

c = ((0,1),(2,3))

[i for i in c if i[0] == 0]
# result: [(0, 1)]

[i for i in c if i[0] == 3]
# result: []

List comps were introduced in 2.0.

哀由 2024-09-05 14:02:45
>>> c = ((0,1),(2,3))
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,),x))]
[(0, 1)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,1),x))]
[(0, 1)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,),x))]
[(2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,3),x))]
[(2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((4,),x))]
[]

对于较大的元组

>>> c=((0,1,2,3),(2,3,4,5))
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,1),x))]
[(0, 1, 2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,2),x))]
[]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,),x))]
[(2, 3, 4, 5)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,3,4),x))]
[(2, 3, 4, 5)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((4,),x))]
[]
>>>

编辑:更紧凑的元组将是

>>> [x for x in c if all(len(set(y))==1 for y in zip((0,),x))]
[(0, 1, 2, 3)]
>>> c = ((0,1),(2,3))
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,),x))]
[(0, 1)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,1),x))]
[(0, 1)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,),x))]
[(2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,3),x))]
[(2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((4,),x))]
[]

With larger Tuple

>>> c=((0,1,2,3),(2,3,4,5))
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,1),x))]
[(0, 1, 2, 3)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((0,2),x))]
[]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,),x))]
[(2, 3, 4, 5)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((2,3,4),x))]
[(2, 3, 4, 5)]
>>> [x for x in c if all(1 if len(set(y)) is 1 else 0 for y in zip((4,),x))]
[]
>>>

Edit: more compact one would be

>>> [x for x in c if all(len(set(y))==1 for y in zip((0,),x))]
[(0, 1, 2, 3)]
风吹过旳痕迹 2024-09-05 14:02:45

我自己的解决方案,结合了其他两个答案

f = lambda c, t: [x for x in c if t == x[:len(t)]]

my own solution, combination of two other answers

f = lambda c, t: [x for x in c if t == x[:len(t)]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文