���� Python �̳� | ��9�� ���ݽṹ | |
---|---|---|
��һҳ | ���� | ��һҳ |
�б���Ԫ����ַ����������У�����������ʲô������Ϊʲô����ر��أ����е�������Ҫ�ص�����������������Ƭ�����������������������ǿ��Դ�������ץȡһ���ض���Ŀ����Ƭ�������������ܹ���ȡ���е�һ����Ƭ����һ�������С�
#!/usr/bin/python
# Filename: seq.py
shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
# Indexing or 'Subscription' operation
print
'Item 0 is'
, shoplist[
0
]
print
'Item 1 is'
, shoplist[
1
]
print
'Item 2 is'
, shoplist[
2
]
print
'Item 3 is'
, shoplist[
3
]
print
'Item -1 is'
, shoplist[
-1
]
print
'Item -2 is'
, shoplist[
-2
]
# Slicing on a list
print
'Item 1 to 3 is'
, shoplist[
1
:
3
]
print
'Item 2 to end is'
, shoplist[
2
:
]
print
'Item 1 to -1 is'
, shoplist[
1
:
-1
]
print
'Item start to end is'
, shoplist[:]
# Slicing on a string
name =
'swaroop'
print
'characters 1 to 3 is'
, name[
1
:
3
]
print
'characters 2 to end is'
, name[
2
:]
print
'characters 1 to -1 is'
, name[
1
:
-1
]
print
'characters start to end is'
, name[:]
��Դ�ļ���code/seq.py��
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
���ȣ�������ѧϰ���ʹ��������ȡ�������еĵ�����Ŀ����Ҳ���������±������ÿ�����÷������е�һ������ָ��һ�����е�ʱ��Python��Ϊ��ץȡ�����ж�Ӧλ�õ���Ŀ����ס��Python��0��ʼ��������ˣ�shoplist[0]
ץȡ��һ����Ŀ��shoplist[3]
ץȡshoplist
�����еĵ��ĸ�Ԫ�ء�
����ͬ�������Ǹ�����������������£�λ���Ǵ�����β��ʼ����ġ���ˣ�shoplist[-1]
��ʾ���е����һ��Ԫ�ض�shoplist[-2]
ץȡ���еĵ����ڶ�����Ŀ��
��Ƭ�����������������һ�������ţ�����������һ�Կ�ѡ�����֣�����ð�ŷָע��������ʹ�õ�����������ʮ�����ơ���ס���ǿ�ѡ�ģ���ð���DZ���ġ�
��Ƭ�������еĵ�һ������ð��֮ǰ����ʾ��Ƭ��ʼ��λ�ã��ڶ�������ð��֮��ʾ��Ƭ����������������ָ����һ������Python�ʹ�������ʼ�����û��ָ���ڶ���������Python��ֹͣ������β��ע�⣬���ص����дӿ�ʼλ�� ��ʼ ���պ��� ���� λ��֮ǰ����������ʼλ���ǰ�����������Ƭ�еģ�������λ�ñ��ų�����Ƭ�⡣
������shoplist[1:3]
���ش�λ��1��ʼ������λ��2������ֹͣ��λ��3��һ��������Ƭ����˷���һ������������Ŀ����Ƭ�����Ƶأ�shoplist[:]
�����������еĿ�����
������ø�������Ƭ���������ڴ�����β��ʼ�����λ�á����磬shoplist[:-1]
�᷵�س������һ����Ŀ�����������Ŀ��������Ƭ��
ʹ��Python�����������س��Բ�ͬ��Ƭָ����ϣ�������ʾ�������ܹ����Ͽ�����������е�����֮���������������ͬ�ķ�������Ԫ�顢�б����ַ�����
��һҳ | ��һ�� | ��һҳ |
---|---|---|
�ֵ� | ��ҳ | �ο� |