美文网首页
第4章 当索引行不通时

第4章 当索引行不通时

作者: 有斧头的IceBear | 来源:发表于2022-03-27 00:16 被阅读0次

代码清单4-1 字典示例

# A simple database

# A dictionary with person names as keys. Each person is represented as
# another dictionary with the keys 'phone' and 'addr' referring to their phone
# number and address, respectively.
people = {

    'Alice': {
        'phone': '2341',
        'addr': 'Foo drive 23'
    },

    'Beth': {
        'phone': '9102',
        'addr': 'Bar street 42'
    },

    'Cecil': {
        'phone': '3158',
        'addr': 'Baz avenue 90'
    }

}

# Descriptive labels for the phone number and address. These will be used
# when printing the output.
labels = {
    'phone': 'phone number',
    'addr': 'address'
}

name = input('Name: ')

# Are we looking for a phone number or an address?
request = input('Phone number (p) or address (a)? ')

# Use the correct key:
if request == 'p': key = 'phone'
if request == 'a': key = 'addr'

# Only try to print information if the name is a valid key in
# our dictionary:
if name in people: print("{}'s {} is {}.".format(name, labels[key], people[name][key]))

运行结果如下:

Name: Beth
Phone number (p) or address (a)? p
Beth's phone number is 9102.

代码清单4-2 字典方法示例

# A simple database using get()

# Insert database (people) from Listing 4-1 here.
people = {

    'Alice': {
        'phone': '2341',
        'addr': 'Foo drive 23'
    },

    'Beth': {
        'phone': '9102',
        'addr': 'Bar street 42'
    },

    'Cecil': {
        'phone': '3158',
        'addr': 'Baz avenue 90'
    }

}

labels = {
    'phone': 'phone number',
    'addr': 'address'
}

name = input('Name: ')

# Are we looking for a phone number or an address?
request = input('Phone number (p) or address (a)? ')

# Use the correct key:
key = request # In case the request is neither 'p' nor 'a'
if request == 'p': key = 'phone'
if request == 'a': key = 'addr'

# Use get to provide default values:
person = people.get(name, {})
label = labels.get(key, key)
result = person.get(key, 'not available')

print("{}'s {} is {}.".format(name, label, result))

运行结果如下:

Name: Gumby
Phone number (p) or address (a)? batting average
Gumby's batting average is not available.

相关文章

  • 第4章 当索引行不通时

    通过名称来访问其各个值的数据结构。这种数据结构称为映射(mapping)。字典是Python中唯一的内置映射类型,...

  • 第4章 当索引行不通时

    代码清单4-1 字典示例 运行结果如下: 代码清单4-2 字典方法示例 运行结果如下:

  • elasticsearch rollover index

    rollover作用 当ES索引过大时,rollover,满足条件后,新建索引,将索引别名转向新索引。 rollo...

  • Python数据分析学习

    当数据索引不是整数时: 利用标签切片运算与普通的Python切片运算不同,末端是包含的 当数据索引不是整数时:

  • Mysql

    2020-01-21 联合索引本质 当创建(a,b,c)联合索引时,相当于创建了(a)单列索引,(a,b)联合索引...

  • 创建高性能索引

    索引基础 索引的类型 B-Tree索引 当人们谈论索引时,如果没有特别指明类型,那多半说的是B-Tree索引。存储...

  • Mysql索引查询失效的情况

    MySQL索引失效的几种情况 1、like 以%开头,索引无效;当like前缀没有%,后缀有%时,索引有效。 2、...

  • MySQL索引原理

    一、索引的类型 1.1 B-Tree索引还是B+Tree索引? 当人们谈论索引时,如果没有特别指明类型,那多半说的...

  • mysql唯一索引 覆盖索引

    当mysql唯一索引是组合索引时,如果查询条件满足组合索引的覆盖条件,同样将是覆盖索引。 测试:新建表t: 添加唯...

  • Oracle为什么要重建索引?

    当我们创建索引时,oracle会为索引创建索引树,表和索引树通过rowid(伪列)来定位数据.当表里的数据发生更新...

网友评论

      本文标题:第4章 当索引行不通时

      本文链接:https://www.haomeiwen.com/subject/tieejrtx.html