python 11课 列表:一个打了激素的数组

澳门新葡亰娱乐官网 4

修改方法如下:

  1. 能否修改一下改成如图所示(使用两种方法):

澳门新葡亰娱乐官网 1

打开 e/member/class/member_loginfun.php 文件查找:

方法一:
1.member.insert(1, 88)
2.member.insert(3, 90)
3.member.insert(5, 85)
4.member.insert(7, 90)
5.member.append(88)
复制代码
方法二:
1.member = [‘小甲鱼’, 88, ‘黑夜’, 90, ‘迷途’, 85, ‘怡静’, 90,
‘秋舞斜阳’, 88]
复制代码
对于这种情况,明显是第二种方法看起来要好一些嘛。

修改为:

澳门新葡亰娱乐官网,二. 测试题

解答:

这个功能想必大家都想知道吧,其实修改实现起来还是非常容易的,看看方法就知道了,可以同时实现用户名或者使用注册是的邮箱进行登陆帐号。

>>> name = ['F', 'i', 'h', 'C']
>>> name.insert(1, 's')
>>> name
['F', 's', 'i', 'h', 'C']
#注意数字代表的位置
>>> name = ['F', 'i', 'h', 'C']
>>> name.insert(2, 's')
>>> name
['F', 'i', 's', 'h', 'C']

member = [‘小甲鱼’, 88, ‘黑夜’, 90, ‘迷途’, 85, ‘怡静’, 90, ‘秋舞斜阳’,
88]

if(chemail($username)){ $username=RepPostVar($username); $r=$empire-fetch1("select username from {$dbtbpre}enewsmember where email='$username'"); $username=$r['username'];}else{ $username=RepPostVar($username);}
>>> for i in range(member):
    print("in")


Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    for i in range(member):
TypeError: 'list' object cannot be interpreted as an integer
>>> for i in range(member):
    print("i"n)

SyntaxError: unexpected character after line continuation character
>>> for i in range(member):
    print("i",n)

SyntaxError: unexpected character after line continuation character
>>> for i in range(member):
    print(i)


Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    for i in range(member):
TypeError: 'list' object cannot be interpreted as an integer
#此处不需要给member加上range

>>> for i in member:
    print(i)


fish
88
dark
90
road
85
quiet
90
sun
88
  1. 上一题打印的样式不是很好,能不能修改一下代码打印成下图的样式呢?【请至少使用两种方法实现
$username=RepPostVar($username);

三. 动动手

澳门新葡亰娱乐官网 2

  1. 有列表name = [‘F’, ‘i’, ‘h’,
    ‘C’],如果想要在元素’i’和’h’之间插入元素’s’,应该使用什么方法插入?

方法一:使用 insert() 和 append() 方法修改列表。

>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member1 = member[0]
>>> member2 = member[1]
>>> member3 = member[2]
>>> member4 = member[3]
>>> member5 = member[4]
>>> member1.append(88)
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    member1.append(88)
AttributeError: 'str' object has no attribute 'append'
>>> member1.extend(88)
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    member1.extend(88)
AttributeError: 'str' object has no attribute 'extend'
>>> member1
'fish'
>>> member1.append('88')
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    member1.append('88')
AttributeError: 'str' object has no attribute 'append'
>>> member1 = member(0)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    member1 = member(0)
TypeError: 'list' object is not callable
>>> member.append(88)
>>> member
['fish', 'dark', 'road', 'quiet', 'sun', 88]
>>> member1 = [member[0]]
>>> member1
['fish']
>>> member1.append(88)
>>> member1 = member[0]
>>> member2 = member[1]
>>> member3 = member[2]
>>> member4 = member[3]
>>> member5 = member[4]
SyntaxError: multiple statements found while compiling a single statement
>>> member1 = [member[0]]
>>> member2 = [member[1]]
>>> member3 = [member[2]]
>>> member4 = [member[3]]
>>> member5 = [member[4]]
SyntaxError: multiple statements found while compiling a single statement
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member1 = [member[0]]
>>> member2 = [member[1]]
>>> member3 = [member[2]]
>>> member4 = [member[3]]
>>> member5 = [member[4]]
#需要把member[4]变成列表之后才能添加元素

