列表被识别为元组

时间:2018-10-10 02:49:04

标签: python

我的代码说明这是一个列表。但是不知何故,系统给了我一个例外:

TypeError: 'tuple' object doesn't support item deletion

我的代码对此进行了解释:

x = (insert a tuple elements here)
x = list(x)

我的代码就是这个(我正在编写一个可以识别数字表达式的代码。this lib.中的更多内容)

def num_expression(*nums):
    nums = list(nums)


    if '*' in nums:
        numsam = nums
        search = __charposition(numsam,'*')
        nums2 = 0
        nums3 = 0
        for i in search:
            num2 = i - 1
            num3 = i + 1
            del numsam[i]
            num4 = numsam[num2]*numsam[num3]
            syn = i - 1
            del numsam[syn]
            numsam[i] = num4
    if '/' in nums:
        numsam = nums
        search = __charposition(numsam,'/')
        nums2 = 0
        nums3 = 0
        for i in search:
            num2 = i - 1
            num3 = i + 1
            del numsam[i]
            num4 = numsam[num2]/numsam[num3]
            syn = i - 1
            del numsam[syn]
            numsam[i] = num4
    excptd_vari = 1
    pos = 0
    num = 0
    syn1 = ''
    syn2 = ''
    syn3 = ''
    syn4 = ''
    size = len(nums)

    for i in nums:

        if expctd_vari == 1 and isinstance(i, int):
            syn1 = i
            expctd_vari = 2
            num1syn = True
        elif expctd_vari == 2 and isinstance(i, str):
            syn2 = i
            expctd_vari = 3

        elif expctd_vari == 3 and isinstance(i, int):
            expctd_vari = 2
            if num1syn == True:
                syn3 = i
                if syn2 == '+':
                    num+= syn1+syn3
                if syn2 == '-':
                    num-= syn1+syn3
                else:
                    raise SyntaxError
                num1syn = False
            else:
                syn4 = i
                if syn2 == '+':
                    num+= syn3+syn4
                if syn2 == '-':
                    num-= syn3+syn4
        else:
            raise SyntaxError

def __charposition(string, char):
    pos = [] 
    for n in range(len(string)):
        if string[n] == char:
            pos.append(n)
    return pos

观察:我也在解决分频器问题。只是在决赛中添加回报。

1 个答案:

答案 0 :(得分:1)

问题可能出在那一行:

def num_expression(*nums):

当我检查提供的参数nums时,我得到((4, 5, 6),),转换为列表后的结果为[(4, 5, 6)]

尝试将行更改为:

def num_expression(nums):