【问题标题】:AttributeError: "tuple" object has no attribute "split"AttributeError:“元组”对象没有属性“拆分”
【发布时间】:2021-07-26 02:58:24
【问题描述】:

我是 python 新手,我对初学者问题有点困难

Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers

这是我写的代码

sea =  9 , 8 , 8 , 8

list = sea.split(",")
tuple = tuple(list)

print ("List: " ,list)
print ("Tuple: " ,tuple)

但它给了我一个属性错误

AttributeError: 'tuple' object has no attribute 'split'

就是这样,呵呵,感谢您花时间阅读本文,请帮我纠正:0

【问题讨论】:

  • 请注意,sea 已经是 tuple

标签: python list object tuples attributeerror


【解决方案1】:

现在,sea 变量包含一个 (9, 8, 8, 8) 元组。

要接受用户的输入,请使用input() 函数。 input() 函数返回一个字符串。所以你可以使用str.split(',')来拆分它。

另外,不要使用listtuple 作为变量名,你会隐藏内置函数。

完整代码:

input_from_user = input()

l = [int(n) for n in input_from_user.split(",")]
t = tuple(l)

print("List: ", l)
print("Tuple: ", t)

输出(例如):

1,2,3,4
List:  [1, 2, 3, 4]
Tuple:  (1, 2, 3, 4)

【讨论】:

  • 现在我知道答案了 :)
【解决方案2】:

您应该用" 包装sea 变量的内容。
你的错误是因为 9 , 8 , 8 , 8 默认不是字符串。正如错误所说,它是元组。通过像 "9 , 8 , 8 , 8" 这样包装它,它会变成字符串,你的代码就可以工作了。

【讨论】:

    【解决方案3】:

    Python 将用逗号分隔的对象解释为一个元组。如果你要打印sea 的类型,你会得到tuple

    .split() 函数用于str objects。它不适用于元组。

    另外,根据您收到的提示,您应该使用sea =input() 而不是定义数字。这将允许运行程序的人选择数字。

    此外,尽量不要使用变量名称,如 listtuple 或可能覆盖内置函数的名称。这可能会导致代码中出现一些不需要的错误。

    【讨论】:

      【解决方案4】:

      重写你的代码如下。

      sea =1,2,3,4
      
      list1 =list(sea)
      tuple1 =sea
      
      print ("List: " ,list1)
      print ("Tuple: " ,tuple1)
      

      不要使用关键字作为变量名,它可能会混淆解释器 它默认将值存储在元组中

      【讨论】:

        【解决方案5】:

        让我给你解释一下, 您接受输入的方式

        海 = 9 , 8 , 8 , 8

        sea变量中的值变成tuple

        稍后当你应用这个函数时,

        list = sea.split(",")

        会报错,由于tuple没有split功能,string有这个内置功能 p>

        如果你想要用户输入,你可以通过这种方式接受输入,这将接受一个字符串:

        sea = input()
        

        或者如果你想要硬编码值,你可以在它们周围使用双引号或单引号来使它们成为字符串:

        sea =  "9 , 8 , 8 , 8"
        

        所以你的代码应该是这样的:

        sea =  input()
        
        l_list = sea.split(",")
        t_tuple = tuple(l_list)
        
        print ("List: " ,l_list)
        print ("Tuple: " ,t_tuple)
        

        也不要使用保留字内置函数名作为变量,否则它们会覆盖内置函数和关键字。

        【讨论】:

          【解决方案6】:

          你必须接受一个字符串输入。

          sea = input('Enter: ')
          list = sea.split(",")
          tuple = tuple(list)
          
          print ("List: " ,list)
          print ("Tuple: " ,tuple)
          

          这行不通

          sea =  9 , 8 , 8 , 8
          

          【讨论】:

            【解决方案7】:

            当您输入sea = 9,8,8,8 时,它变为sea = (9,8,8,8)。这是一个元组。

            试试这个代码

            SEA = "9,8,8,8"
            LIST = [int(x) for x in SEA.split(",")]
            TUPLE = tuple(LIST) #Directly Convert List as a Tuple
            print("List",LIST)
            print("Tuple",TUPLE)
            

            注意:不要使用保留字作为变量

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-01-12
              • 2013-06-21
              • 2021-07-30
              • 2013-07-29
              • 2020-08-29
              • 2015-04-22
              相关资源
              最近更新 更多