Beginning PythonFrom Novice to Professional, Second Edition 2008 phần 2

Thông báo như thế nào Cecil đã khỏi hoàn toàn, và độ dài của danh sách đã bị thu hẹp 5-4. Del tuyên bố có thể được sử dụng để xóa những thứ khác không phải là yếu tố danh sách. Nó có thể được sử dụng với từ điển (xem Chương 4) hoặc thậm chí biến. Để biết thêm thông tin, xem Chương 5. | 42 CHAPTER 2 LISTS AND TUPLES Notice how Cecil is completely gone and the length of the list has shrunk from five to four. The del statement may be used to delete things other than list elements. It can be used with dictionaries see Chapter 4 or even variables. For more information see Chapter 5. Assigning to Slices Slicing is a very powerful feature and it is made even more powerful by the fact that you can assign to slices name list Perl name P e r l name 2 list ar name P e a r So you can assign to several positions at once. You may wonder what the big deal is. Couldn t you just have assigned to them one at a time Sure but when you use slice assignments you may also replace the slice with a sequence whose length is different from that of the original name list Perl name 1 list ython name P y t h o n Slice assignments can even be used to insert elements without replacing any of the original ones numbers 1 5 numbers 1 1 2 3 4 numbers 1 2 3 4 5 Here I basically replaced an empty slice thereby really inserting a sequence. You can do the reverse to delete a slice numbers 1 2 3 4 5 numbers 1 4 numbers 1 5 As you may have guessed this last example is equivalent to del numbers 1 4 . Now why don t you try a slice assignment with a step size different from 1 Perhaps even a negative one CHAPTER 2 LISTS AND TUPLES 43 List Methods You ve encountered functions already but now it s time to meet a close relative methods. A method is a function that is tightly coupled to some object be it a list a number a string or whatever. In general a method is called like this arguments As you can see a method call looks just like a function call except that the object is put before the method name with a dot separating them. You get a much more detailed explanation of what methods really are in Chapter 7. Lists have several methods that allow you to examine or modify their contents. append The append method is used to append an object to the end of a list 1st 1 2 3

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
34    78    1    03-06-2024
6    87    2    03-06-2024
10    72    1    03-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.