玩命加载中 . . .

python-列表


3.1 列表是什么

由一些特殊顺序排列组成的元素

In [1]:

bicycles=['trek', 'canonodale', 'redline', 'specialize']

print(bicycles)
['trek', 'canonodale', 'redline', 'specialize']

3.1.1 访问列表元素

In [2]:

bicycles=['trek', 'canonodale', 'redline', 'specialize']

print(bicycles[0])
trek

3.1.2 索引从0开始而不是1

In [4]:

bicycles=['trek', 'canonodale', 'redline', 'specialize']

print(bicycles[1])

print(bicycles[3])
canonodale
specialize

In [5]:

bicycles=['trek', 'canonodale', 'redline', 'specialize']

print(bicycles[1])

print(bicycles[3])

print(bicycles[-1])
canonodale
specialize
specialize

3.1.3 使用列表中的各个值

In [7]:

bicycles=['trek', 'canonodale', 'redline', 'specialize']

message = 'my first bicycle was a '+bicycles[0].title()+'.'

print(message)
my first bicycle was a Trek.

3.2.1 修改列表元素

In [8]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles[0]='ducati'

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']

3.2.2 在列表中添加元素

1.在列表末尾加入元素

In [12]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles.append('ducati')

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']

2.在列表中加入元素

In [15]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles.insert(0, 'ducati')

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'honda', 'yamaha', 'suzuki']

In [16]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles.insert(2, 'ducati')

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'ducati', 'suzuki']

3.2.3 从列表中删除元素

1.使用del语句删除元素

In [17]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

del motorcycles[2]

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']

2.使用方法pop删除

In [21]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

poped_motocycle=motorcycles.pop()

print(motorcycles)

print(poped_motocycle)

poped_motocycle=motorcycles.pop()

print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
['honda']

3.使用pop弹出任意位置的元素

In [24]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles)

poped_motocycle=motorcycles.pop(1)

print(motorcycles)

print('The first motorcycle I owned was '+ poped_motocycle.title()+'.')
['honda', 'yamaha', 'suzuki']
['honda', 'suzuki']
The first motorcycle I owned was Yamaha.

4.根据值删除元素

In [28]:

motorcycles=['honda', 'yamaha', 'suzuki','ducati']

print(motorcycles)

motorcycles.remove('suzuki')

print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'ducati']

3.3 组织列表

3.3.1 方法sort()对列表进行永久性排列

按字母顺序进行永久性排列(不可逆)

In [31]:

cars=['bmw', 'audi', 'toyota', 'subaru']

cars.sort()

print(cars)
['audi', 'bmw', 'subaru', 'toyota']

按字母由大到小进行永久性排序(不可逆)

In [33]:

cars=['bmw', 'audi', 'toyota', 'subaru']

cars.sort(reverse=True)

print(cars)
['toyota', 'subaru', 'bmw', 'audi']

3.3.2 使用方法sorted()对列表进行临时排列

In [2]:

cars=['bmw', 'audi', 'toyota', 'subaru']

print("Here is the original list:")
print(cars)

print("\nHere is the sorted list:")
print(sorted(cars))

print("\nHere is the original list again:")
print(cars)
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']

3.3.3 方法reverse()倒着打印表

In [3]:

cars=['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']

3.3.4 方法len()确定表的长度

In [4]:

cars=['bmw', 'audi', 'toyota', 'subaru']

print(len(cars))
4

3.4 使用列表时避免索引错误

In [1]:

motorcycles=['honda', 'yamaha', 'suzuki']

print(motorcycles[3])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-2de4b1001f59> in <module>
      1 motorcycles=['honda', 'yamaha', 'suzuki']
      2 
----> 3 print(motorcycles[3])

IndexError: list index out of range

文章作者: Edgar
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Edgar !
评论
  目录