Списки

Останнє оновлення 2025-12-29 | Редагувати цю сторінку

Огляд

Питання

  • How can I store multiple values?

Цілі

  • Explain why programs need collections of values.
  • Write programs that create flat lists, index them, slice them, and modify them through assignment and method calls.

A list stores many values in a single structure.


  • Виконання обчислень із сотнею змінних під назвою pressure_001, pressure_002 тощо, було б принаймні так само повільно, як робити їх вручну.
  • Використовуйте список для зберігання багатьох значень разом.
    • Список позначається квадратними дужками [...].
    • Значення розділяються комами ,.
  • Використовуйте len, щоб дізнатися, скільки значень у списку.

PYTHON

pressures = [0.273, 0.275, 0.277, 0.275, 0.276]
print('pressures:', pressures)
print('length:', len(pressures))

ВИХІД

pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5

Щоб отримати елемент списку, використовуйте його індекс.


  • Just like strings.

PYTHON

print('zeroth item of pressures:', pressures[0])
print('fourth item of pressures:', pressures[4])

ВИХІД

zeroth item of pressures: 0.273
fourth item of pressures: 0.276

Значення елементів списків можна замінити шляхом присвоєння.


  • Використовуйте індексний вираз ліворуч від знаку присвоєння, щоб замінити значення.

PYTHON

pressures[0] = 0.265 
print('нові значення pressures:', pressures)

ВИХІД

Нові значення pressures: [0.265, 0.275, 0.277, 0.275, 0.276]

Додавання елементів до списку подовжує його.


  • Щоб додати елементи в кінець списку, використовуйте list_name.append.

PYTHON

primes = [2, 3, 5]
print('Початкові значення primes:', primes)
primes.append(7)
print('Список primes змінився:', primes)

ВИХІД

Початкові значення primes: [2, 3, 5]
Список primes змінився: [2, 3, 5, 7]
  • append є методом списків.
    • Методи подібні функціям, але вони прив’язані до певних об’єктів.
  • Для виклику методів використовується синтаксис object_name.method_name .
    • Deliberately resembles the way we refer to things in a library.
  • По ходу роботи ми познайомимося з іншими методами, визначеними для списків.
    • Use help(list) for a preview.
  • extend - це метод, схожий на append, але він дозволяє об’єднувати два списки. Наприклад:

PYTHON

teen_primes = [11, 13, 17, 19]
middle_aged_primes = [37, 41, 43, 47]
print('primes is currently:', primes)
primes.extend(teen_primes)
print('primes has now become:', primes)
primes.append(middle_aged_primes)
print('primes has finally become:', primes)

ВИХІД

primes is currently: [2, 3, 5, 7]
primes has now become: [2, 3, 5, 7, 11, 13, 17, 19]
primes has finally become: [2, 3, 5, 7, 11, 13, 17, 19, [37, 41, 43, 47]]

Note that while extend maintains the “flat” structure of the list, appending a list to a list means the last element in primes will itself be a list, not an integer. Списки можуть містити значення будь-якого типу; отже, можливі списки списків.

Use del to remove items from a list entirely.


  • We use del list_name[index] to remove an element from a list (in the example, 9 is not a prime number) and thus shorten it.
  • del is not a function or a method, but a statement in the language.

PYTHON

primes = [2, 3, 5, 7, 9]
print('primes перед видаленням останнього елементу:', primes)
del primes[4]
print('primes після видалення останнього елементу:', primes)

ВИХІД

primes перед видаленням останнього елементу: [2, 3, 5, 7, 9]
primes після видалення останнього елементу: [2, 3, 5, 7]

Порожній список не містить жодних значень.


  • Use [] on its own to represent a list that doesn’t contain any values.
    • Порожній список - це “нуль списків.”
  • Helpful as a starting point for collecting values (which we will see in the next episode).

Списки можуть містити значення різних типів.


  • Один список може містити числа, рядки та будь-що інше.

PYTHON

goals = [1, 'Створити списки.', 2, 'Вилучити елементи із списків.', 3, 'Змінити списки.']

Рядки символів можна індексувати як списки.


  • Отримати окремі символи з рядка символів можна за допомогою індексів у квадратних дужках.

PYTHON

element = 'carbon'
print('нульовий символ:', element[0])
print('третій символ:', element[3])

ВИХІД

zeroth character: c
third character: b

Рядки символів незмінні.


  • Неможливо змінити символи в рядку після його створення.
    • Immutable: can’t be changed after creation.
    • In contrast, lists are mutable: they can be modified in place.
  • Python considers the string to be a single value with parts, not a collection of values.

PYTHON

element[0] = 'C'

ПОМИЛКА

TypeError: 'str' object does not support item assignment
  • Списки та рядки символів є колекціями.

Indexing beyond the end of the collection is an error.


  • Python reports an IndexError if we attempt to access a value that doesn’t exist.
    • This is a kind of runtime error.
    • Cannot be detected as the code is parsed because the index might be calculated based on data.

