[英]How to set only n number of inputs for input().split() in python?
[英]N number of set() inputs in python
A 是用戶給定的集合 我要檢查的是所有給定的集合是否是 A 的超級集合 我只是不知道如何在循環中為集合獲取多個輸入 我的代碼應該像這樣運行
enter elements of set A : 3 4 5 6 7 2
input total numbers of other sets you want to input: 3
幫助我獲得多個這樣的輸入
input set: 1 2 3 4 5
input set: 3 4 5 4
input set: 3 2 4 1 2 4 5
如果 A 是所有這些的超集print
True
否則print
False
我的代碼就是這樣,但它不能完美地工作
A = set(map(int, input().split()))
for i in range(0,int(input())):
N = set(map(int, input().split()))
if A.union(N) == A:
print(True)
else:
print(False)
要檢查一個集合“A”是否是另一個集合“B”的超集,'set' class 已經有一個方法:
A.issuperset(B)
以下代碼檢查 A 是否是 B 的超集
創建一個空集B
,然后在 for 循環中將輸入集組合到該空集。
然后在for循環之后檢查B
是否是A
的超集
A = set(map(int, input("Enter elements of set A: ").split()))
B = set()
for i in range(0,int(input("Input total numbers of other sets you want to input: "))):
N = set(map(int, input("Input set: ").split()))
B = B | N # or B.union(N)
print(B.issuperset(A))
謝謝大家我嘗試了很多次並且得到了完美運行代碼的方式,如果需要任何更改,請查看此代碼
A = set(map(int, input().split()))
B = set()
for i in range(0,int(input())):
B = B.union(set(map(int, input().split())))
if B == A:
print(True)
else:
print(False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.