【问题标题】:Python Type-Conversion ErrorsPython 类型转换错误
【发布时间】:2022-01-21 02:55:14
【问题描述】:

在 Python 中,我正在尝试编写两个函数。首先,我将一个列表作为参数并将其作为单个整数返回。其次,我将一个列表作为参数并将其作为一个集合返回。

当我尝试使用给定的测试函数运行程序时,我遇到了两个我无法解决的错误。 (A) 我收到一个类型错误,指出“strings = [str(integer) for integer in list_to_int]”不可迭代,并且 (B) 我有 an_integer 和 convert_set 的未引用变量。我试图重新排列断言函数并重做变量,但似乎没有任何效果。

def list_to_int(list):
    """
    :param list
    convert list to string
    convert string to int
    :return: set
    """
    strings = [str(integer) for integer in list_to_int]
    a_string = "".join(strings)
    an_integer = int(a_string)
    return an_integer

def list_to_set(list):
    """
    :param list:
    comvert to set
    :return: set
    """
    converted_set = set(list_to_set)
    return converted_set

# TEST FUNCTIONS
assert list_to_int([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_int([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_int([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")

assert list_to_set([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
print("correct")
assert list_to_set([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
print("correct")
assert list_to_set([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
print("correct")

【问题讨论】:

  • 您正在尝试迭代对函数的引用而不是列表。将strings = [str(integer) for integer in list_to_int] 替换为strings = [str(integer) for integer in list].. 另外请考虑重命名list,使用保留关键字作为变量名绝不是一个好主意

标签: python list type-conversion integer set


【解决方案1】:

在这两个函数中,您错误地将函数名称引用为列表连接中的迭代器,而不是 list 参数。

换句话说,在list_to_int,改变

strings = [str(integer) for integer in list_to_int]

strings = [str(integer) for integer in list]

并在list_to_set 中更改

converted_set = set(list_to_set)

converted_set = set(list)

另外,我建议不要将函数参数命名为list。名称list 保留为列表对象的默认构造函数,因此最好不要在该上下文中覆盖它并将参数命名为其他名称(即larr 等)

【讨论】:

    【解决方案2】:

    您正在迭代 list_to_int,这是函数本身...您必须迭代 list 参数。 与第二个功能相同;您正在尝试将实际函数转换为集合而不是函数的参数。

    像这样重写你的代码:

    def list_to_int(list):
        """
        :param list
        convert list to string
        convert string to int
        :return: set
        """
        strings = [str(integer) for integer in list]
        a_string = "".join(strings)
        an_integer = int(a_string)
        return an_integer
    
    def list_to_set(list):
        """
        :param list:
        convert to set
        :return: set
        """
        converted_set = set(list)
        return converted_set
    
    # TEST FUNCTIONS
    assert list_to_int([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
    print("correct")
    assert list_to_int([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
    print("correct")
    assert list_to_int([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
    print("correct")
    
    assert list_to_set([1, 2, 3, 4]) == 1234, 'list_to_int([1, 2, 3, 4]) expected 1234'
    print("correct")
    assert list_to_set([4, 3, 2, 1]) == 4321, 'list_to_int([4, 3, 2, 1]) expected 4321'
    print("correct")
    assert list_to_set([3, 1, 2, 4]) == 3124, 'list_to_int([3, 1, 2, 4]) expected 3124'
    print("correct")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      相关资源
      最近更新 更多