Python-学习3

Python-List

Obtain the value

The value is obtained from ‘0’, which means ‘0’ is the first value.

1
2
3
spam=['cat','bat','rat','elephant']
spam
#out:['cat', 'bat', 'rat', 'elephant']
1
2
spam[0]
#out:'cat'

We can also add it to a string:

1
2
'The '+spam[1]+' ate the '+spam[0]+'.'
#out:'The bat ate the cat.'

The list in the list:

1
2
3
spam=['cat','bat'],[10,20,30,40,50]
spam[1][1]
#out:20

Use negative subscripts to get the value:

1
2
3
spam=['cat','bat','rat','elephant']
spam[-3]
#out:'bat'

Obtain the slice

Use range of subscripts (the range includes the former one, but doesn’t include the later one)

1
2
3
spam=['cat','bat','rat','elephant']
spam[0:-2]
#out:['cat','bat']

without one/both subscript(s) (from the beginning or/and the end)

  • [:] means the whole list
1
2
3
spam=['cat','bat','rat','elephant']
spam[:2]
#out:['cat', 'bat']

Length

Length is the number of value it has.

1
2
3
spam=['cat','dog','moose']
len(spam)
#out: 3

Changing the value by using subscripts

1
2
3
4
5
6
spam=['cat','bat','rat','elephant']
spam[1]='aardvark'
spam[2]=spam[1]
spam[-1]=12345
spam
#out:['cat', 'aardvark', 'aardvark', 12345]

The combination and calculation of list

1
2
3
4
5
[1,2,3]+['A','B','C']
#out:[1, 2, 3, 'A', 'B', 'C']

[1,2,3]*3
#out:[1, 2, 3, 1, 2, 3, 1, 2, 3]

Delete by using del

1
2
3
4
spam=[1,2,3,4]
del spam[2]
spam
#out:[1, 2, 4]

one usage: enter the cats’ name:

1
2
3
4
5
6
7
8
9
10
catnames=[]
while True:
print('Enter the name of cat '+str(len(catnames)+1)+'(Or enter nothing to stop.):')
name=input()
if name =='':
break
catnames=catnames+[name]
print('The cat names are:')
for name in catnames:
print(' '+name)

The processing steps:

Enter the name of cat 1(Or enter nothing to stop.):
Alpha
Enter the name of cat 2(Or enter nothing to stop.):
Bravo
Enter the name of cat 3(Or enter nothing to stop.):
Cathy
Enter the name of cat 4(Or enter nothing to stop.):

The result:

The cat names are:
Alpha
Bravo
Cathy

Use list in loop

1
2
3
4
5
6
7
8
supplies=['pen','staplers','flame-throwers','binders']
for i in range(len(supplies)):
print('Index '+str(i)+' in supplies is: '+supplies[i])
#out:
#Index 0 in supplies is: pen
#Index 1 in supplies is: staplers
#Index 2 in supplies is: flame-throwers
#Index 3 in supplies is: binders

in and not in

example:

1
2
'howdy'in['hello','hi','howdy']
#out:True

usage:

1
2
3
4
5
6
7
8
9
10
11
mypet=['Zophie','Pooka','Simon']
print('Enter a pet name:')
name=input()
if name not in mypet:
print('I do not have a pet named '+name)
else:
print(name+' is my pet.')
#out:
#Enter a pet name:
#Simon
#Simon is my pet.

improve the former usage about the cats’ names:

1
2
3
4
5
6
7
8
9
10
11
12
13
catnames=[]
while True:
print('Enter the name of cat '+str(len(catnames)+1)+'(Or enter nothing to stop.):')
name=input()
if name =='':
break
if name in catnames: #added part
print('The name has existed')
else:
catnames=catnames+[name]
print('The cat names are:')
for name in catnames:
print(' '+name)

The processing steps:

Enter the name of cat 1(Or enter nothing to stop.):
Alpha
Enter the name of cat 2(Or enter nothing to stop.):
Bravo
Enter the name of cat 3(Or enter nothing to stop.):
Alpha
The name has existed
Enter the name of cat 3(Or enter nothing to stop.):
Cathy
Enter the name of cat 4(Or enter nothing to stop.):

The result:

The cat names are:
Alpha
Bravo
Cathy

Mutiple values:

We can give the list other values.

1
2
3
4
cat=['fat','black','loud']
size, color, dispostion=cat
print(size)
#out:fat

Other instructions

append: add a new value to the list in the end

1
2
3
4
spam=[1,2,3,4]
spam.append('mouse')
spam
#out:[1, 2, 3, 4, 'mouse']

insert: add a new value in the middle

1
2
3
4
spam=[1,2,3,4]
spam.insert(1,'chicken')
spam
#out:[1, 'chicken', 2, 3, 4]

index: show the position of the value

1
2
3
spam=[1,2,3,4]
spam.index(3)
#out: 2

remove: remove the value from the list

1
2
3
4
spam=[1,2,3,4]
spam.remove(1)
spam
#out:[2, 3, 4]

sort: put the value in order

numbers:

1
2
3
4
spam=[2,5,3.14,1,-7]
spam.sort()
spam
#out:[-7, 1, 2, 3.14, 5]

words: (the upper letters are in the front)

1
2
3
4
spam=['ants','cats','dogs','badgers']
spam.sort()
spam
#out:['ants', 'badgers', 'cats', 'dogs']

reverse: reverse the order of the value

1
2
3
4
spam=['ants', 'badgers', 'cats', 'dogs']
spam.sort(reverse=True)
spam
#out:['dogs', 'cats', 'badgers', 'ants']

ignore the lower or upper letters:

1
2
3
4
spam=['b','a','A','z','Z']
spam.sort(key=str.lower)
spam
#out: ['a', 'A', 'b', 'z', 'Z']

tuple: (can be read only)

1
2
tuple([1,2,3,4])
#out:(1, 2, 3, 4)

*if turn dictionary to tuple, it will only show the key:

1
2
tuple({1:2,3:4})
#out:(1, 3)