Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 85

Sequence Operations

Оглавление

In the previous section, we saw the usefulness of the string slicing and concatenation operations. These operations are also useful with lists, because lists are sequences of elements. We list other commonly used sequence operations in Table 3.3. Note that the first three (in, not in, and +) use operators, the next two use indexing and slicing as described earlier, the next three (len, min, and max) use functions, and the last operation employs a method to determine how many times an object occurs within the specified sequence. Additional sequence operations as well as additional details pertaining to the operations in the table are in the official Python documentation (Python Software Foundation, 2019, “Common Sequence Operations”).

Table 3.3

The Python code in Figure 3.11 illustrates the use of several of these sequence operations on both a string and a list. Lines 2 and 3 of the code in Figure 3.11 construct the list and string, respectively. Lines 6 and 7 use the len function to determine how many elements are in the list and string sequences. Lines 10 and 11 use the .count() method of each sequence type to determine how many times the character “a” appears in each sequence. Lines 14 and 15 determine if the character “e” is in each sequence. Lines 18 and 19 use the slicing operation to report the third to seventh elements in each sequence.

Description

Figure 3.11 Commonly Used Sequence Operations

Figure 3.12 illustrates the output of executing the Python code. Interestingly, the length of the list is only 10, whereas the length of the string is 35, even though the specification of the list appears longer in the code. The explanation for this is that the len function is counting the number of elements, and in the list, some of the elements are several characters or numeric digits. The string length is determined by the number of characters in the string, which includes the spaces between the letters in addition to the string’s alphanumeric characters. Perhaps more surprising is the next output that reports the character “a” appears only once in the list but three times in the string. Looking at the list, we see the letter “a” twice, right at the beginning. However, the second instance is the string “ab” and not solely the character “a,” so it is not a “match” for what we are counting. Because the string is a sequence of characters, the interpreter examines each character individually, and thus the result is 3, as the character “a” appears three times in the string. The next output reports that “e” does not appear in the list but does appear in the string. When looking at line 2 and the construction of the list, we can see the letter “e” in the True and False elements. However, these are Boolean values and so are not a match when checking to see if the character “e” is in the list. The string operates differently, due to the fact that the interpreter examines each character individually. Finally, the output for the slicing operation (recall that the index 2 actually references the third element in the sequence, and even though the index 7 references the eighth element in each sequence, the last value specified in the slice is not included) shows that for a list, five list elements are returned, some being strings, a number, and a Boolean value, whereas for a string, five characters are returned (including a space in the middle in this example). These and other sequence operations are extremely useful in Python, and we will see more applications later in this chapter as well as in later chapters of the textbook.

Description

Figure 3.12 Output from Executing Sequence Operations

Introduction to Python Programming for Business and Social Science Applications

Подняться наверх