���� Python �̳� | ��9�� ���ݽṹ | |
---|---|---|
��һҳ | �ֵ� | ��һҳ |
�ֵ���������ͨ����ϵ�����ֲ��ҵ�ַ����ϵ����ϸ����ĵ�ַ�����������ǰ��������֣���ֵ����ϸ�������ϵ��һ��ע�⣬��������Ψһ�ģ����������������ǡ��ͬ���Ļ��������ҵ���ȷ����Ϣ��
ע�⣬��ֻ��ʹ�ò��ɱ�Ķ������ַ���������Ϊ�ֵ�ļ�����������Բ��ɱ��ɱ�Ķ�����Ϊ�ֵ��ֵ������˵�����ǣ���Ӧ��ֻʹ�üĶ�����Ϊ����
��ֵ�����ֵ����������ķ�ʽ��ǣ�d = {key1 : value1, key2 : value2 }
��ע�����ǵļ�/ֵ����ð�ŷָ���������ö��ŷָ������Щ�������ڻ������С�
��ס�ֵ��еļ�/ֵ����û��˳��ġ��������Ҫһ���ض���˳����ô��Ӧ����ʹ��ǰ�Լ�����������
�ֵ���dict
���ʵ��/����
#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
ab = {
'Swaroop'
:
'[email protected]'
,
'Larry'
:
'[email protected]'
,
'Matsumoto'
:
'[email protected]'
,
'Spammer'
:
'[email protected]'
}
print
"Swaroop's address is %s"
% ab[
'Swaroop'
]
# Adding a key/value pair
ab[
'Guido'
] =
'[email protected]'
# Deleting a key/value pair
del
ab[
'Spammer'
]
print
'\nThere are %d contacts in the address-book\n'
%
len
(ab)
for
name, address
in
ab.items():
print
'Contact %s at %s'
% (name, address)
if
'Guido'
in
ab:
# OR ab.has_key('Guido')
print
"\nGuido's address is %s"
% ab[
'Guido'
]
��Դ�ļ���code/using_dict.py��
$ python using_dict.py
Swaroop's address is [email protected]
There are 4 contacts in the address-book
Contact Swaroop at [email protected]
Contact Matsumoto at [email protected]
Contact Larry at [email protected]
Contact Guido at [email protected]
Guido's address is [email protected]
����ʹ���Ѿ����ܹ��ı�Ǵ������ֵ�ab
��Ȼ������ʹ�����б���Ԫ���½����Ѿ����۹���������������ָ�������Ӷ�ʹ�ü�/ֵ�ԡ����ǿ��Կ����ֵ���ͬ��ʮ�ּ�
���ǿ���ʹ��������������Ѱַһ������Ϊ����ֵ��������������һ���µļ�/ֵ�ԣ���������������������Ƕ�Guido������һ����
���ǿ���ʹ�����ǵ������ѡ���del
�����ɾ����/ֵ�ԡ�����ֻ��Ҫָ���ֵ��������������ָ��Ҫɾ���ļ���Ȼ������Ǵ��ݸ�del
���Ϳ����ˡ�ִ�����������ʱ����������֪���Ǹ�������Ӧ��ֵ��
������������ʹ���ֵ��items
��������ʹ���ֵ��е�ÿ����/ֵ�ԡ���᷵��һ��Ԫ����б�������ÿ��Ԫ�鶼����һ����Ŀ���������Ӧ��ֵ������ץȡ����ԣ�Ȼ��ֱ�for..in
ѭ���еı���name
��address
Ȼ����for�����д�ӡ��Щֵ��
���ǿ���ʹ��in
������������һ����/ֵ���Ƿ���ڣ�����ʹ��dict
���has_key
�����������ʹ��help(dict)
���鿴dict
��������������
�ؼ��ֲ������ֵ䡣�����һ���Ƕȿ������ں�����ʹ�õĹؼ��ֲ����Ļ������Ѿ�ʹ�����ֵ��ˣ�ֻ����һ�¡������ں�������IJ����б���ʹ�õļ�/ֵ�ԡ������ں�����ʹ�ñ�����ʱ����ֻ������ʹ��һ���ֵ�ļ������ڱ�������Ƶ������б����� ���ű� ����
��һҳ | ��һ�� | ��һҳ |
---|---|---|
Ԫ�� | ��ҳ | ���� |