swift4.0 数组Array,集合Set和字典Dictionary

swift4.0 数组Array,集合Set和字典Dictionary,第1张

#输出的顺序却不是我们预想的那样初始化的顺序,查询相关文献得知,Python保证遍##历字典所有元素,但不保证遍历的顺序,假如我们期望按预先的顺序进行遍历的话,#我目前找到一个办法,就是通过两个List分别存储Key和Value,然后通过zip合并为Dic#tionary,再遍历:

# 通过zip方法合并两个List为Dictionary

# 遍历会按原先的顺序

keys = ["b", "a", "c", "e", "d"]

values = ["2", "1", "3", "5", "4"]

 

for key,value in zip(keys, values):

    print(key,value)

 

# -- OUTPUT --

# b 2

# a 1

# c 3

# e 5

# d 4

package test;import javautil;/ 取得Map中的key的集合用的方法是keySet,返回的是Set, 取得Map中的value的集合用的方法是values,返回的是Collect。 值得一提的是,本题中是HashMap,如果改为TreeMap,keySet()方法返回的便会按着字母

字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一内建的映射类型,基本的 *** 作包括如下:

(1)len():返回字典中键—值对的数量;

(2)d[k]:返回关键字对于的值;

(3)d[k]=v:将值关联到键值k上;

(4)del d[k]:删除键值为k的项;

(5)key in d:键值key是否在d中,是返回True,否则返回False。

(6)clear函数:清除字典中的所有项

(7)copy函数:返回一个具有相同键值的新字典;deepcopy()函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题

(8)fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

(9)get函数:访问字典成员

(10)has_key函数:检查字典中是否含有给出的键

(11)items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

(12)keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

(13)pop函数:删除字典中对应的键

(14)popitem函数:移出字典中的项

(15)setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

(16)update函数:用一个字典更新另外一个字典

(17) values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

一、字典的创建

11 直接创建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

运算结果

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

>>>

12 通过dict创建字典

# __ coding:utf-8 __

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的内容:'

printitems

printu'利用dict创建字典,输出字典内容:'

d=dict(items)

printd

printu'查询字典中的内容:'

printd['one']

printd['three']

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

items中的内容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict创建字典,输出字典内容:

{'four':4,'three':3,'two':2,'one':1}

查询字典中的内容:

>>>

或者通过关键字创建字典

# __ coding:utf-8 __

d=dict(one=1,two=2,three=3)

printu'输出字典内容:'

printd

printu'查询字典中的内容:'

printd['one']

printd['three']

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

输出字典内容:

{'three':3,'two':2,'one':1}

查询字典中的内容:

>>>

二、字典的格式化字符串

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s"%d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'four':4,'three':3,'two':2,'one':1}

threeis3

>>>

三、字典方法

31 clear函数:清除字典中的所有项

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3,'four':4}

printd

dclear()

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'four':4,'three':3,'two':2,'one':1}

{}

>>>

请看下面两个例子

311

# __ coding:utf-8 __

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

>>>

312

# __ coding:utf-8 __

d={}

dd=d

d['one']=1

d['two']=2

printdd

dclear()

printd

printdd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'two':2,'one':1}

{}

{}

>>>

312与311唯一不同的是在对字典d的清空处理上,311将d关联到一个新的空字典上,这种方式对字典dd是没有影响的,所以在字典d被置空后,字典dd里面的值仍旧没有变化。但是在312中clear方法清空字典d中的内容,clear是一个原地 *** 作的方法,使得d中的内容全部被置空,这样dd所指向的空间也被置空。

32 copy函数:返回一个具有相同键值的新字典

# __ coding:utf-8 __

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X复制到Y:'

y=xcopy()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,观察输出:'

printy

printx

printu'删除Y中的值,观察输出'

y['test']remove('c')

printy

printx

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X复制到Y:

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,观察输出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

删除Y中的值,观察输出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

>>>

注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题。

# __ coding:utf-8 __

fromcopyimportdeepcopy

x={}

x['test']=['a','b','c','d']

y=xcopy()

z=deepcopy(x)

printu'输出:'

printy

printz

printu'修改后输出:'

x['test']append('e')

printy

printz

运算输出:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

输出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后输出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

>>>

33 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

# __ coding:utf-8 __

d=dictfromkeys(['one','two','three'])

printd

运算输出:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':None,'two':None,'one':None}

>>>

或者指定默认的对应值

# __ coding:utf-8 __

d=dictfromkeys(['one','two','three'],'unknow')

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':'unknow','two':'unknow','one':'unknow'}

>>>

34 get函数:访问字典成员

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

printdget('one')

printdget('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

1

None

>>>

注:get函数可以访问字典中不存在的键,当该键不存在是返回None

35 has_key函数:检查字典中是否含有给出的键

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

printdhas_key('one')

printdhas_key('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

True

False

>>>

36 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

list=ditems()

forkey,valueinlist:

  printkey,':',value

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

>>>

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

it=diteritems()

fork,vinit:

  print"d[%s]="%k,v

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

>>>

37 keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=dkeys()

printlist

printu'\niterkeys方法:'

it=diterkeys()

forxinit:

  printx

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

>>>

38 pop函数:删除字典中对应的键

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

dpop('one')

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

>>>

39 popitem函数:移出字典中的项

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

dpopitem()

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

>>>

310 setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

# __ coding:utf-8 __

d={'one':1,'two':2,'three':3}

printd

printdsetdefault('one',1)

printdsetdefault('four',4)

printd

运算结果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

>>>

311 update函数:用一个字典更新另外一个字典

# __ coding:utf-8 __

d={

  'one':123,

  'two':2,

  'three':3

  }

printd

x={'one':1}

dupdate(x)

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

>>>

312 values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

# __ coding:utf-8 __

d={

  'one':123,

  'two':2,

  'three':3,

  'test':2

  }

printdvalues()

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\testpy=======

[2,3,2,123]

>>>

Python里面的字典能不能通过值value来找到对应的键key

可以的。例:

d = {'1':'one','2':'two','3':'three'}

last = dvalues()[2]

second = d['2']

print(last)

print(second)

输出:

two

two

明白了吗?还没有的话可以追问

以上就是关于swift4.0 数组Array,集合Set和字典Dictionary全部的内容,包括:swift4.0 数组Array,集合Set和字典Dictionary、js从字典里取到一个字,用什么算法最快、Python中如何以dict的key排序输出等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9638185.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存