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.

4.6 Συνένωση δύο ή περισσότερα tuples και μεθόδους

Για να συνδυάσετε δύο ή περισσότερα tuples μπορείτε να χρησιμοποιήσετε τον τελεστή +:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
tuple1 = ("apple", "banana", "cherry") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3)
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)

Το αποτέλεσμα θα είναι ένα νέο tuple που περιέχει όλα τα στοιχεία του tuple1 και του tuple2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
('apple', 'banana', 'cherry', 1, 2, 3)
('apple', 'banana', 'cherry', 1, 2, 3)
('apple', 'banana', 'cherry', 1, 2, 3)

Αν θέλετε να επαναλάβετε τα στοιχεία ενός tuple έναν συγκεκριμένο αριθμό φορών, μπορείτε να χρησιμοποιήσετε τον τελεστή *:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
fruits = ("apple", "banana", "cherry")
my_tuple = fruits * 2
print(my_tuple)
fruits = ("apple", "banana", "cherry") my_tuple = fruits * 2 print(my_tuple)
fruits = ("apple", "banana", "cherry")
my_tuple = fruits * 2
print(my_tuple)

Αυτό θα εκτυπώσει:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

Τα στοιχεία του αρχικού tuple επαναλαμβάνονται δύο φορές στο νέο tuple.

Η Python έχει δύο ενσωματωμένες μεθόδους που μπορείτε να χρησιμοποιήσετε σε tuples.

  1. count(): Επιστρέφει τον αριθμό εμφανίσεων μιας τιμής σε ένα tuple.
  2. index(): Επιστρέφει τη θέση της πρώτης εμφάνισης μιας τιμής σε ένα tuple.

Παρακάτω παραδείγματα χρήσης των δύο μεθόδων:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_tuple = (1, 2, 2, 3, 4, 2)
# Επιστρέφει τον αριθμό των εμφανίσεων της τιμής 2 στο tuple
print(my_tuple.count(2)) # εκτυπώνει 3
# Επιστρέφει τη θέση της πρώτης εμφάνισης της τιμής 3 στο tuple
print(my_tuple.index(3)) # εκτυπώνει 3
my_tuple = (1, 2, 2, 3, 4, 2) # Επιστρέφει τον αριθμό των εμφανίσεων της τιμής 2 στο tuple print(my_tuple.count(2)) # εκτυπώνει 3 # Επιστρέφει τη θέση της πρώτης εμφάνισης της τιμής 3 στο tuple print(my_tuple.index(3)) # εκτυπώνει 3
my_tuple = (1, 2, 2, 3, 4, 2)

# Επιστρέφει τον αριθμό των εμφανίσεων της τιμής 2 στο tuple
print(my_tuple.count(2))  # εκτυπώνει 3

# Επιστρέφει τη θέση της πρώτης εμφάνισης της τιμής 3 στο tuple
print(my_tuple.index(3))  # εκτυπώνει 3
top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων