简体   繁体   English

如何匹配作为复制结果的确切列表

[英]How to match exact list which is the result of a replication

if I have a list which is the result of a replication, how do I match it exactly?如果我有一个复制结果的列表,我该如何准确匹配它?

x = [1, 2] * 10000
match x:
  case list([1, 2] * 10000):
    print(1)

gives,给,

    case list([1, 2] * 10000):
                     ^
SyntaxError: invalid syntax

The switch cases in python right now doesn't allow simple operations on case conditions.现在 python 中的 switch case 不允许对 case 条件进行简单的操作。

you can use another class where you define your cases as an alternative :您可以使用另一个类来定义您的案例作为替代:

(for more info check the documentation here ) (有关更多信息,请查看此处的文档)

class Cases:
    A = [1, 2] * 10000
    B = [1, 2] * 100000


x = [1, 2] * 100000

match x:
    case Cases.A:
        print(1)
    case Cases.B:
        print(2)

output:输出:

2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM