If I have a list like this:
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (0, 6), (1, 7), (1, 8), (2, 9), (2, 10), (3, 11), (4, 12), (4, 13), (5, 14), (5, 15)]
Is there a way to merge the lists with one common number into sth. like (0,1,2),(0,1,7) etc?
I have tried playiing around with:
list_1 = [1, 2, 2, 3]
list_2 = [3, 4]
set_1 = set(list_1)
set_2 = set(list_2)
list_2_items_not_in_list_1 = list(set_2 - set_1)
combined_list = list_1 list_2_items_not_in_list_1
print(combined_list)
CodePudding user response:
If you want check all pairs.
from itertools import permutations
lst = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (0, 6), (1, 7), (1, 8), (2, 9), (2, 10), (3, 11), (4, 12), (4, 13), (5, 14), (5, 15)]
res = [(*a, b[1]) for a, b in permutations(lst, r=2) if a[1] == b[0]]
If you want check pairs with checking of order into lst
.
from itertools import combinations
lst = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0), (0, 6), (1, 7), (1, 8), (2, 9), (2, 10), (3, 11), (4, 12), (4, 13), (5, 14), (5, 15)]
res = [(*a, b[1]) for a, b in combinations(lst, r=2) if a[1] == b[0]]
CodePudding user response:
I used this now because I am using a python libary function:
for x in range(len(res)):
k=res[x]
x=k[0]
y=k[1]
z=k[2]
ang=rdMolTransforms.GetAngleDeg(conf, x, y, z)
allangle=str("%.0f" % ang)
My output is now:
118
87
148
118
94
154
123
118
118
87
148
118
94
154
118
How do i check if the string contains the number 118? and print how often it appears in my results.