How to use multiple key values and repeated key values in python dictionary (detailed explanation)

Use a dictionary in Python, the format is as follows:

1

dict={ key1:value1 , key2;value2 ...}

The format used when actually accessing dictionary values ​​is as follows:

1

dict[key]

Multi-key

The multi-key form of the dictionary is as follows:

1

dict={(ke11,key12):value ,(key21,key22):value ...}

The specific form when actually accessing the value in the dictionary is as follows (take the first key as an example):

1

dict[key11,key12]

or:

1

dict[(key11,key12)]

The following is a practical example:

Multivalued

When a key value corresponds to multiple values, the format:

1

dict={key1:(value1,value2 ..), key2:(value1,value2 ...) ...}

The format for accessing the values ​​in the dictionary is as follows:

1

dict[key]

or

1

dict[key][index]

Loop assignment (emphasis)

The syntax structure is shown in the following example

to sum up:

Through the above description, we can know that in the definition of the dictionary, the colon (:) before and after the sign is a whole respectively, that is, use parentheses () to include the parts before and after the colon respectively. When accessing the dictionary value, it is best to put the key Become a whole in parentheses.

Multiple key-value pairs with the same key-value

That is, in the dictionary, there are at least two members whose keys are the same, but the values ​​corresponding to the keys are different. The format is as follows:

1

2

3

dict={ key1: value1

key1: vaklue2,

... }

In this form, the value assigned to the key later becomes the true value of the key.

Use lists and dictionaries as dictionary values

format

1

2

3

dict={ key1:(key11:value,key12:value) ,

key2:(key21:value,key22:value)

}

Access dictionary value format (take the first key as an example):

1

dict[key1][key11]

The actual example is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

#encoding=utf-8 

   

print '中国' 

   

#字典的一键多值 

   

print'方案一 list作为dict的值 值允许重复'   

   

d1={} 

key=1 

value=2 

d1.setdefault(key,[]).append(value) 

value=2 

d1.setdefault(key,[]).append(value) 

   

print d1 

   

#获取值 

print '方案一 获取值' 

print list(d1[key]) 

   

print '方案一 删除值,会留下一个空列表' 

d1[key].remove(value) 

d1[key].remove(value) 

print d1  

   

print '方案一 检查是否还有一个值' 

print d1.get(key,[]) 

   

print '方案二 使用子字典作为dict的值 值不允许重复' 

   

d1={} 

key=1 

keyin=2 

value=11 

d1.setdefault(key,{})[keyin]=value 

keyin=2 

value=22 

d1.setdefault(key,{})[keyin]=value 

keyin=3 

value=33 

d1.setdefault(key,{})[keyin]=value 

   

print d1 

   

print '方案二 获取值' 

print list(d1[key]) 

   

print '方案二 删除值,会留下一个空列表' 

del d1[key][keyin] 

keyin=2 

del d1[key][keyin] 

print d1 

   

print '方案二 检查是否还有一个值' 

print d1.get(key,()) 

   

print '方案三 使用set作为dict的值 值不允许重复' 

d1={} 

key=1 

value=2 

d1.setdefault(key,set()).add(value) 

value=2 

d1.setdefault(key,set()).add(value) 

value=3 

d1.setdefault(key,set()).add(value) 

   

print d1 

   

print '方案三 获取值' 

print list(d1[key]) 

   

print '方案三 删除值,会留下一个空列表' 

d1[key].remove(value) 

value=2 

d1[key].remove(value) 

print d1  

   

print '方案三 检查是否还有一个值' 

print d1.get(key,()) 

 

Print result:

China
Option 1 list as the value of dict allows repeating
{1: [2, 2]}

Get the value
[2, 2] and
delete the value, leaving an empty list
{1: []} to
check if there is another value
[]


方案二 使用子字典作为dict的值 值不允许重复
{1: {2: 22, 3: 33}}
获取值
[2, 3]
删除值,会留下一个空列表
{1: {}}
检查是否还有一个值
{}


方案三 使用set作为dict的值 值不允许重复
{1: set([2, 3])}
获取值
[2, 3]
删除值,会留下一个空列表
{1: set([])}
检查是否还有一个值
set([])

Guess you like

Origin blog.csdn.net/guoguo527/article/details/70756775