如果我有這樣的串列:
[(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)]
有沒有辦法將具有一個公共數字的串列合并到 sth。像(0,1,2),(0,1,7)等?
我試過玩弄:
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)
uj5u.com熱心網友回復:
如果你想檢查所有對。
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]]
如果您想通過檢查訂單來檢查對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]]
uj5u.com熱心網友回復:
我現在使用它是因為我使用的是 python 庫函式:
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)
我的輸出現在是:
118
87
148
118
94
154
123
118
118
87
148
118
94
154
118
如何檢查字串是否包含數字 118?并列印它在我的結果中出現的頻率。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367493.html
下一篇:我無法訪問串列中的模型欄位
