Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

5.4 Loop Sets και join Sets

Για να επαναλάβετε ένα σύνολο, μπορείτε να χρησιμοποιήσετε μια for βρόχο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
thisset = {"apple", "banana", "cherry"}
for x in thisset:
  print(x)

Αυτό θα εκτυπώσει το κάθε στοιχείο του συνόλου σε μια νέα γραμμή.

Μπορείτε να συνδυάσετε δύο ή περισσότερα σύνολα στην Python χρησιμοποιώντας διάφορους τρόπους.

Μπορείτε να χρησιμοποιήσετε τη μέθοδο union(), η οποία επιστρέφει ένα νέο σύνολο που περιέχει όλα τα στοιχεία από τα δύο σύνολα ή τη μέθοδο update(), η οποία εισάγει όλα τα στοιχεία από ένα σύνολο στο άλλο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Using the union() method:
set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "mango", "apple"}
set3 = set1.union(set2)
print(set3)
# Using the update() method:
set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "mango", "apple"}
set1.update(set2)
print(set1)
# Using the union() method: set1 = {"apple", "banana", "cherry"} set2 = {"orange", "mango", "apple"} set3 = set1.union(set2) print(set3) # Using the update() method: set1 = {"apple", "banana", "cherry"} set2 = {"orange", "mango", "apple"} set1.update(set2) print(set1)
# Using the union() method:
set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "mango", "apple"}
set3 = set1.union(set2)
print(set3)

# Using the update() method:
set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "mango", "apple"}
set1.update(set2)
print(set1)

Και οι δύο μέθοδοι επιστρέφουν ένα νέο σύνολο που περιέχει όλα τα στοιχεία από τα δύο αρχικά σύνολα. Η διαφορά είναι ότι η μέθοδος union() δεν αλλάζει το αρχικό σύνολο, ενώ η μέθοδος update() προσθέτει τα στοιχεία στο αρχικό σύνολο.

Μπορείτε να χρησιμοποιήσετε τη μέθοδο intersection_update() για να κρατήσετε μόνο τα αντικείμενα που υπάρχουν σε και τα δύο sets. Αυτή η μέθοδος τροποποιεί το πρώτο σύνολο για να κρατήσει μόνο τα κοινά στοιχεία μεταξύ του πρώτου και του δεύτερου συνόλου.

[adinserter block=”2″]

Παρακάτω είναι ένα παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set1.intersection_update(set2)
print(set1)
set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set1.intersection_update(set2) print(set1)
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set1.intersection_update(set2)

print(set1)

Το αποτέλεσμα θα είναι το σύνολο {"apple"} καθώς αυτό είναι το μόνο κοινό στοιχείο στα δύο σύνολα.

Επίσης, μπορείτε να χρησιμοποιήσετε τη μέθοδο intersection() αντί για τη μέθοδο intersection_update() για να δημιουργήσετε ένα νέο σύνολο που θα περιέχει τα κοινά στοιχεία των δύο αρχικών συνόλων χωρίς να τροποποιήσετε τα αρχικά σύνολα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.intersection(set2)
print(set3)
set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.intersection(set2) print(set3)
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1.intersection(set2)

print(set3)

Το αποτέλεσμα θα είναι το σύνολο {"apple"} καθώς αυτό είναι το μόνο κοινό στοιχείο στα δύο σύνολα, αλλά τα αρχικά σύνολα set1 και set2 δεν έχουν τροποποιηθεί.

Η μέθοδος symmetric_difference_update() θα διατηρήσει μόνο τα στοιχεία που ΔΕΝ είναι παρόντα σε και τα δύο σύνολα. Με άλλα λόγια, θα διαγράψει τα κοινά στοιχεία από τα σύνολα.

Εδώ είναι ένα παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set1.symmetric_difference_update(set2)
print(set1)
set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set1.symmetric_difference_update(set2) print(set1)
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set1.symmetric_difference_update(set2)

print(set1)

Το παραπάνω πρόγραμμα εκτυπώνει:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{'cherry', 'google', 'banana', 'microsoft'}
{'cherry', 'google', 'banana', 'microsoft'}
{'cherry', 'google', 'banana', 'microsoft'}

Παρατηρούμε ότι το αποτέλεσμα είναι ένα σύνολο με όλα τα στοιχεία που δεν υπάρχουν σε και τα δύο σύνολα, δηλαδή τα στοιχεία “apple”, “microsoft” και “google”.

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων