Trong Python, bạn có thể sử dụng a.intersection(b) để tìm các mục phổ biến cho cả hai bộ.
Có cách nào để thực hiện phiên bản đối diện của điều này không? Các mục không phổ biến cho cả a và a ^ b 0; Các mặt hàng độc đáo trong a được kết hợp với các mặt hàng độc đáo trong a ^ b 0?
hỏi ngày 29 tháng 4 năm 2015 lúc 15:13Apr 29, 2015 at 15:13
0
Bạn đang tìm kiếm sự khác biệt đối xứng; Tất cả các phần tử chỉ xuất hiện trong tập A hoặc trong Set B, nhưng không phải cả hai:
a.symmetric_difference(b)Từ tài liệu phương thức a ^ b 3:
Trả về một bộ mới với các phần tử trong tập hợp hoặc khác nhưng không phải cả hai.
Bạn cũng có thể sử dụng toán tử a ^ b 4, nếu cả a và a ^ b 0 đều là bộ:
a ^ btrong khi a ^ b 3 lấy bất kỳ điều gì khác nhau cho đối số khác.
Đầu ra là tương đương với a ^ b 8, sự kết hợp của cả hai bộ trừ đi giao điểm của cả hai bộ.
Seanny123
8.10312 Huy hiệu vàng66 Huy hiệu bạc119 Huy hiệu đồng12 gold badges66 silver badges119 bronze badges
Đã trả lời ngày 29 tháng 4 năm 2015 lúc 15:15Apr 29, 2015 at 15:15
Martijn Pieters ♦ Martijn Pieters♦Martijn Pieters
994K277 Huy hiệu vàng3915 Huy hiệu bạc3259 Huy hiệu đồng277 gold badges3915 silver badges3259 bronze badges
2
a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9}
Đã trả lời ngày 28 tháng 5 năm 2018 lúc 4:39May 28, 2018 at 4:39
Cách tốt nhất là một danh sách hiểu.
a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9]Bạn có thể tham gia cả hai danh sách
Đã trả lời ngày 22 tháng 3 năm 2019 lúc 17:24Mar 22, 2019 at 17:24
2
Hãy thử mã này cho (Set (A) - Giao lộ (A & B))
a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a)đầu ra là a ^ b 9 Tôi hy vọng, nó sẽ hoạt động
Đã trả lời ngày 11 tháng 3 năm 2018 lúc 11:00Mar 11, 2018 at 11:00
5
E, F là hai danh sách bạn muốn kiểm tra Disjoint
a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9]Điều này lặp lại để liệt kê và trả về danh sách phân tách
Đã trả lời ngày 31 tháng 8 năm 2018 lúc 5:16Aug 31, 2018 at 5:16
buda__buda__buda__
331 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges
Bài viết này cho thấy các hoạt động khác nhau trên các bộ python.examples:
Examples:
Trong Python, bên dưới các toán hạng nhanh có thể được sử dụng cho các hoạt động khác nhau.
| cho Liên minh. & cho giao lộ. Từ vì sự khác biệt^ cho sự khác biệt đối xứng
& for intersection.
– for difference
^ for symmetric difference
a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 0____21 a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 22____23a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 5a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 7a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 9a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9] 1a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9] 2
a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9] 3a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 1 a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 22a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9] 6a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 5a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 0a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 7a={1,2,4,5,6} b={5,6,4,9} c=(a^b)&b print(c) # you got {9} 4a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 4a = [ 1,2,3,4] b = [ 8,7,9,2,1] c = [ element for element in a if element not in b] d = [ element for element in b if element not in a] print(c) # output is [ 3,4] print(d) # output is [8,7,9] 2
a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 6a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 7a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 8a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 9
a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 6a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 7a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 2a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 3
a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 6a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 7a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 6a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 7a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 8 a = [1,2,3,4] b = [8,7,9,2,1] c = [] def loop_to_check(e,f): for i in range(len(e)): if e[i] not in f: c.append(e[i]) loop_to_check(a,b) loop_to_check(b,a) print(c) ## output is [3,4,8,7,9] 9
a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 6a = [1,2,3,4,5,6] b = [2,3] for i in b: if i in a: a.remove(i) print(a) 7Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8] Intersection : [2, 4] Difference : [8, 0, 6] Symmetric difference : [0, 1, 3, 5, 6, 8] 2Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8] Intersection : [2, 4] Difference : [8, 0, 6] Symmetric difference : [0, 1, 3, 5, 6, 8] 3
Output:
('Union :', set([0, 1, 2, 3, 4, 5, 6, 8])) ('Intersection :', set([2, 4])) ('Difference :', set([8, 0, 6])) ('Symmetric difference :', set([0, 1, 3, 5, 6, 8]))