遍历列表,如果列表中的任何索引值的长度 < 3。然后追加到 new_list。忽略在其他地方重复这些元素的索引

Loop through list, if any of the index values in list has length < 3. Then append to new_list. Ignore indexes that repeat those elements anywhere else

假设,我有一个名为 C 的列表。

C = (1,2,3),(4,5,6),(20),(14),(16,17,18,21),(21,22),(22,23),(24,25)

我想将 20 和 14 等值加入 new_list;只有当 20 和 14 不 出现在任何子集中,例如(1,2,3),(16,17,18,21)等。我不想加入(21,22),因为(21)已经存在于(16, 17,18,21)。我也不想加入 (21, 22) 或 (22,23),因为它们会重复。

简而言之,我选择 (24, 25) & (20) & (14) 因为它们在任何子集中都不重复。此外,它们必须少于 3 个元素。

示例

new_list = (24,25,20,14)

这是我到目前为止尝试过的方法。

new_list=[];
for a in range(0, len(C)):
    #suppose to prevent out of range error
    if a <= len(C) -1:
        #suppose to check every subset for repeating elements
        for b in range(len(C[a+1])):
            #again to prevent out of range error
            if b <= len(C) - 1:
                #using set comparison (this may be a semantic bug)
                false_or_true_trip_wire = set(str(C[a])) <= set(C[b])
                #I don't want to accidently append a duplicate
                I_do_not_want_both_sets_too_equal = set(str(C[a])) != set(C[b])
                if false_or_true_trip_wire == 'False':
                    if I_do_not_want_both_sets_too_equal == 'True':
                        new_list.append(C[a])
                        new_list.append(C[b])

输出

任何子集只有一个元素时出现类型错误。它适用于具有 2 个或更多元素的子集,例如 len() 为 3 的子集。例如 (1,2,3)

我正在尝试做的事情之一的示例。

C = (5,6,2),(1,3,4),(4),(3,6,2)

for j in range(0, len(C)):
  print(len(C[j]))

示例的输出

3
3
Traceback (most recent call last):
  File "C:/Users/User/Desktop/come on.py", line 4, in <module>
    print(len(C[j]))
TypeError: object of type 'int' has no len()

问题

那么,有什么方法可以创建一个函数来执行我上面举例说明的操作吗?而且,不使用 str()?

Suppose, I have list called C.

C = (1,2,3),(4,5,6),(20),(14),(16,17,18,21),(21,22),(22,23),(24,25)

首先,您的 C 变量被分配给 tupletupleint 个对象,而不是 listSee this tutorial for more info on these objects. You can also verify this is the case with your own code here.

Type errors when any of the subsets have one element. It would work for subsets with 2 or more elements such as one's with len() of 3. Such as (1,2,3)

你得到 TypeError 因为单个对象的 tuple 实际上不是 tuple,它只是其中的那个对象。因此,如果您在嵌套对象上调用 len 函数,那么 len 函数将 raise 这样的 TypeError 如果嵌套对象不是具有 [=26= 的序列对象] 方法。

int 对象没有此 __len__ 方法,因此当它们被传递到 len 函数时会引发 TypeError: object of type 'int' has no len()。在您分配给 Ctuple 中,您在索引 2 (20) 和 3 (14) 处有两个这样的 int 对象。要真正将它们变成 tuple 对象,您需要使用尾随逗号来制作所谓的单例:

C = (1,2,3),(4,5,6),(20,),(14,),(16,17,18,21),(21,22),(22,23),(24,25)

for j in range(0, len(C)):
    # Now that every object in the tuple is another tuple len(C[j]) will work!
    print(len(C[j]))
    print(type(C[j]))

See it work in python tutor!

既然这样,我不想假设您想将 Ctupletupleint 对象更改为只是 tupletuple 对象的 tuple,所以让我们回到原来的 C = (1,2,3),(4,5,6),(20),(14),(16,17,18,21),(21,22),(22,23),(24,25) 看看我们是否可以编写一些代码,按照您的规则生成您预期的 (24,25,20,14)概述:

In short, I'm taking (24, 25) & (20) & (14) because they don't repeat in any of the subsets. Also, they must have less than 3 elements.

这是我想出的似乎遵循这些规则的代码:

C = (1,2,3),(4,5,6),(20),(14),(16,17,18,21),(21,22),(22,23),(24,25)
temp_C = [x  if type(x) == tuple else tuple([x]) for x in C]
new_c = []
for i,c in enumerate(temp_C):
    l = len(c)
    if l <= 2:
        temp_b = [
            item for j, sub in enumerate(temp_C) for item in sub if i!=j
        ]
        if all(
            [y not in temp_b for y in c]
        ):
            [new_c.append(y) for y in c]

new_c = tuple(new_c)

print(new_c)

输出:

(20, 14, 24, 25)

这与 (24,25,20,14) 的顺序不同,但它非常接近我今晚将为您提供的预期输出。最后,here is that code in python tutor。希望当您逐步了解它时,它的逻辑对您来说会变得更加清晰。