PYTHON

print('99th element of element is:', element[99])

ВИХІД

IndexError: string index out of range
Вправа

Fill in the Blanks

Fill in the blanks so that the program below produces the output shown.

PYTHON

values = ____
values.____(1)
values.____(3)
values.____(5)
print('first time:', values)
values = values[____]
print('second time:', values)

ВИХІД

first time: [1, 3, 5]
second time: [3, 5]

PYTHON

values = []
values.append(1)
values.append(3)
values.append(5)
print('first time:', values)
values = values[1:]
print('second time:', values)
Вправа

How Large is a Slice?

If start and stop are both non-negative integers, how long is the list values[start:stop]?

The list values[start:stop] has up to stop - start elements. For example, values[1:4] has the 3 elements values[1], values[2], and values[3]. Why ‘up to’? As we saw in episode 2, if stop is greater than the total length of the list values, we will still get a list back but it will be shorter than expected.

Вправа

Від рядків до списків і назад.

Given this:

PYTHON

print('string to list:', list('tin'))
print('list to string:', ''.join(['g', 'o', 'l', 'd']))

ВИХІД

string to list: ['t', 'i', 'n']
list to string: gold
  1. What does list('some string') do?
  2. What does '-'.join(['x', 'y', 'z']) generate?
  1. list('some string') converts a string into a list containing all of its characters.

  2. join returns a string that is the concatenation of each string element in the list and adds the separator between each element in the list. This results in x-y-z. The separator between the elements is the string that provides this method.

Вправа

Working With the End

Що друкує наступна програма?

PYTHON

element = 'helium'
print(element[-1])
  1. How does Python interpret a negative index?
  2. If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent?
  3. If values is a list, what does del values[-1] do?
  4. How can you display all elements but the last one without changing values? (Hint: you will need to combine slicing and negative indexing.)

The program prints m.

  1. Python interprets a negative index as starting from the end (as opposed to starting from the beginning). The last element is -1.

  2. The last index that can safely be used with a list of N elements is element -N, which represents the first element.

  3. del values[-1] removes the last element from the list.

  4. values[:-1]

Вправа

Перехід по списку

Що друкує наступна програма?

PYTHON

element = 'fluorine'
print(element[::2])
print(element[::-1])
  1. If we write a slice as low:high:stride, what does stride do?
  2. What expression would select all of the even-numbered items from a collection?

The program prints

PYTHON

furn
eniroulf
  1. stride is the step size of the slice.

  2. Зріз 1::2 вибирає всі елементи з парними номерами з колекції: він починається з елементу 1 (який є другим елементом, оскільки індексація починається з 0), продовжується до кінця (оскільки end не задано) і використовує розмір кроку 2 (таким чином обираючи кожний другий елемент).

Вправа

Границі зрізу

Що друкує наступна програма?

PYTHON

element = 'lithium'
print(element[0:20])
print(element[-1:3])

ВИХІД

lithium

The first statement prints the whole string, since the slice goes beyond the total length of the string. The second statement returns an empty string, because the slice goes “out of bounds” of the string.

Вправа

Sort and Sorted

Що друкують ці дві програми? Простими словами поясніть різницю між sorted(letters) and letters.sort().

PYTHON

# Program A
letters = list('gold')
result = sorted(letters)
print('letters is', letters, 'and result is', result)

PYTHON

# Program B
letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)

Program A prints

ВИХІД

letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']

Program B prints

ВИХІД

letters is ['d', 'g', 'l', 'o'] and result is None

sorted(letters) returns a sorted copy of the list letters (the original list letters remains unchanged), while letters.sort() sorts the list letters in-place and does not return anything.

Вправа

Копіювання (чи ні)

Що друкують ці дві програми? Простими словами поясніть різницю між new = old and new = old[:].

PYTHON

# Program A
old = list('gold')
new = old      # simple assignment
new[0] = 'D'
print('new is', new, 'and old is', old)

PYTHON

# Program B
old = list('gold')
new = old[:]   # assigning a slice
new[0] = 'D'
print('new is', new, 'and old is', old)

Program A prints

ВИХІД

new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']

Program B prints

ВИХІД

new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']

new = old makes new a reference to the list old; new and old point towards the same object.

new = old[:] however creates a new list object new containing all elements from the list old; new and old are different objects.

Ключові моменти
  • A list stores many values in a single structure.
  • Щоб отримати елемент списку, використовуйте його індекс.
  • Значення елементів списків можна замінити шляхом присвоєння.
  • Додавання елементів до списку подовжує його.
  • Щоб повністю видалити елементи зі списку, використовуйте del.
  • Порожній список не містить жодних значень.
  • Списки можуть містити значення різних типів.
  • Рядки символів можна індексувати як списки.
  • Рядки символів незмінні.
  • Indexing beyond the end of the collection is an error.