玩命加载中 . . .

python-列表操作


4.1 遍历整个列表

遍历整个列表,对列表中的元素,执行同样的操作

In [1]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician)
alice
david
carolina

4.1.2 在for循环中执行更多的操作

In [2]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician.title()+ ",that was a great trick!")
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!

In [3]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician.title()+ ",that was a great trick!")

    print("I can't wait to see your next trick, "+magician.title()+".\n")
Alice,that was a great trick!
I can't wait to see your next trick, Alice.

David,that was a great trick!
I can't wait to see your next trick, David.

Carolina,that was a great trick!
I can't wait to see your next trick, Carolina.

4.1.3 在for循环结束后执行一些操作

In [4]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician.title()+ ",that was a great trick!")

    print("I can't wait to see your next trick, "+magician.title()+".\n")

print("Thank you ,everyone. That was a great magic show!")
Alice,that was a great trick!
I can't wait to see your next trick, Alice.

David,that was a great trick!
I can't wait to see your next trick, David.

Carolina,that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you ,everyone. That was a great magic show!

4.2.1 忘记缩进

In [5]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

print(magician.title()+ ",that was a great trick!")
  File "<ipython-input-13-2cc406bd9de6>", line 3
    print(magician.title()+ ",that was a great trick!")
        ^
IndentationError: expected an indented block

4.2.2 忘记缩进额外的代码行

In [6]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician.title()+ ",that was a great trick!")

print("I can't wait to see your next trick, "+magician.title()+".\n")
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
I can't wait to see your next trick, Carolina.

4.2.3 不必要的缩进

In [7]:

message="Hello Python World!"

    print(message)
  File "<ipython-input-16-6fe279010ca3>", line 2
    print(message)
    ^
IndentationError: unexpected indent

4.2.4 循环后不必要的缩进

In [8]:

magicians = ['alice', 'david','carolina']

for magician in magicians:

    print(magician.title()+ ",that was a great trick!")

    print("I can't wait to see your next trick, "+magician.title()+".\n")

    print("Thank you ,everyone. That was a great magic show!")
Alice,that was a great trick!
I can't wait to see your next trick, Alice.

Thank you ,everyone. That was a great magic show!
David,that was a great trick!
I can't wait to see your next trick, David.

Thank you ,everyone. That was a great magic show!
Carolina,that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you ,everyone. That was a great magic show!

4.2.5 遗漏了冒号

In [9]:

magicians = ['alice', 'david','carolina']

for magician in magicians

    print(magician.title()+ ",that was a great trick!")
  File "<ipython-input-1-c913b202d98f>", line 2
    for magician in magicians
                             ^
SyntaxError: invalid syntax

4.3 创建数值列表

4.3.1 使用函数range()

In 10]:

for value in range(1,7):

    print(value)
1
2
3
4
5
6

4.3.2 使用range()函数创建数字列表

In [11]:

numbers = list(range(1,7))

print(numbers)
[1, 2, 3, 4, 5, 6]

In [12]:

numbers = list(range(3,46,6))

print(numbers)
[3, 9, 15, 21, 27, 33, 39, 45]

In [13]:

squares = []

for value in range(1,11):

    square = value**2

    squares.append(square)

print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In [14]:

squares = []

for value in range(1,11):

    squares.append(value**2)

print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.3.3 对数字列表进行简单的统计计算

In [15]:

digits = [1,2,3,4,5,6,7,8,9,0]

min(digits)

Out[15]:

0

In [16]:

digits = [1,2,3,4,5,6,7,8,9,0]

max(digits)

Out[16]:

9

In [17]:

digits = [1,2,3,4,5,6,7,8,9,0]

sum(digits)

Out[17]:

45

4.3.4 列表解析

In [18]:

squares = [value**2 for value in range(1,11) ]

print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.4.1 切片

In [19]:

players = ['charles', 'martina' ,'michael', 'florence', 'eli']

print(players[1:3])
['martina', 'michael']

In [20]:

players = ['charles', 'martina' ,'michael', 'florence', 'eli']

print(players[:2])
['charles', 'martina']

4.4.2 遍历切片

In [21]:

players = ['charles', 'martina' ,'michael', 'florence', 'eli']

print("There is the first three players in my team :")

for player in players[:3]:

    print(player.title())
There is the first three players in my team :
Charles
Martina
Michael

4.4.3 复制列表

In [22]:

my_foods = ['pizza', 'falatel', 'carrot cake']

friend_foods = my_foods[:]

print("My favorite foods are :")

print(my_foods)

print("\nMy friends's favorite foods are :")

print(friend_foods)
My favorite foods are :
['pizza', 'falatel', 'carrot cake']

My friends's favorite foods are :
['pizza', 'falatel', 'carrot cake']

In [23]:

my_foods = ['pizza', 'falatel', 'carrot cake']

friend_foods = my_foods[:]

my_foods.append('cannoli')

friend_foods.append('ice cream')

print("My favorite foods are :")

print(my_foods)

print("\nMy friends's favorite foods are :")

print(friend_foods)
My favorite foods are :
['pizza', 'falatel', 'carrot cake', 'cannoli']

My friends's favorite foods are :
['pizza', 'falatel', 'carrot cake', 'ice cream']

4.5.1 定义元组

In [24]:

dimensions = (100,7)

print(dimensions[0])

print(dimensions[1])
100
7

In [25]:

dimensions = (100,7)

dimensions[0] = 9

print(dimensions[0])

print(dimensions[1])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-ad836170a6e7> in <module>
      1 dimension = (100,7)
      2 
----> 3 dimension[0] = 9
      4 
      5 print(dimension[0])

TypeError: 'tuple' object does not support item assignment

4.5.2 遍历元组中的值

In [26]:

dimensions = (200,78)

for dimension in dimensions:

    print(dimension)
200
78

4.5.3 修改元组变量

In [27]:

dimensions = (200,78)

print("Original dimensions :")

for dimension in dimensions:

    print(dimension)

dimensions = (4,98)

print("Modified dimensions :")

for dimension in dimensions:

    print(dimension)
Original dimensions :
200
78
Modified dimensions :
4
98

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