如何使用Python重新组合列表中的“子列表”?

问题描述

如何使用Python重新组合列表中的“子列表”?例如,我要重新组合以下列表:

[[0,3],[1,2],[0,1],3]]

我希望将[0,1]与[2,3]分组; [0,2]与[1,3]; [0,3]与[1,2],并将其余所有“坐标”保留为单个元组。例如,在这种情况下,我想要

输出为:

[[[0,2]],[[0,3]]

(这里的最后两个元组是“ rest”) 有没有办法可以实现这个目标?谢谢!

解决方法

假设您有一个列表列表(例如示例中的列表,而不是实际的元组):

problem = [[0,3],[1,2],[0,1],3]]

您想对这些对进行配对,以使每个对都包含一次(0,1,2,3)中的每个值?

target = [0,3]

所有无法匹配的对都自己保留吗?这是一种实现方式:

answer = []
while problem:
    # take the first element from problem
    x1 = problem.pop()
    # construct what the paired element should look like
    x2 = [x for x in target if x not in x1]
    try:
        # attempt to remove it from problem
        problem.remove(x2)
        # if the remove succeeds,both x1 and x2 have now been removed,add the pair
        answer.append([x1,x2])
    except ValueError:
        # when trying to remove a non-existent value,a ValueError is raised
        # in that case,there is no matching pair,just add a single
        answer.append(x1)

这是假设:

  • 您永远不会在target
  • 中没有出现的值对中
  • 对中的值总是像target
  • 中那样排序
  • 您不介意将单曲与组合混在一起
  • 您不介意在此过程中修改原始problem

放在一起:

problem = [[0,3]]
target = [0,3]

answer = []
while problem:
    x1 = problem.pop()
    x2 = [x for x in target if x not in x1]
    try:
        problem.remove(x2)
        answer.append([x1,x2])
    except ValueError:
        answer.append(x1)

print(answer)

结果:

[[[0,2]],[[1,3]],3]]
,

由于条目仅是0、1、2、3,并且每个元组中的值都是唯一的,这意味着该元组的总和只能由一对生成,除了3之外,我的意思是: [0,1]是返回值之和的唯一元组,同样,[0,2]是返回2的唯一元组,但是[0,3]和[1,2]返回3,因此他们需要检查它是否是包含一个特定值(例如3

)的元组
a = [[0,3]]
b = []
c = []
d_03 = []
d_12 = []
e = []
f = []

list_of_pairs = []
for tuples in a:
    if sum(tuples) == 1:
        b.append(tuples)
        
    elif sum(tuples) == 2:
        c.append(tuples)
        
    elif sum(tuples) == 4:
        e.append(tuples)
        
    elif sum(tuples) == 5:
        f.append(tuples)
    
    #testing if there is a 3 in that tuple as suggested above
    elif sum(tuples) == 3 and 3 in tuples:
        d_03.append(tuples)

    else:
        d_12.append(tuples)
#one way to figure out the perfect range would be compare the len of the lists that match and use the smaller one,so you don not need the else statement      
for i in range(5):
    #compare len with i+1 because python starts counting from 0
    #if there is any element in those lists,their len will be bigger than 0
    if len(b) >= i+1 and len(f) >= i+1:
        #b have [0,1] only as tuples
        #f have [2,3] only as tuples
        #so when you append one after the other,they will always match
        list_of_pairs.append(b[i])
        list_of_pairs.append(f[i])
        #pop it so at the end will only be in those lists the ones wich don't have their pair
        b.pop(i)
        f.pop(i)
        #insert blank spaces just so you do not modify the indexes of any elements in the list
        b.insert(i-1,'')
        f.insert(i-1,'')
    else:
        #break it because if you run the conditional above it will return IndexError
        #the following i values would also return this error
        break
    
for i in range(5):
    #same principle for c and e
    #and after for d
    if len(c) >= i+1 and len(e) >= i+1:
        list_of_pairs.append(c[i])
        list_of_pairs.append(e[i])
        c.pop(i)
        e.pop(i)
        c.insert(i-1,'')
        e.insert(i-1,'')
    else:
        break
    
for i in range(5):
    if len(d_12) >= i+1 and len(d_03) >= i+1:
        list_of_pairs.append(d_03[i])
        list_of_pairs.append(d_12[i])
        d_12.pop(i)
        d_03.pop(i)
        d_12.insert(i-1,'')
        d_03.insert(i-1,'')
    else:
        break
    
        
#this final list must have the pairs and also the rest
final_list = []
#append the pairs so it will be presented first and as a list inside the final list
final_list.append(list_of_pairs)
#extend by all the previous lists except for list of pairs and a
#this will put any rest in the final list as separate therms from the pairs
final_list.extend(b)
final_list.extend(c)
final_list.extend(d_03)
final_list.extend(d_12)
final_list.extend(e)
final_list.extend(f)
#now the final list might have some '' as rest
#so you can loop through it removing them
while '' in final_list:
    final_list.remove('')
print(final_list)

这是输出:

[[[0,3]]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...