笨方法学python39

代码如下:

# 字典,可爱的字典
'''
字典:类似列表的一种存储数据的返方法,但获取字典的数据,你用的不是数值索引,而是任何你想用的东西
将字典当作数据库存储和组织数据
'''

#对比字典和列表的功能
#列表
'''
使用数值作为列表的索引,可以用过数值找到列表中的元素
你只能使用数值来获取列表的项
'''
things =['a','b','c','d']
print(things[1])
#位置第1个对应的内容
things[1]='z'
print(things[1])
#使位置第1个对应的内容定义为z
print(things)

#字典
'''
你可以用过任何东西(不只是数值)找到元素,
不管字典里的内容类型使什么,都能相关联,
'''
stuff={'name':'Zed','age':39,'height':6*12+2}
print (stuff['name'])
print(stuff['age'])
print(stuff['height'])
stuff['city']="SF"
print(stuff['city'])

#两个数值
stuff[1]='WoW'
stuff[2]='Neato'
print(stuff[2])
print(stuff[2])

#del的使用
del stuff['city']
del stuff[1]
del stuff[2]
print(stuff)

结果输出:

b
z
['a', 'z', 'c', 'd']
Zed
39
74
SF
Neato
Neato
{'name': 'Zed', 'age': 39, 'height': 74}

进程已结束,退出代码0

代码如下:

#create a mapping of state to above abbreviation
states={
    'Oregon':'OR',
    'Florida':'FL' ,
    'California':'CA',
    'New York':'NY',
    'Michigan':'MI'
}
print(states)
#{}花括号是字典的标志

#create a basic set of states and some cities in them
cities={
    'CA':'San Francisco',
    'MI':'Detroit',
    'FL':'Jacksonville',
}
print(cities)

#add some more cities
cities['NY']='New York'
cities['OR']='Portland'
print(cities)
#print out some cities
print('-'*10)
print("NY State has:",cities['NY'])
print("OR State has:",cities['OR'])
#do it by using the state then cities dict
print('-'*10)
print("Michigan has:",cities[states['Michigan']])
print("Florida has:",cities[states['Florida']])
#print every state abbreviation
print('-'*10)
for state,abbrev in list(states.items()):
    print(f"{state} is abbreviated {abbrev}")
#print every cities in state
print('-'*10)
for abbrev,city in list (cities.items()):
    print(f"{abbrev} has the city {city}")
#now do both at the same time
print('-'*10)
for state,abbrev in list(states.items()):
    print(f"{state} state is abbreviated {abbrev}")
    print(f"and has city {cities[abbrev]}")
print('-'*10)
# safety get a abbreviation by state that might not be there
state=states.get('Texas')

if not states:
    print("Sorry,no texas.")
#get a city with a default value
city=cities.get('TX','Does Not Exist')
print(f"The city for the state 'TX' is {city}")
'''
列表和字典的不同:
列表是有序排列,字典是将{键}对应{值}

字典能用在哪里:
通过某个值查看另一个值,也可以把字典叫做“查找表”

列表能用在哪里:
专供有序排列的数据,只要知道索引就能查到对应的值了

排序的词典:
collections.OrderedDict
python中字典 Dict 是利用hash存储,各元素之间没有顺序 ;而在 OrderedDict 中是按照 添加顺序存储 的 有序 字典 ;
'''

结果输出:

{'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI'}
{'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
{'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville', 'NY': 'New York', 'OR': 'Portland'}
----------
NY State has: New York
OR State has: Portland
----------
Michigan has: Detroit
Florida has: Jacksonville
----------
Oregon is abbreviated OR
Florida is abbreviated FL
California is abbreviated CA
New York is abbreviated NY
Michigan is abbreviated MI
----------
CA has the city San Francisco
MI has the city Detroit
FL has the city Jacksonville
NY has the city New York
OR has the city Portland
----------
Oregon state is abbreviated OR
and has city Portland
Florida state is abbreviated FL
and has city Jacksonville
California state is abbreviated CA
and has city San Francisco
New York state is abbreviated NY
and has city New York
Michigan state is abbreviated MI
and has city Detroit
----------
The city for the state 'TX' is Does Not Exist

进程已结束,退出代码0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值