for循环追加字符串,但不连接

时间:2020-01-28 19:34:27

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

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

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

1 个答案:

答案 0 :(得分:0)

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

这是您的第一个示例,上面有我对正在发生的事情的评论。

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,因为该变量已被用作内置类型(含义为字符串)。

了解您如何调用此函数也将很有帮助。