Δημοσιεύτηκε: 01 Ιουν 2009, 01:04
Καλησπέρα παιδιά, υποβάλω το δεύτερο μισό του κεφαλαίου 11 (δομές δεδομένων) και αναλαμβάνω και το 14 (είσοδος έξοδος).
και η μετάφρασή του
- Κώδικας: Επιλογή όλων
Sequences
Lists, tuples and strings are examples of sequences, but what are sequences and what is so
special about them?
The major features is that they have membership tests (i.e. the in and not in expressions)
and indexing operations. The indexing operation which allows us to fetch a particular item
in the sequence directly.
The three types of sequences mentioned above - lists, tuples and strings, also have a
slicing operation which allows us to retrieve a slice of the sequence i.e. a part of the
sequence.
Example:
#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
name = 'swaroop'
# Indexing or 'Subscription' operation
print('Item 0 is', shoplist[0])
print('Item 1 is', shoplist[1])
print('Item 2 is', shoplist[2])
print('Item 3 is', shoplist[3])
print('Item -1 is', shoplist[-1])
print('Item -2 is', shoplist[-2])
print('Character 0 is', name[0])
# Slicing on a list
print('Item 1 to 3 is', shoplist[1:3])
print('Item 2 to end is', shoplist[2:])
print('Item 1 to -1 is', shoplist[1:-1])
print('Item start to end is', shoplist[:])
# Slicing on a string
print('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1])
print('characters start to end is', name[:])
Output:
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Character 0 is s
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
How It Works:
First, we see how to use indexes to get individual items of a sequence. This is also referred
to as the subscription operation. Whenever you specify a number to a sequence within
square brackets as shown above, Python will fetch you the item corresponding to that
position in the sequence. Remember that Python starts counting numbers from 0. Hence,
shoplist[0] fetches the first item and shoplist[3] fetches the fourth item in the
shoplist sequence.
The index can also be a negative number, in which case, the position is calculated from the
end of the sequence. Therefore, shoplist[-1] refers to the last item in the sequence and
shoplist[-2] fetches the second last item in the sequence.
The slicing operation is used by specifying the name of the sequence followed by an
optional pair of numbers separated by a colon within square brackets. Note that this is very
similar to the indexing operation you have been using till now. Remember the numbers are
optional but the colon isn't.
The first number (before the colon) in the slicing operation refers to the position from
where the slice starts and the second number (after the colon) indicates where the slice will
stop at. If the first number is not specified, Python will start at the beginning of the
sequence. If the second number is left out, Python will stop at the end of the sequence.
Note that the slice returned starts at the start position and will end just before the end
position i.e. the start position is included but the end position is excluded from the
sequence slice.
Thus, shoplist[1:3] returns a slice of the sequence starting at position 1, includes
position 2 but stops at position 3 and therefore a slice of two items is returned. Similarly,
shoplist[:] returns a copy of the whole sequence.
You can also do slicing with negative positions. Negative numbers are used for positions
from the end of the sequence. For example, shoplist[:-1] will return a slice of the
sequence which excludes the last item of the sequence but contains everything else.
You can also provide a third argument for the slice, which is the step for the slicing (by
default, the step size is 1):
>>> shoplist = ['apple', 'mango', 'carrot', 'banana']
>>> shoplist[::1]
['apple', 'mango', 'carrot', 'banana']
>>> shoplist[::2]
['apple', 'carrot']
>>> shoplist[::3]
['apple', 'banana']
>>> shoplist[::-1]
['banana', 'carrot', 'mango', 'apple']
Notice that when the step is 2, we get the items with position 0, 2, ... When the step size is
3, we get the items with position 0, 3, etc.
Try various combinations of such slice specifications using the Python interpreter
interactively i.e. the prompt so that you can see the results immediately. The great thing
about sequences is that you can access tuples, lists and strings all in the same way!
Set
Sets are unordered collections of simple objects. These are used when the existence of an
object in a collection is more important than the order or how many times it occurs.
Using sets, you can test for membership, whether it is a subset of another set, find the
intersection between two sets, and so on.
>>> bri = set(['brazil', 'russia', 'india'])
>>> 'india' in bri
True
>>> 'usa' in bri
False
>>> bric = bri.copy()
>>> bric.add('china')
>>> bric.issuperset(bri)
True
>>> bri.remove('russia')
>>> bri & bric # OR bri.intersection(bric)
{'brazil', 'india'}
How It Works:
The example is pretty much self-explanatory because it involves basic set theory
mathematics taught in school.
References
When you create an object and assign it to a variable, the variable only refers to the object
and does not represent the object itself! That is, the variable name points to that part of
your computer's memory where the object is stored. This is called as binding of the name
to the object.
Generally, you don't need to be worried about this, but there is a subtle effect due to
references which you need to be aware of:
Example:
#!/usr/bin/python
# Filename: reference.py
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same
object!
del shoplist[0] # I purchased the first item, so I remove it from the
list
print('shoplist is', shoplist)
print('mylist is', mylist)
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object
print('Copy by making a full slice')
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print('shoplist is', shoplist)
print('mylist is', mylist)
# notice that now the two lists are different
Output:
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
How It Works:
Most of the explanation is available in the comments.
Remember that if you want to make a copy of a list or such kinds of sequences or complex
objects (not simple objects such as integers), then you have to use the slicing operation to
make a copy. If you just assign the variable name to another name, both of them will refer
to the same object and this could be trouble if you are not careful.
Note for Perl programmers
Remember that an assignment statement for lists does not create a copy. You have to
use slicing operation to make a copy of the sequence.
More About Strings
We have already discussed strings in detail earlier. What more can there be to know? Well,
did you know that strings are also objects and have methods which do everything from
checking part of a string to stripping spaces!
The strings that you use in program are all objects of the class str. Some useful methods of
this class are demonstrated in the next example. For a complete list of such methods, see
help(str).
Example:
#!/usr/bin/python
# Filename: str_methods.py
name = 'Swaroop' # This is a string object
if name.startswith('Swa'):
print('Yes, the string starts with "Swa"')
if 'a' in name:
print('Yes, it contains the string "a"')
if name.find('war') != -1:
print('Yes, it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
Output:
$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
How It Works:
Here, we see a lot of the string methods in action. The startswith method is used to find
out whether the string starts with the given string. The in operator is used to check if a
given string is a part of the string.
The find method is used to do find the position of the given string in the string or returns
-1 if it is not successful to find the substring. The str class also has a neat method to join
the items of a sequence with the string acting as a delimiter between each item of the
sequence and returns a bigger string generated from this.
Summary
We have explored the various built-in data structures of Python in detail. These data
structures will be essential for writing programs of reasonable size.
Now that we have a lot of the basics of Python in place, we will next see how to design and
write a real-world Python program.
και η μετάφρασή του
- Κώδικας: Επιλογή όλων
Ακολουθίες (Sequences)
Οι λίστες, οι πλειάδες και οι συμβολοσειρές είναι παραδείγματα ακολουθιών, αλλά τι είναι οι ακολουθίες και τι το ιδιαίτερο με αυτές;
Tα κυρίαρχα χαρακτηριστικά είναι ότι έχουν δοκιμές ένταξης (membership tests) (δηλ. τις εκφράσεις in και not in) και λειτουργίες ευρετηρίασης (indexing operations). H λειτουργία ευρετηρίασης μας επιτρέπει να κάνουμε μετάκληση ενός ιδιαίτερου στοιχείου στην ακολουθία απευθείας.
Οι τρεις τύποι ακολουθιών που αναφέρθηκαν παραπάνω, λίστες, πλειάδες και συμβολοσειρές έχουν επίσης μια λειτουργία τεμαχισμού (slicing operation), που μας επιτρέπει να ανακτούμε ένα μέρος (slice) της ακολουθίας.
Παράδειγμα:
#!/usr/bin/python
# Filename: seq.py
shoplist = ['apple', 'mango', 'carrot', 'banana']
name = 'swaroop'
# Indexing or 'Subscription' operation
print('Item 0 is', shoplist[0])
print('Item 1 is', shoplist[1])
print('Item 2 is', shoplist[2])
print('Item 3 is', shoplist[3])
print('Item -1 is', shoplist[-1])
print('Item -2 is', shoplist[-2])
print('Character 0 is', name[0])
# Slicing on a list
print('Item 1 to 3 is', shoplist[1:3])
print('Item 2 to end is', shoplist[2:])
print('Item 1 to -1 is', shoplist[1:-1])
print('Item start to end is', shoplist[:])
# Slicing on a string
print('characters 1 to 3 is', name[1:3])
print('characters 2 to end is', name[2:])
print('characters 1 to -1 is', name[1:-1])
print('characters start to end is', name[:]
Έξοδος:
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Character 0 is s
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
Πως δουλεύει:
Αρχικά βλέπουμε πως να χρησιμοποιούμε ευρετήρια για να παίρνουμε μοναδικά στοιχεία της ακολουθίας. Αυτό αναφέρεται επίσης σαν δανειστική λειτουργία (subscription operation). Οποτεδήποτε καθορίζεις ένα νούμερο σε μια ακολουθία μέσα σε αγκύλες,όπως φαίνεται παραπάνω, η Python θα κάνει μετάκληση του στοιχείου που αντιστοιχεί σε αυτή τη θέση στην ακολουθία. Θυμηθείτε ότι η Python αρχει να μετράει τα νούμερα από το μηδέν. Για αυτό το λόγο η shoplist[0] κάνει μετάκληση του πρώτου στοιχείου και η shoplist[3] κάνει ματάκληση του τέταρτου στοιχείου στην ακολουθία shoplist.
Το ευρετήριο μπορεί να είναι ένας αρνητικός αριθμός, στην οποία περίπτωση, η θέση υπολογίζεται από το τέλος της ακολουθίας. Έτσι, η shoplist[-1] αναφέρεται στο τελευταίο στοιχείο στην ακολουθία και η shoplist[-2] κάνει μετάκληση του δεύτερου από το τέλος στοιχείου στην ακολουθία.
Η λειτουργία τεμαχισμού χρησιμοποιείται καθορίζοντας την ονομασία της ακολουθίας, ακολουθούμενη από ένα προαιρετικό ζευγάρι αριθμών, που διαχωρίζονται από διπλή τελεία μέσα σε αγκύλες. Σημειώστε ότι αυτό είναι παρόμοιο με τη λειτουργία ευρετηρίασης, που έχει χρησιμοποιηθεί μέχρι τώρα. Θυμηθείτε ότι τα νούμερα είναι προαιρετικά αλλά η διπλή τελεία δεν είναι.
Το πρώτο νούμερο (πριν από τη διπλή τελεία) στη λειτουργία τεμαχισμού αναφέρεται στη θέση από όπου το τμήμα (slice) αρχίζει και το δεύτερο νούμερο (μετά τη διπλή τελεία) δείχνει που θα σταματήσει το τμήμα. Εάν δεν καθορίζεται το πρώτο νούμερο, η Python θα αρχίσει στην αρχή της ακολουθίας. Εάν το δεύτερο νούμερο παραλήφθηκε, η Python θα σταματήσει στο τέλος της ακολουθίας. Σημειώστε ότι το τμήμα επέστρεψε starts στην αρχική θέση και θα τελειώσει ακριβώς πριν από τη θέση end, δηλ. η αρχική θέση περιλαμβάνεται αλλά η τελική θέση αποκλείεται από το τμήμα της ακολουθίας. Έτσι η shoplist[1:3] επιστρέφει ένα τμήμα της ακολουθίας αρχίζοντας στη θέση 1, περιλαμβάνει τη θέση 2, αλλά σταματάει στη θέση 3, συνεπώς, ένα τμήμα δύο αντικειμένων επιστρέφεται. Παρόμοια, η shoplist[:] επιστρέφει ένα αντίγραφο όλης της ακολουθίας.
Μπορείτε επίσης να κάνετε τεμαχισμό με αρνητικές θέσεις. Οι αρνητικοί αριθμοί χρησιμοποιούνται για θέσεις από το τέλος της ακολουθίας. Για παράδειγμα, η shoplist[:-1] θα επιστρέψει ένα τμήμα της ακολουθίας η οποία παραλείπει το τελευταίο στοιχείο της ακολουθίας, αλλά περιέχει κάθε άλλο. Μπορείτε επίσης να δώσετε ένα τρίτο όρισμα για το τμήμα, το οποίο είναι το βήμα (step) για τον τεμαχισμό (από προεπιλογή το μέγεθος βήματος είναι 1):
>>> shoplist = ['apple', 'mango', 'carrot', 'banana']
>>> shoplist[::1]
['apple', 'mango', 'carrot', 'banana']
>>> shoplist[::2]
['apple', 'carrot']
>>> shoplist[::3]
['apple', 'banana']
>>> shoplist[::-1]
['banana', 'carrot', 'mango', 'apple']
Παρατηρήστε ότι όταν το βήμα είναι 2, παίρνουμε τα στοιχεία με θέση 0, 2, ...Όταν το μέγεθος βήματος είναι 3, παίρνουμε τα στοιχεία με θέση 0, 3, κ.τ.λ.
Δοκιμάστε διάφορους συνδυασμούς από τέτοιους προσδιορισμούς τμημάτων, χρησιμοποιώντας το διερμηνευτή Python αλληλεπιδραστικά, δηλ. την προτροπή (prompt) έτσι ώστε να μπορείτε να δείτε τα αποτελέσματα αμέσως. Το σπουδαίο θέμα με τις ακολουθίες είναι ότι μπορείτε να εισάγετε πλειάδες, λίστες και συμβολοσειρές όλες με τον ίδιο τρόπο.
Σύνολο (Set)
Τα σύνολα είναι μη διατεταγμένες συλλογές απλών αντικεινένων. Αυτά χρησιμοποιούνται όταν η ύπαρξη ενός αντικειμένου σε μια συλλογή είναι πιο σπουδαία από την εντολή ή πόσες φορές αυτή συμβαίνει. Χρησιμοποιώντας τα σύνολα, μπορείτε να ελέγξετε για ένταξη (membership), εάν είναι ένα υποσύνολο (subset) ενός άλλου συνόλου, να βρείτε την τομή (intersection) ανάμεσα σε δύο σύνολα και ούτω καθεξής.
>>> bri = set(['brazil', 'russia', 'india'])
>>> 'india' in bri
True
>>> 'usa' in bri
False
>>> bric = bri.copy()
>>> bric.add('china')
>>> bric.issuperset(bri)
True
>>> bri.remove('russia')
>>> bri & bric # OR bri.intersection(bric)
{'brazil', 'india'}
Πως δουλεύει:
To παράδειγμα εξηγείται από μόνο του, διότι περιλαμβάνει βασική θεωρία μαθηματικών συνόλων που διδάσκεται στο σχολείο.
Παραπομπές (References)
Όταν δημιουργείτε ένα αντικείμενο και το εκχωρείτε σε μια μεταβλητή, μόνον η μεταβλητή παραπέμπει στο αντικείμενο και δεν αντιπροσωπεύει το αντικείμενο τον εαυτό του.H ονομασία της μεταβλητής δείχνει σε αυτό το σημείο της μνήμης του υπολογιστή, που αποθηκεύεται το αντικείμενο. Αυτό ονομάζεται συσχέτιση (binding) της ονομασίας με το αντικείμενο. Γενικά δεν πρέπει να ανησυχείτε για αυτό, αλλά υπάρχει μια λεπτή επίδραση εξαιτίας των παραπομπών την οποία πρέπει να αντιληφθείτε.
Παράδειγμα:
#!/usr/bin/python
# Filename: reference.py
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist είναι ακόμα μια ονομασία που δείχνει στο ίδιο αντικείμενο !
del shoplist[0] #Αγόρασα το πρώτο στοιχείο κι έτσι το μετακίνησα από τη λίστα
print('shoplist is', shoplist)
print('mylist is', mylist)
# παρατηρήστε ότι και τα δύο shoplist και mylist τυπώνουν την ίδια λίστα χωρίς
# το 'apple' επιβεβαιώνοντας ότι δείχνουν στο ίδιο αντικείμενο
print('Copy by making a full slice')
mylist = shoplist[:] # φτιάξε ένα αντίγραφο κάνοντας ένα πλήρες τμήμα
del mylist[0] # μετακίνησε το πρώτο στοιχείο
print('shoplist is', shoplist)
print('mylist is', mylist)
# παρατηρήστε ότι τώρα οι δύο λίστες είναι διαφορετικές
Έξοδος:
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
Πως δουλεύει:
Το μεγαλύτερο μέρος της εξήγησης είναι διαθέσιμο στα σχόλια. Θυμηθείτε ότι εάν θέλετε να φτιάξετε ένα αντίγραφο μιας λίστας, ή τέτοιου είδους ακολουθίες, ή σύμπλοκα αντικείμενα (όχι απλά αντικείμενα όπως ακέραιους αριθμούς), τότε πρέπει να χρησιμοποιήσετε τη λειτουργία τεμαχισμού για να φτιάξετε αντίγραφο. Εάν εκχωρείτε την ονομασία της μεταβλητής σε μια άλλη ονομασία, τότε και οι δυο τους θα παραπέμπουν στο ίδιο αντικείμενο και αυτό θα μπορούσε να είναι πρόβλημα για σας, εάν δεν είστε προσεκτικοί.
Σημείωση για τους προγραμματιστές της Perl
Θυμηθείτε ότι μια εντολή εκχώρησης για λίστες δεν δημιουργεί αντίγραφο. Πρέπει να χρησιμοποιήσεις τη λειτουργία τεμαχισμού για να φτιάξεις αντίγραφο της ακολουθίας.
Περισσότερα για τις συμβολοσειρές
Έχουμε ήδη συζητήσει νωρίτερα για τις συμβολοσειρές (strings) λεπτομερώς. Όμως τι περισσότερο μπορούμε να μάθουμε; Λοιπόν, γνωρίζατε ότι οι συμβολοσειρές είναι επίσης αντικείμενα που κάνουν τα πάντα, από τον έλεγχο του τμήματος μιας συμβολοσειράς μέχρι απογύμνωση χώρων.
Οι συμβολοσειρές που χρησιμοποιείς στο πρόγραμμα είναι όλες αντικείμενα της κλάσης str. Μερικές χρήσιμες μεθόδοι της τάξης παρουσιάζονται στο επόμενο παράδειγμα. Για μια ολοκληρωμένη λίστα τέτοιων μεθόδων, κοιτάξτε τη help(str).
Παράδειγμα:
#!/usr/bin/python
# Filename: str_methods.py
name = 'Swaroop' # Αυτή είναι μια συμβολοσειρά αντικείμενο
if name.startswith('Swa'):
print('Yes, the string starts with "Swa"')
if 'a' in name:
print('Yes, it contains the string "a"')
if name.find('war') != -1:
print('Yes, it contains the string "war"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
Έξοδος:
$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
Πως δουλεύει:
Eδώ βλέπουμε πολλές μεθόδους της συμβολοσειράς σε ενέργεια. Η μέθοδος startswith χρησιμοποιείτε για να ανακαλύψουμε αν η συμβολοσειρά αρχίζει με τη δοθείσα συμβολοσειρά. Ο τελεστής in χρησιμοποιείται για να ελέγξει αν η δοθείσα συμβολοσειρά είναι μέρος της συμβολοσειράς. Η μέθοδος find χρησιμοποιείται για να ανακαλύψει τη θέση της δοθείσας συμβολοσειράς στη συμβολοσειρά ή επιστρέφει -1 εάν δεν επιτύχει την ανακάλυψη της υποσυμβολοσειράς (substring). Η κλάση str επίσης έχει μια καθαρή μέθοδο για να ενώνει (join) τα στοιχεία μιας ακολουθίας με τη συμβολοσειρά, ενεργώντας σαν διαχωριστικό (delimiter) ανάμεσα σε κάθε στοιχείο της ακολουθίας και επιστρέφει μια μεγαλύτερη συμβολοσειρά γεννημένη από αυτό.
Σύνοψη
Έχουμε διερευνήσει τις διάφορες ενσωματωμένες δομές δεδομένων της Python με λεπτομέρεια. Αυτές οι δομές δεδομένων θα είναι απαραίτητες για τη συγγραφή προγραμμάτων σε λογικό μέγεθος. Τώρα που έχουμε πολλά από τα βασικά της Python επίκαιρα, θα δούμε πως να σχεδιάζουμε και να γράφουμε ένα πρόγραμμα Python, στον πραγματικό κόσμο.