开发者

Forcing tuples within tuples?

开发者 https://www.devze.com 2022-12-15 20:02 出处:网络
I\'ve got a python function that should loop through a tuple of coordinates and print their contents:

I've got a python function that should loop through a tuple of coordinates and print their contents:

def do(coordList):
    for element in coordList:
        print element
y=((5,5),(4,4))
x=((5,5))

When y is run through the function, it outputs (5,5) and (4,4), the desired result. However, running x through the function outputs 5 and 5.

Is there a way to force x to be defined as a tuple within a tuple, and if开发者_StackOverflow中文版 not, what is the easiest way to resolve this problem?


Use a trailing comma for singleton tuples.

x = ((5, 5),)


x=((5,5),)

(x) is an expression (x,) is a singleton tuple.


This is an old and infuriating quirk of python syntax. You have to include a trailing comma to make Python see a tuple:

x = ((5,5),)


You need to add a comma after your first tuple. ((5,5),) should work.


Simply add a comma:

x=((5,5),)
0

精彩评论

暂无评论...
验证码 换一张
取 消