>>> member1.append(88)
>>> member2.append(90)
>>> member3.append(85)
>>> member4.append(90)
>>> member5.append(88)
>>> member = member1 + member2 + member3 + member4 + member5
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member.insert(0, 88, 1, 90, 2, 85, 3, 90, 4, 88)
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    member.insert(0, 88, 1, 90, 2, 85, 3, 90, 4, 88)
TypeError: insert() takes exactly 2 arguments (10 given)
>>> member.insert(0, 88)
>>> member
[88, 'fish', 'dark', 'road', 'quiet', 'sun']
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member.insert(1, 88)
>>> member
['fish', 88, 'dark', 'road', 'quiet', 'sun']
>>> member.insert(3, 90)
#用insert()方法每次添加完元素之后,之后的元素位置都会改变

>>> member
['fish', 88, 'dark', 90, 'road', 'quiet', 'sun']
>>> member.insert(5, 85)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 'sun']
>>> member.insert(7, 90)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun']
>>> member.insert(9, 88)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member = ['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
#直接覆盖的方法最为简单

>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]

3

  1. 利用for循环打印上边member列表中的每个内容,如图:

member = [‘小甲鱼’, ‘黑夜’, ‘迷途’, ‘怡静’, ‘秋舞斜阳’]

#第一种方法
member = ['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
i = 0
while i <= 8:
    print(member[i:i+2])
    i += 2

不过对于大型的列表,第一种方法可能更合适,所以我们说永远没有最好的,只有最合适的。

>>> member = ['Jack', 'David', 'Hsin', 'Chi']
>>> member
['Jack', 'David', 'Hsin', 'Chi']
>>> number = [1, 2, 3, 4, 5]
>>> number
[1, 2, 3, 4, 5]
>>> mix = [1, 'Jack', 3.14, [1, 2, 3]]
>>> mix
[1, 'Jack', 3.14, [1, 2, 3]]
>>> empty = []
>>> empty
[]
>>> member
['Jack', 'David', 'Hsin', 'Chi']
>>> member.append('Shelly')
>>> member
['Jack', 'David', 'Hsin', 'Chi', 'Shelly']
#append作为一种添加元素的方法

>>> len(member)
5
>>> member.append('fishc', 'fish')
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    member.append('fishc', 'fish')
TypeError: append() takes exactly one argument (2 given)
>>> member.extend('fishc', 'fish')
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    member.extend('fishc', 'fish')
TypeError: extend() takes exactly one argument (2 given)
#append不能一次添加两个元素
#extend可以一次添加几个元素,但是需要以数组的方式添加

>>> member.extend(['fishc', 'fish'])
>>> member
['Jack', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>> len(member)
7
>>> member.insert(1, 'Rose')
>>> member
['Jack', 'Rose', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>> member.insert(0, 'Titanic')
#insert是第三种添加元素的方法,并且可以添加到特定位置

>>> member
['Titanic', 'Jack', 'Rose', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>> 

澳门新葡亰娱乐官网 3

4

  1. 假定给定一下列表:
    member = [‘fish’, ‘dark’, ‘road’, ‘quiet’, ‘sun’]
    要求将列表修改为:
    member = [‘fish’, 88, ‘dark’, 90, ‘road’, 85, ‘quiet’, 90, ‘sun’,
    88]
    方法一:使用insert()和append()方法修改列表
    方法二:重新创建一个同名字的列表覆盖

1

  1. 利用 for 循环打印上边 member 列表中的每个内容
>>> member = ['Jack', 'Rose']
>>> member.append(['David', 'Mike'])
>>> member
['Jack', 'Rose', ['David', 'Mike']]
>>> member = ['Jack', 'Rose']
>>> member.extend(['David', 'Mike'])
>>> member
['Jack', 'Rose', 'David', 'Mike']

方法二:重新创建一个同名字的列表覆盖。

  1. member.append([‘David’, ‘Mike’])和member.extend([‘David’,
    ‘Mike’])实现的效果一样吗?

要求将列表修改为:

澳门新葡亰娱乐官网 4

假设给定以下列表:

一. 课上代码

  1. 自己动手试试看,并分析在这种情况下,向列表添加数据应当采用哪种方法比较好?
![](https://upload-images.jianshu.io/upload_images/7725813-d67df5a9da6c445b.png)

2

![](https://upload-images.jianshu.io/upload_images/7725813-63b0368ae2e2004d.png)
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图