简体   繁体   中英

How to distinguish between a tuple and a list in Python's structural pattern matching?

I want to use Python's structural pattern matching to distinguish between a tuple (eg representing a point) and a list of tuples.

The straightforward approach does not work though:

def fn(p):
    match p:
        case (x, y):
            print(f"single point: ({x}, {y})")
        case [*points]:
            print("list of points:")
            for x, y in points:
                print(f"({x}, {y})")

fn((1, 1))
fn([(1, 1), (2, 2)])

which outputs:

single point: (1, 1)
single point: ((1, 1), (2, 2))

whereas I want it to output:

single point: (1, 1)
list of points:
(1, 1)
(2, 2)

Switching the order of the case statements also does not help here.

What is a good way to solve this with pattern matching?

Use tuple(foo) for matching a tuple and list(foo) for matching a list

def fn(p):
    match p:
        case tuple(contents):
            print(f"tuple: {contents}")
        case list(contents):
            print(f"list: {contents}")

fn((1, 1))  # tuple: (1, 1)
fn([(1, 1), (2, 2)])  # list: [(1, 1), (2, 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