1006- Python 字典(Dictionary) items()方法

来源:互联网 发布:淘宝直播达人合作 编辑:程序博客网 时间:2024/06/09 01:47

Python 字典的一些操作

目录

[TOC]来生成目录:

  • Python 字典的一些操作
      • 目录
      • 字典基础
    • Markdown及扩展
      • 代码块

字典基础

http://www.runoob.com/python/python-dictionary.html

Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。

d = {key1 : value1, key2 : value2 }
dict = {‘Alice’: ‘2341’, ‘Beth’: ‘9102’, ‘Cecil’: ‘3258’}

Markdown及扩展

代码块

inventory = {    'gold' : 500,    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}# Adding a key 'burlap bag' and assigning a list to itinventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']# Sorting the list found under the key 'pouch'inventory['pouch'].sort() # Your code hereinventory['pocket']=['seashell','strange berry','lint']inventory['backpack'].sort() inventory['backpack'].remove('dagger') inventory['gold'] += 50
 Instructions    01.Add a key to inventory called 'pocket'    02.Set the value of 'pocket' to be a list consisting of the strings 'seashell', 'strange berry', and 'lint'    03..sort() the items in the list stored under the 'backpack' key    04.Then .remove('dagger') from the list of items stored under the 'backpack' key    05.Add 50 to the number stored under the 'gold' key
#A Day at the Supermarketshopping_list = ["banana", "orange", "apple"]stock = {    "banana": 6,    "apple": 0,    "orange": 32,    "pear": 15}prices = {    "banana": 4,    "apple": 2,    "orange": 1.5,    "pear": 3}# Write your code below!def compute_bill(food):    total=0    for item in food:        total =total + prices[item]  # not food[item]    return total
0 0
原创粉丝点击