мета-данные страницы
  •  
Загрузка не удалась. Возможно, проблемы с правами доступа?

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слеваПредыдущая версия
Следующая версия
Предыдущая версия
python:week4 [09/02/2017 11:31] – [Задача 6] ybezrukovpython:week4 [16/10/2018 12:18] (текущий) ybezrukov
Строка 5: Строка 5:
 До значения всегда можно добраться обратившись к нему по ключу, например: До значения всегда можно добраться обратившись к нему по ключу, например:
  
-{{url>http://novt.net/iframe-embed.html#code=d+%3D+dict(%29%0A%0Ad%5B%22Frodo%22%5D+%3D+%22has+the+Ring%22%0Ad%5B%22Sauron%22%5D+%3D+%22is+looking+for+the+Ring%22%0Ad%5B%22Gollum%22%5D+%3D+%22lost+the+Ring%22%0Ad%5B%22Gandalf%22%5D+%3D+%22does+not+want+the+Ring%22%0A%0Aprint(d%5B%22Sauron%22%5D%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}+{{url>https://pt.nsunc.com/iframe-embed.html#code=d+%3D+dict(%29%0A%0Ad%5B%22Frodo%22%5D+%3D+%22has+the+Ring%22%0Ad%5B%22Sauron%22%5D+%3D+%22is+looking+for+the+Ring%22%0Ad%5B%22Gollum%22%5D+%3D+%22lost+the+Ring%22%0Ad%5B%22Gandalf%22%5D+%3D+%22does+not+want+the+Ring%22%0A%0Aprint(d%5B%22Sauron%22%5D%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}
  
 В качестве ключа может выступать любой тип данных Питона: строка, число, список и т.д. В качестве ключа может выступать любой тип данных Питона: строка, число, список и т.д.
Строка 11: Строка 11:
 Словарь можно создать и другим способом, прямо в момент объявления переменной: Словарь можно создать и другим способом, прямо в момент объявления переменной:
  
-{{url>http://novt.net/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A%0Aprint(d%5B%22Sauron%22%5D%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}+{{url>https://pt.nsunc.com/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A%0Aprint(d%5B%22Sauron%22%5D%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}
  
 Так как словарь это еще и коллекция (вместе с списками и другими представителями этого класса), в Питоне существует удобный способ перебора всех элементов: Так как словарь это еще и коллекция (вместе с списками и другими представителями этого класса), в Питоне существует удобный способ перебора всех элементов:
  
-{{url>http://novt.net/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A++++%0Afor+key+in+d%3A%0A++++print(key+%2B+%22+%22+%2B+d%5Bkey%5D%29%0A++++%0A%23+or+even+easier%0A%0Afor+name,+state+in+d.items(%29%3A%0A++++print(name+%2B+%22+%22+%2B+state%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}+{{url>https://pt.nsunc.com/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A++++%0Afor+key+in+d%3A%0A++++print(key+%2B+%22+%22+%2B+d%5Bkey%5D%29%0A++++%0A%23+or+even+easier%0A%0Afor+name,+state+in+d.items(%29%3A%0A++++print(name+%2B+%22+%22+%2B+state%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}
  
 Лишнюю запись из словаря удалить можно с помощью функции del() Лишнюю запись из словаря удалить можно с помощью функции del()
  
-{{url>http://novt.net/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A++++%0Adel(d%5B%22Gandalf%22%5D%29++++%0A%0Afor+name,+state+in+d.items(%29%3A%0A++++print(name+%2B+%22+%22+%2B+state%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}+{{url>https://pt.nsunc.com/iframe-embed.html#code=d+%3D+%7B++++%0A++++%22Frodo%22+%3A+%22has+the+Ring%22,%0A++++%22Sauron%22+%3A+%22is+looking+for+the+Ring%22,%0A++++%22Gollum%22+%3A+%22lost+the+Ring%22,%0A++++%22Gandalf%22+%3A+%22does+not+want+the+Ring%22%0A++++%7D%0A++++%0Adel(d%5B%22Gandalf%22%5D%29++++%0A%0Afor+name,+state+in+d.items(%29%3A%0A++++print(name+%2B+%22+%22+%2B+state%29%0A&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=0&codeDivWidth=350&codeDivHeight=400}}
  
 ====== Задача 6 ====== ====== Задача 6 ======