首页 > 解决方案 > for 循环追加字符串,但不连接

问题描述

我有这个二维列表。我想推导出销量最多的凸轮模型,并且该阵列有 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

标签: python-3.xliststring-concatenation

解决方案


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

这是您的第一个示例,其中包含我对正在发生的事情的评论。

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,因为它已经用作内置类型(意思是字符串)。

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


推荐阅读