Python 确定一个子列表,其中包含元素的绝对值之和最大的交替符号元素 问题:让我们考虑一个具有正值(正负)的数组。 确定包含具有交替符号(正后负,负后正)的元素的子阵列,其中元素的绝对值之和最大

Python 确定一个子列表,其中包含元素的绝对值之和最大的交替符号元素 问题:让我们考虑一个具有正值(正负)的数组。 确定包含具有交替符号(正后负,负后正)的元素的子阵列,其中元素的绝对值之和最大,python,Python,在使用这个列表运行代码之后,它没有给我正确的输出 输出是[6,-3,7,-5,13,12,8,-9],但它应该是[6,-3,7,-5,13,-9,8] a_list = [2, 6, -3, 7, -2, -5, 10, 13, 13, 13, 13, 12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 8, -9, 8, -9, -9, -9, -9, -9, -9, -9, -9, 12, 12, 12, 12, 12] # [6, -3, 7, -5, 12] sum_l

在使用这个列表运行代码之后,它没有给我正确的输出 输出是
[6,-3,7,-5,13,12,8,-9]
,但它应该是
[6,-3,7,-5,13,-9,8]

a_list = [2, 6, -3, 7, -2, -5, 10, 13, 13, 13, 13, 12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 8, -9, 8, -9, -9, -9, -9, -9, -9, -9, -9, 12, 12, 12, 12, 12] # [6, -3, 7, -5, 12]
sum_list = []
i=0
while i <len(a_list)-1:
    if (a_list[i] < 0 and a_list[i+1] > 0):
        if a_list[i] in sum_list:
            pass
        else:
            if sum_list[-1]<0:
                pass
            else:
                sum_list.append(a_list[i])
    if (a_list[i] > 0 and a_list[i+1] < 0):
        if a_list[i] in sum_list:
            pass
        else:
            if sum_list[-1]>0:
                pass
            else:
                sum_list.append(a_list[i])
    else:
         if a_list[i]>0 and  a_list[i+1]>0:
             if max(a_list[i],a_list[i+1]) in sum_list:
                 pass
             else:
                sum_list.append(max(a_list[i],a_list[i+1]))
         if a_list[i]<0 and  a_list[i+1]<0:
             if min(a_list[i],a_list[i+1]) in sum_list:
                 pass
             else:
                sum_list.append(min(a_list[i], a_list[i + 1]))
    i=i+1
