简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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