鱼C论坛

 找回密码
 立即注册
查看: 1831|回复: 0

[技术交流] python入门:L25字典当索引不好用时

[复制链接]
发表于 2017-7-9 20:54:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
字典组成:
键key ;值value
字典或可称为hash table
字典是python中唯一一个映射类型:
映射类型要区别于之前讲过的序列类型
序列类型是以数组的方式来存储的,通过索引的方式获取相应位置的值,
索引值和对应元素的值是没有任何关系的
Bsp:
>>> brand=['lining','nike','adidas','fishc']
>>> slogan=['everything is possible','just do it','impossible is nothing','let programming change the world']
>>> print('fishc slogan is :',slogan[brand.index('fishc')])
fishc slogan is : let programming change the world
创建和访问字典:
>>> dict2={1:'one',2:'two',3:'three'}
>>> dict2[2]
'two'
创建字典的方式:
dict(mapping) -> new dictionary initialized from a mapping object's
|      (key, value) pairs
Bsp:
        1. dict3= dict((('F',70),('i',105),('s',115),('h',104),('c',99)))
>>> dict3
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'c': 99}
注意:字典只有一个参数,此处用元组将多个映射关系组成一个参数
        2. 通过关键字和值创建字典
        >>> dict4=dict(xiaojiayu='let programming change the world',cangjingkong='let AVconquer all the otaku ')
        >>> dict4
        {'xiaojiayu': 'let programming change the world', 'cangjingkong': 'let AVconquer all the otaku '}
        注意:关键字不能加引号
        3. 直接给字典的键赋值,若有此键则更改它的值,若不存在此键则创建新的键并为其赋值。与linux里的VI(打开一个文件的用法)比较相似
        >>> dict4['cangjingkong']='all the AV works should learn to program'
        >>> dict4
        {'xiaojiayu': 'let programming change the world', 'cangjingkong': 'all the AV works should learn to program'}
        >>> dict4['aidisheng']='genius is composed of 99 percent of hardwork and one percent of talent,but this 1 percent is more important than the 99% hardwork '
        >>> dict4
{'xiaojiayu': 'let programming change the world', 'cangjingkong': 'all the AV works should learn to program', 'aidisheng': 'genius is composed of 99 percent of hardwork and one percent of talent,but this 1 percent is more important than the 99% hardwork '}

评分

参与人数 2鱼币 +2 收起 理由
小甲鱼 + 1
康小泡 + 1

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表