【问题标题】:for loop appends string, but doesn't concatenatefor 循环追加字符串,但不连接
【发布时间】:2020-01-28 19:34:27
【问题描述】:

我有这个二维列表。我想推导出销量最多的凸轮模型,并且该阵列有 2 个模型与之相关。我定义了一个函数来返回品牌的数量和索引。数量和索引被附加到一个新列表中,但是当我尝试连接原始二维列表中的值时,连接不成功,它只显示第一个模型的名称。有人可以解释这里出了什么问题吗?

UnitSold = [['Dash Cam Model', 'SJ Branch', 'PJ Branch', 'KL Branch'], ['RS Pro with GPS', 5, 4, 3],
            ['Transcend Drive Pro', 2, 2, 3], ['H203 1080P', 3, 2, 5], ['Pioneer', 4, 5, 3]]
def maxItem():
    n = 0
    k: list = []
    for i in range(1, len(UnitSold)):
        m = 0
        for j in range(1, len(UnitSold[i])):
            m += UnitSold[i][j]
        if m >= n:
            n = m
            k.append(n)
            k.append(i)
            return k
    return k

此方法有效,并且当您打印此函数时,列表会附加 4 个值。

而此方法仅打印 2 个值,即总数量和较早加载的型号名称。

def maxItem():
    n = 0
    k: list = []
    str = ""
    for i in range(1, len(UnitSold)):
        m = 0
        for j in range(1, len(UnitSold[i])):
            m += UnitSold[i][j]
        if m >= n:
            n = m
            str += UnitSold[i][0]
            return str, n
    return str, n

【问题讨论】:

  • 我真的很难理解你在这里想要实现的目标,如果我错了,请纠正我。你希望你的函数返回两个绑定的模型吗?如果是这种情况,当你击中第一个项目时,你会提前返回,这会让你失去你的功能。
  • @Axe319 不,我希望 concat 方法起作用。我创建了 list append 方法来测试该方法是否返回 4 个值,并且确实如此。但是当我在 concat 中做同样的事情时它只返回 2 个值。

标签: python-3.x list string-concatenation


【解决方案1】:

我将其发布为答案,因为它更易于解释。

这是您与我的 cmets 的第一个示例,说明正在发生的事情。

UnitSold = [['Dash Cam Model', 'SJ Branch', 'PJ Branch', 'KL Branch'], ['RS Pro with GPS', 5, 4, 3],
            ['Transcend Drive Pro', 2, 2, 3], ['H203 1080P', 3, 2, 5], ['Pioneer', 4, 5, 3]]
def maxItem():
    n = 0
    k: list = []

    # this loop iterates from 1 through 4
    for i in range(1, len(UnitSold)):
        m = 0

        # In the first iteration of the above loop
        # when i == 1 it hits this loop 
        # which iterates from 1 through 3
        # counting the integers in this list
        # ['RS Pro with GPS', 5, 4, 3]
        for j in range(1, len(UnitSold[i])):
            m += UnitSold[i][j]

        # after this loop is completed it checks if
        # m (which is equal to 12) is more
        # or greater than n (which is equal to 0)
        if m >= n:
            # it then assigns 12 to n
            # appends 12 and i (which is equal to 1)
            # to an empty list
            # now your list looks like this [12, 1]
            n = m
            k.append(n)
            k.append(i)

            # it then hits this return which ends your function
            # and hands back the k list with 2 elements
            # as it always will on the first iteration of the
            # outer loop
            return k
    # this return is never hit
    return k

print(maxItem())

这是您的第二个示例,它基本上以稍微不同的方式做同样的事情。唯一的区别是你交回一个元组而不是一个列表,你交回第一个元素的值而不是第一个元素。

UnitSold = [['Dash Cam Model', 'SJ Branch', 'PJ Branch', 'KL Branch'], ['RS Pro with GPS', 5, 4, 3],
            ['Transcend Drive Pro', 2, 2, 3], ['H203 1080P', 3, 2, 5], ['Pioneer', 4, 5, 3]]
def maxItem():
    n = 0
    k: list = []
    str = ''

    # this loop iterates from 1 through 4
    for i in range(1, len(UnitSold)):
        m = 0

        # In the first iteration of the above loop
        # when i == 1 it hits this loop 
        # which iterates from 1 through 3
        # counting the integers in this list
        # ['RS Pro with GPS', 5, 4, 3]
        for j in range(1, len(UnitSold[i])):
            m += UnitSold[i][j]

        # after this loop is completed it checks if
        # m (which is equal to 12) is more
        # or greater than n (which is equal to 0)
        if m >= n:
            # it then assigns 12 to n
            # concatenates 'RS Pro with GPS' 12 onto an empty string
            # now your string looks like this 'RS Pro with GPS'
            n = m
            str += UnitSold[i][0]

            # it then hits this return which ends your function
            # and hands back an unnamed Tuple with 2 elements
            # so essentially it's doing this
            # first_item = (str, n)
            # return first_item
            return str, n
    # this return is never hit
    return str, n

print(maxItem())

附带说明,您不应将变量命名为 str,因为它已用作内置类型(即字符串)。

这也有助于了解您是如何调用此函数的。

【讨论】:

  • 哦,谢谢,我现在明白了。停止循环的是 return 语句的早期定位。
猜你喜欢
  • 2019-02-08
  • 2020-06-29
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
相关资源
最近更新 更多