[英]How to conditionally append tuples from one list to another list of tuples?
[英]How to append tuples from for loop to list
inter = []
for inter in intersections:
a, b = inter
for i in range(6):
for j in range(6):
lines_edges[int(b) + i, int(a) + j] = [0, 0, 255]
cv2.circle(lines_edges,(int(a),int(b)), 5, (0, 255, 0), cv2.FILLED)
print(inter)
OUTPUT:
(255.49047141424273, 253.22266800401204)
(261.42908992416034, 227.0)
(312.7111046847889, 227.0)
(361.3178929765886, 296.4939799331104)
(362.0041891809848, 227.5)
(362.5263157894737, 165.73684210526315)
(410.92892156862746, 228.5)
(463.1585111920874, 228.5)
OUTPUT 我想要:
[(255.49047141424273, 253.22266800401204) (261.42908992416034, 227.0) (312.7111046847889, 227.0) (361.3178929765886, 296.4939799331104) (362.0041891809848, 227.5) (362.5263157894737, 165.73684210526315) (410.92892156862746, 228.5) (463.1585111920874, 228.5)]
你只需要:
append()
將元組添加到數組例如:
result = []
tuples = [(1,2), (3,4)]
for tuple in tuples:
result.append(tuple)
或者,如果您想從操作中構造元組,例如您希望列表具有包含每個輸入元組的最小值和最大值的元組,您可以執行以下操作:
result = []
tuples = [(3,1,7,2), (3,4,2,1)]
for tuple in tuples:
inner_tuple = (min(tuple), max(tuple))
result.append(inner_tuple)
我使用上面的元組列表作為參考,您只需要在 for 循環中使用.append()
即可。
您需要更改變量名稱以包含結果,因為當您將變量inter
設置為intersections
的內部元素時, inter
將不再是一個空數組,因此您需要更改它:
# Change inter to result
result = []
for inter in intersections:
# do things here...
item = .....
result.append(item)
print(result)
您只需要在循環中的內部列表中使用 append 元組。
inter.append(tuple)
s = []
for inter in intersections:
a, b = inter
for i in range(6):
for j in range(6):
lines_edges[int(b) + i, int(a) + j] = [0, 0, 255]
cv2.circle(lines_edges,(int(a),int(b)), 5, (0, 255, 0), cv2.FILLED)
s.append(inter)
a = [(int(x), int(y)) for x,y in s]
print(a)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.