gpt4 book ai didi

python - 字符串列表或单个字符串中的命名元组之间的区别?

转载 作者:行者123 更新时间:2023-12-02 03:01:16 26 4
gpt4 key购买 nike

这两个命名元组有什么区别:

Point = collections.namedtuple('Point','x,y')

Point = collections.namedtuple('Point',['x','y'])

对我来说,两者在功能上似乎是相同的(使用 python 3.8.0)

最佳答案

这两个命名元组确实是相同的:-)

>>> import collections
>>> Point1 = collections.namedtuple('Point','x,y')
>>> Point2 = collections.namedtuple('Point',['x','y'])
>>> Point1._fields
('x', 'y')
>>> Point2._fields
('x', 'y')

根据 namedtuple() docs :

The field_names are a sequence of strings such as ['x', 'y']. Alternatively, field_names can be a single string with each fieldname separated by whitespace and/or commas, for example 'x y' or 'x, y'.

关于python - 字符串列表或单个字符串中的命名元组之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59923045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com