【问题标题】:What is the keyword for a tuple? [closed]元组的关键字是什么? [关闭]
【发布时间】:2021-01-11 17:28:40
【问题描述】:

我正在尝试查找 tuple 的关键字,例如关键字“列表”

这是我的例子:

输入:

list1 = [1, 2, 3]
if type(list1) == list:
    print("this is a list")

输出:

this is a list

我想知道元组而不是列表的关键字是什么。

【问题讨论】:

  • 只是元组。
  • 你试过tuple吗?
  • print(type((1,1)))
  • print(type((1,1)))
  • 注意,这些不是关键字。

标签: python tuples


【解决方案1】:

您可以使用tuple。您可能还想使用isinstance

tuple1 = (1, 2, 3)
if isinstance(tuple1, tuple):
    print("this is a tuple")

【讨论】:

    【解决方案2】:

    Python 中元组的类型就是tuple

    【讨论】:

      【解决方案3】:

      tuple 本身就是一个类型关键字。只需检查一下

      t = (1, 2, 3)
      if type(t) == tuple:
          print("this is a tuple")
      # this is a tuple
      

      【讨论】:

        猜你喜欢
        • 2022-12-05
        • 1970-01-01
        • 2015-05-16
        • 2012-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        相关资源
        最近更新 更多