print(sum_list)```
a_list=[2,6,-3,7,-2,-5,10,13,13,13,12,12,12,12,12,8,8,8,8,-9,-9,-9,-9,12,-12]#[6,-3,7,-5,12]
sum_list=[]
i=0
而我(0):
如果总和列表中的列表[i]:
通过
其他:
如果总和列表[-1]0和一个列表[i+1]<0):
如果总和列表中的列表[i]:
通过
其他:
如果总和列表[-1]>0:
通过
其他:
sum_list.append(a_list[i])
其他:
如果一个_列表[i]>0和一个_列表[i+1]>0:
如果总和列表中的max(a_列表[i],a_列表[i+1]):
通过
其他:
sum_list.append(max(a_list[i],a_list[i+1]))

如果一个列表[i]更新:代码通常是不正确的,我就这样做了

更新:更正版本见下文

这将给出预期的结果(带有一些内联注释):

更正版本:

#https://stackoverflow.com/questions/65673470/

# test lists for a few (edge) cases
list_1 = []
list_2 = [0]
list_3 = [0, 0]
list_4 = [0, 0, 0]
list_5 = [-2, 4, -2, 4]
list_6 = [2, -1, 2, -2, -3, -4, 3]
list_7 = [-99, 2, -1, 3, -1, 4, 5, -3, -3, 4, -1, -3, -4, -5, 2, 3, -5, 6, 4, 7]

t_list = [ 2, 6
          , -3
          , 7
          , -2, -5
          , 10, 13, 13, 13, 12, 12, 12, 8, 8, 8
          , -9
          , 8
          , -9, -9, -9
          , 12, 12, 12
           ]  # expexted result: [6, -3, 7, -5, 13, -9, 8]

all_lists = [list_1, list_2, list_3, list_4, list_5, list_6, list_7, t_list]

def alternating_nums(a_list):
    sum_list = []
    if not a_list:
        return sum_list
    number = a_list[0]
    sum_list = [number]
    srch_neg = number >= 0  # look for next pos./neg. number
    other_ok = True

    for number in a_list:
        if srch_neg:  # searching for a neg. number
            if number >= 0:
                if number in sum_list:
                    continue
                if number <= sum_list[-1]:
                    continue
                if not other_ok:
                    continue
                sum_list[-1] = number  # found a "better" one
                continue
            if number < 0:
                if number in sum_list:
                    other_ok = False
                    continue
                sum_list.append(number)
                srch_neg = False  # now searching for a pos. number
                other_ok = True  # a consecutive smaller neg. will be ok, too
                continue
        else:  # searching for a pos. number
            if number < 0:
                if number in sum_list:
                    continue
                if number >= sum_list[-1]:
                    continue
                if not other_ok:
                    continue
                sum_list[-1] = number  # found a "better" one
                continue
            if number >= 0:
                if number in sum_list:
                    other_ok = False
                    continue
                sum_list.append(number)
                srch_neg = True  # now searching for a neg. number
                other_ok = True  # a consecutive larger pos. will be ok, too
                continue
    return sum_list

for a_list in all_lists:
    print(a_list)
    sum_list = alternating_nums(a_list)
    print(sum_list, '\n')
#https://stackoverflow.com/questions/65673470/
#少数(边缘)情况的测试列表
列表_1=[]
列表_2=[0]
列表_3=[0,0]
列表_4=[0,0,0]
列表5=[-2,4,-2,4]
列表6=[2,-1,2,-2,-3,-4,3]
列表7=[-99,2,-1,3,-1,4,5,-3,-3,4,-1,-3,-4,-5,2,3,-5,6,4,7]
t_list=[2,6
, -3
7.
, -2, -5
, 10, 13, 13, 13, 12, 12, 12, 8, 8, 8
, -9
8.
, -9, -9, -9
, 12, 12, 12
]#扩展结果:[6,-3,7,-5,13,-9,8]
所有_列表=[列表_1、列表_2、列表_3、列表_4、列表_5、列表_6、列表_7、t_列表]
def交替数值(列表):
sum_list=[]
如果不是清单:
返回和列表
编号=a_列表[0]
总和列表=[数字]
srch_neg=编号>=0#查找下一个位置/neg。数
other_ok=True
对于_列表中的编号:
如果srch_neg:#搜索一个neg。数
如果数字>=0:
如果总和列表中的数字:
持续
如果数字=总和列表[-1]:
持续
如果没有其他问题,请执行以下操作:
持续
sum_list[-1]=数字#找到了一个“更好”的
持续
如果数字>=0:
如果总和列表中的数字:
other_ok=错误
持续
总和列表。追加(数字)
srch_neg=True#正在搜索一个neg。数
other_ok=True#连续较大的位置也可以
持续
返回和列表
对于所有_列表中的_列表:
打印(a_列表)
总和列表=交替列表(a列表)
打印(总和列表“\n”)

似乎更容易将列表分成交替符号子列表,看看哪一个具有最高的
和(map(abs,sublist))
。这不应该是
[6,-3,7,-5,13,-9,8,-9,12]
?@ack no,它不应该有任何重复项,12在8之后,你需要按顺序添加它们。只有当元素旁边有另一个符号相同的元素时,你才能跳转元素,因为你不能有重复项。最后两个数字无效。谢谢@很抱歉,代码需要修改-我正在修改…没问题,也谢谢您的修改版本。这两个程序都非常有用
a_list: []
sum_list: []

a_list: [0]
sum_list: []

a_list: [0, 0]
sum_list: []

a_list: [0, 0, 0]
sum_list: []

a_list: [-2, 4, -2, 4]
sum_list: [-2, 4]

a_list: [2, -4, 2, -4]
sum_list: [2, -4]

a_list: [2, 6, -3, 7, -2, -5, 10, 13, 13, 13, 13, 12, 12, 12, 12, 12, 8, 8, 8, 8, 8, 8, -9, 8, -9, -9, -9, -9, -9, -9, -9, -9, 12, 12, 12, 12, 12]
sum_list: [6, -3, 7, -5, 13, -9, 8]
#https://stackoverflow.com/questions/65673470/

# test lists for a few (edge) cases
list_1 = []
list_2 = [0]
list_3 = [0, 0]
list_4 = [0, 0, 0]
list_5 = [-2, 4, -2, 4]
list_6 = [2, -1, 2, -2, -3, -4, 3]
list_7 = [-99, 2, -1, 3, -1, 4, 5, -3, -3, 4, -1, -3, -4, -5, 2, 3, -5, 6, 4, 7]

t_list = [ 2, 6
          , -3
          , 7
          , -2, -5
          , 10, 13, 13, 13, 12, 12, 12, 8, 8, 8
          , -9
          , 8
          , -9, -9, -9
          , 12, 12, 12
           ]  # expexted result: [6, -3, 7, -5, 13, -9, 8]

all_lists = [list_1, list_2, list_3, list_4, list_5, list_6, list_7, t_list]

def alternating_nums(a_list):
    sum_list = []
    if not a_list:
        return sum_list
    number = a_list[0]
    sum_list = [number]
    srch_neg = number >= 0  # look for next pos./neg. number
    other_ok = True

    for number in a_list:
        if srch_neg:  # searching for a neg. number
            if number >= 0:
                if number in sum_list:
                    continue
                if number <= sum_list[-1]:
                    continue
                if not other_ok:
                    continue
                sum_list[-1] = number  # found a "better" one
                continue
            if number < 0:
                if number in sum_list:
                    other_ok = False
                    continue
                sum_list.append(number)
                srch_neg = False  # now searching for a pos. number
                other_ok = True  # a consecutive smaller neg. will be ok, too
                continue
        else:  # searching for a pos. number
            if number < 0:
                if number in sum_list:
                    continue
                if number >= sum_list[-1]:
                    continue
                if not other_ok:
                    continue
                sum_list[-1] = number  # found a "better" one
                continue
            if number >= 0:
                if number in sum_list:
                    other_ok = False
                    continue
                sum_list.append(number)
                srch_neg = True  # now searching for a neg. number
                other_ok = True  # a consecutive larger pos. will be ok, too
                continue
    return sum_list

for a_list in all_lists:
    print(a_list)
    sum_list = alternating_nums(a_list)
    print(sum_list, '\n')