1
2
3
4
5
6
7
8
9
10
11
|
>>> xpleaf.
xpleaf.clear(
xpleaf.copy(
xpleaf.
get
(
xpleaf.has_key(
xpleaf.items(
xpleaf.keys(
xpleaf.pop(
xpleaf.popitem(
xpleaf.setdefault(
xpleaf.update(
|
1
2
3
4
5
6
7
8
|
>>> xpleaf = {
...
'name'
:
'xpleaf'
,
...
'occupation'
:
'student'
,
...
'hobby'
:
'computer'
,
...
'dream'
:
'excellent hacker'
... }
>>> xpleaf
{
'hobby'
:
'computer'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
|
1
2
|
>>> xpleaf[
'hobby'
]
'computer'
|
1
2
3
|
>>> xpleaf[
'hobby'
] =
'IT'
>>> xpleaf
{
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
|
1
2
3
|
>>> xpleaf[
'girlfriend'
] =
'none'
>>> xpleaf
{
'girlfriend'
:
'none'
,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
|
1
2
3
4
|
>>> xpleaf.has_key(
'dream'
)
True
>>> xpleaf.has_key(
'wife'
)
False
|
1
2
|
>>> xpleaf.items()
[(
'girlfriend'
,
'none'
), (
'hobby'
,
'IT'
), (
'dream'
,
'excellent hacker'
), (
'name'
,
'xpleaf'
), (
'occupation'
,
'student'
)]
|
1
2
3
4
5
6
7
8
|
>>>
for
key,value
in
xpleaf.items():
... print key,value
...
girlfriend none
hobby IT
dream excellent hacker
name xpleaf
occupation student
|
1
2
3
4
5
6
7
8
|
>>>
for
key
in
xpleaf:
... print key,xpleaf[key]
...
girlfriend none
hobby IT
dream excellent hacker
name xpleaf
occupation student
|
1
2
3
4
5
6
|
>>> xpleaf
{
'girlfriend'
:
'none'
,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
>>> xpleaf.
get
(
'dream'
)
'excellent hacker'
>>> xpleaf.
get
(
'wife'
) ===>如果没有该key值则不会有输出
>>>
|
1
2
|
>>> xpleaf.keys()
[
'girlfriend'
,
'hobby'
,
'dream'
,
'name'
,
'occupation'
]
|
1
2
3
4
5
6
|
>>> xpleaf
{
'girlfriend'
:
'none'
,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
>>> xpleaf.pop(
'girlfriend'
)
'none'
>>> xpleaf
{
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
2
,
'e'
:
5
,
'd'
:
4
,
6
:
'f'
}
>>> a.popitem()
(
'a'
,
1
)
>>> a.popitem()
(
'c'
,
3
)
>>> a.popitem()
(
'b'
,
2
)
>>> a.popitem()
(
'e'
,
5
)
>>> a.popitem()
(
'd'
,
4
)
>>> a.popitem()
(
6
,
'f'
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf
{
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
>>> xpleaf.setdefault(
'hobby'
,
'computer'
) ===>
'hobby'
键值对已经存在,不会添加
'IT'
>>> xpleaf
{
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'name'
:
'xpleaf'
,
'occupation'
:
'student'
}
>>> xpleaf.setdefault(
'weight'
,
'55kg'
) ===>
'weight'
键值对不存在,会进行添加
'55kg'
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf.setdefault(
'wife'
) ===>添加没有的键值对,
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
: None,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
|
1
2
3
4
5
6
7
8
9
|
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
2
}
>>> b
{
'e'
:
4
,
'g'
:
6
,
'f'
:
5
}
>>> a.update(b)
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
2
,
'e'
:
4
,
'g'
:
6
,
'f'
:
5
}
>>> b
{
'e'
:
4
,
'g'
:
6
,
'f'
:
5
}
|
1
2
3
4
5
6
7
|
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
2
,
'e'
:
4
,
'g'
:
6
,
'f'
:
5
}
>>> c
{
'b'
:
'cover2'
,
'g'
:
'cover1'
}
>>> a.update(c)
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
'cover2'
,
'e'
:
4
,
'g'
:
'cover1'
,
'f'
:
5
}
|
1
2
3
4
|
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
: None,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf.values()
[
'xpleaf'
,
'55kg'
, None,
'IT'
,
'excellent hacker'
,
'student'
]
|
1
2
3
4
5
|
>>> a
{
'a'
:
1
,
'c'
:
3
,
'b'
:
'cover2'
,
'e'
:
4
,
'g'
:
'cover1'
,
'f'
:
5
}
>>> a.clear()
>>> a
{}
|
1
2
3
4
5
|
>>> del a
>>> a
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
NameError: name
'a'
is
not defined
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
: None,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf_copy = xpleaf
>>> xpleaf_copy
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
: None,
'hobby'
:
'IT'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf[
'hobby'
] =
'IT_Field'
>>> xpleaf_copy
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
: None,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf_copy[
'wife'
] =
'None!!!'
>>> xpleaf_copy
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
:
'None!!!'
,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
:
'None!!!'
,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
|
1
2
3
4
5
6
7
8
|
>>> xpleaf_copy2 = xpleaf.copy()
>>> xpleaf_copy2
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
:
'None!!!'
,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf_copy2[
'wife'
] =
'CL'
>>> xpleaf_copy2
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
:
'CL'
,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
>>> xpleaf
{
'name'
:
'xpleaf'
,
'weight'
:
'55kg'
,
'wife'
:
'None!!!'
,
'hobby'
:
'IT_Field'
,
'dream'
:
'excellent hacker'
,
'occupation'
:
'student'
}
|