列表中的python列表 - 为什么重复子列表

时间:2016-01-09 18:09:05

标签: python list

问题:根据1-9中给定的一串数字创建一个列表,这样字符串中的每个数字都会插入到主列表中,并且每个后面的小数字都会插入到子列表中,直到找到比然后将其插入到正常(主)列表中并继续。

  

问题:前。 string = '654976'预期结果= [6,[5,4],9,[7,6]]   actual = [6,[5,4],[5,4],9,[7,6],[7,6]]< =为什么重复子列表?

def create_inner_list(string):
    mainlist = []
    prev = 0
    sublist = []
    if len(string) <= 1
        return mainlist.append(string)
    for each in string:
        if int(each) <= prev:
            prev = int(each)
            sublist.append(int(each))
            print(sublist)
            mainlist.append(sublist)
            print(mainlist)
            continue
        sublist = []
        mainlist.append(int(each))
        prev = int(each)
    return mainlist

我也尝试在字符串上使用split方法,但它没有多大帮助,或者没有完全理解如何使用它。希望很清楚。提前谢谢。

5 个答案:

答案 0 :(得分:2)

您要为子列表中的每个数字附加一次子列表。您必须将append(sublist)移到if

之外
def create_inner_list(string):
    prev = 0
    mainlist = []
    sublist = []
    for each in string:
        if int(each) <= prev:
            prev = int(each)
            sublist.append(int(each))
        else:
            if sublist:
                mainlist.append(sublist)
            sublist = []
            mainlist.append(int(each))
            prev = int(each)
    if sublist:
        mainlist.append(sublist)
    return mainlist

或更复杂:

def create_inner_list(string):
    prev = 0
    current_list = mainlist = []
    for each in string:
        if int(each) <= prev:
            if current_list is mainlist:
                current_list = []
                mainlist.append(current_list)
        else:
            current_list = mainlist
        prev = int(each)
        current_list.append(prev)
    return mainlist

答案 1 :(得分:1)

这是因为你的continue声明。

每次迭代后

for each in string:

您希望使用代码

重新初始化您的子列表
sublist = []

但是使用您的continue语句,您正在跳过该步骤(因此您将在下一次迭代中再次添加相同的列表)。确保在每个循环开始时重新初始化子列表:

for each in string:
    sublist = []
    ...

答案 2 :(得分:1)

您看到每个子列表两次的原因是,每次找到低于上一个元素的元素时,您都会将子列表添加到主列表中。

def create_inner_list(string):
    mainlist = []
    prev = 0
    sublist = []
    if len(string) <= 1
        return mainlist.append(string)
    for each in string:
        if int(each) <= prev:
            prev = int(each)
            sublist.append(int(each))
            print(sublist)            <----- This code is executed once
            mainlist.append(sublist)  <-----  for each element you are 
            print(mainlist)           <-----  adding to the sublist
            continue
        sublist = []
        mainlist.append(int(each))
        prev = int(each)
    return mainlist

此外,在for循环之后,您应检查是否存在尚未附加到主列表的非空子列表。

请尝试使用此功能:

def create_inner_list(string):
    mainlist = []
    prev = 0
    sublist = []
    if len(string) <= 1:
        return mainlist.append(string)
    for each in string:
        if int(each) <= prev:
            prev = int(each)
            sublist.append(int(each))
            continue
        if len(sublist) > 0:
            mainlist.append(sublist)
            sublist = []
        mainlist.append(int(each))
        prev = int(each)
        print(mainlist)
    if len(sublist) > 0:
        mainlist.append(sublist)
        sublist = []
        print(mainlist)
    return mainlist

答案 3 :(得分:1)

这是使用itertools.groupby和&#34;静态&#34;的另一种解决方案变量

import itertools as it

def group_f(value):
    value = int(value)
    if not hasattr(group_f, 'prev'):
        group_f.prev = 0
        group_f.g = False
    if value > group_f.prev:
        group_f.g = not group_f.g
    group_f.prev = value
    return group_f.g

def create_inner_list(s):
    res = []
    for _, values in it.groupby(s, group_f):
        res.append(next(values))
        res.append(list(values))
    return res

print(create_inner_list('654976'))

产生

['6', ['5', '4'], '9', ['7', '6']]

基本上,group_f会在每个子序列后返回一个True / False的交替序列。

groupby使用它将原始输入值分组,从而将输入序列拆分为子序列。

create_inner_list使用这些列表形成新列表,将每个组的第一个元素和其余元素作为列表附加。

答案 4 :(得分:0)

这个怎么样?:

def create_inner_list(string1):

    main_list = []

    for i in range(0, len(string1), 3):

        main_list.append(string1[i])

        if len(string1) - i == 2:
            main_list.append(string1[i+1])

        elif len(string1) - i == 1:
            break

        else:
            main_list.append(list(string1[i+1] + string1[i+2]))

    return main_list

def main():

    string2 = '714683241390'
    print create_inner_list(string2)

main()