【问题标题】:AttributeError: 'int' object has no attribute 'isdigit' with arraysAttributeError: \'int\' 对象没有数组的属性 \'isdigit\'
【发布时间】:2023-01-08 22:09:29
【问题描述】:

这是一个非常简单的脚本,我在使用时遇到了一些问题 基本上我希望脚本打印数组中除整数以外的内容。

array = ["hello", "hey", 1, "sup", 8]
for x in array:
  if x.isdigit():
    continue
  else:
    print(x)

我想也许使用 isnumeric.() 会解决它,但似乎也不起作用

因为我很新,所以我很难搞清楚

【问题讨论】:

    标签: python


    【解决方案1】:

    您想要检查值的类型。 1"1"不同; isdigit 是由str 类型而不是int 类型定义的方法。

    for x in array:
        if isinstance(x, str):
            print(x)
    

    【讨论】:

      【解决方案2】:
      array = ["hello", "hey", 1, "sup", 8]
      for x in array:
        if isinstance(x, int):
          continue
      else:
          print(x)
      

      这将打印数组中所有非整数的元素。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 2021-11-30
        • 2020-03-26
        • 2013-04-15
        相关资源
        最近更新 更多