Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
סטים
Sets are lists with no duplicate entries. Let's say you want to collect a list of words used in a paragraph:
קבוצות הן רשימות ללא ערכים כפולים. נניח שברצונך לאסוף רשימת מילים המופיעות בפסקה:
print(set("my name is Eric and Eric is my name".split()))
הפלט יהיה רשימה המכילה את "my", "name", "is", "Eric", ולבסוף "and". מכיוון ששאר המשפט משתמש במילים שכבר קיימות בקבוצה, הן אינן מוכנסות פעמיים.
קבוצות הן כלי חזק בפייתון מכיוון שהן יכולות לחשב הבדלים וחיתוכים בין קבוצות אחרות. לדוגמה, נניח שיש לך רשימת משתתפים באירועים A ו-B:
a = set(["Jake", "John", "Eric"])
print(a)
b = set(["John", "Jill"])
print(b)
כדי לגלות אילו חברים השתתפו בשני האירועים, ניתן להשתמש בשיטת "intersection":
``` a = set(["Jake", "John", "Eric"]) b = set(["John", "Jill"])
print(a.intersection(b)) print(b.intersection(a)) ```
כדי לגלות אילו חברים השתתפו רק באחד מהאירועים, השתמש בשיטת "symmetric_difference":
``` a = set(["Jake", "John", "Eric"]) b = set(["John", "Jill"])
print(a.symmetric_difference(b)) print(b.symmetric_difference(a)) ```
כדי לגלות אילו חברים השתתפו רק באירוע אחד ולא באחר, השתמש בשיטת "difference":
``` a = set(["Jake", "John", "Eric"]) b = set(["John", "Jill"])
print(a.difference(b)) print(b.difference(a)) ```
כדי לקבל רשימה של כל המשתתפים, השתמש בשיטת "union":
``` a = set(["Jake", "John", "Eric"]) b = set(["John", "Jill"])
print(a.union(b)) ```
תרגיל
כדי לתרגל, השתמש ברשימות הנתונות להדפיס קבוצה המכילה את כל המשתתפים מאירוע A שלא השתתפו באירוע B.
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
A = set(a)
B = set(b)
print(A.difference(B))
test_output_contains("['Jake', 'Eric']")
success_msg("Nice work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!