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)
I would like to join values such as 20 and 14 into a new_list; only if 20, and 14 do not
occur in any of the subsets such as (1,2,3),(16,17,18,21), etc. I wouldn't want to join (21,22) because (21) already exists in (16,17,18,21). Neither would I want to join (21, 22) or (22,23) because they repeat.
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.
Example
new_list = (24,25,20,14)
This is what I tried so far.
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])
Output
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)
Example of one of the things I'm trying to do.
C = (5,6,2),(1,3,4),(4),(3,6,2)
for j in range(0, len(C)):
print(len(C[j]))
Output from example
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()
Question
So, is there any way to create a function that would do what I exemplified above? And, without using str()?
Please do give an explanation for someone who hasn't taken a class in Python, but has been self-teaching.
If I can find a function to get rid of all these nested loops it would make it so much easier to get rid of semantic bugs that cause type errors. Any answer would help.