python中的列表

时间:2024-04-02 15:09:34

Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。

数组:存储同一种数据类型的集合 scores=[12,13,14]
列表:(打了激素的数组):可以存储任意数据类型的集合

一.创建列表

In [1]: name1 = 'tom'

In [2]: name2 = 'lily'

In [3]: name3 = 'bob'

In [4]: name4 ='coco'

In [5]: name1
Out[5]: 'tom'

In [6]: name2
Out[6]: 'lily'

In [7]: name3
Out[7]: 'bob'

In [8]: name = ['tom','lily','bob','cooc']

In [9]: name
Out[9]: ['tom', 'lily', 'bob', 'cooc']

In [10]: type(name)
Out[10]: list

一个变量存储多个信息

# 列表里:可以存储不同的数据类型
li = [1,1.2,True,'hello']
print li
print type(li)

# 列表里面也可以嵌套列表(列表:也是一种数据类型)
li = [1,1.2,True,'hello',[1,2,3,4,5]]
print li
print type(li)

 

二.列表的增加

service = ['http', 'ssh', 'ftp']

# 1.
print service + ['firewalld']

# 2.append:追加 追加一个元素到列表中
service.append('firewalld')
print service

# 3.extend:拉伸 追加多个元素到列表中
service.extend(['mysql', 'firewalld'])
print service

# 4. insert:在指定索引位置插入元素
service.insert(1,'samab')
print service

python中的列表

三.列表的删除

service = ['http', 'ssh', 'ftp']

 #1.如果pop()不传递值的时候,默认弹出最后一个元素
# print service.pop()
# pop()也可以传递索引值
# print service.pop(0)

python中的列表

# 2.remove:删除指定的元素
# service.remove('ssh')
# print service

python中的列表

# 3.del 关键字 从内存中删除列表
# print service
# del service
# print  service

此时service已经在内存中删除,会报错
python中的列表

四.列表的修改

service = ['http', 'ssh', 'ftp']

# 通过索引,重新赋值
service[0] = 'mysql'
print service

# 通过切片
print service[:2]
service[:2] = ['samba','ladp']
print service

python中的列表

五.列表的查看

service = ['http', 'ssh', 'ftp','ftp']

# 查看列表中元素出现的次数
print service.count('ssh')

# 查看指定元素的索引值
print service.index('ssh')

python中的列表

 

六.列表的排序

# service = ['http', 'ssh', 'ftp','ftp']
#
# # 按照Ascii码进行排序的
# service.sort()
# print service
#
# service.sort(reverse=True)
# print service

# phones = ['bob', 'harry', 'Lily', 'Alice']
# # phones.sort()
# # 对字符串排序不区分大小写
# #phones.sort(key=str.lower)
# phones.sort(key=str.upper)
# print phones

python中的列表

import random
li = list(range(10))
print li
# 将原有的列表顺序打乱
random.shuffle(li)
print li

python中的列表

 

七.列表的特性

service = ['http', 'ssh', 'ftp']

# # 索引
# print service[0]
# print service[-1]

python中的列表
# # 切片
# print service[::-1] # 列表的翻转
# print service[1:] # 除了第一个元素之外的其他元素
# print service[:-1] # 除了最后一个元素之外的其他元素

python中的列表
# # 重复
# print service * 3
# # 连接
# service1 = ['mysql','firewalld']
# print service + service1

python中的列表
# # 成员操作符
# print 'firewalld' in service
# print 'firewalld' in service1
# print 'firewalld' not in service

python中的列表
# for循环遍历
# print '显示服务'.center(50,'*')
# for se in service:
#     print se

python中的列表

# 列表里嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
# 索引
print service2[0][1]
print service2[-1][1]
# 切片
print service2[:][1]
print service2[:-1][0]
print service2[0][:-1]

python中的列表

 

八.列表练习题

 1.假定有下面这样的列表:
    names = ['fentiao', 'fendai', 'fensi', 'apple']
    输出结果为:'I have fentiao, fendai, fensi and apple.'

python中的列表

代码:

names = ['fentiao', 'fendai', 'fensi', 'apple']
print 'I have ' + ','.join(names[:-1]) + ' and ' + names[-1]

 

题目二:

1.后台管理员只有一个用户:admin 密码:admin
2.当管理员登陆成功后,才可以管理用户信息
3.管理用户信息包含
    添加用户信息
    删除用户信息
    查看用户信息
    退出

python中的列表

代码:

print '管理员登陆'.center(50, '*')
inuser = raw_input('UserName:')
inpasswd = raw_input('Password:')

# 现存用户名
users = ['root', 'westos']
# 现存用户密码
passwds = ['123', '456']

if inuser == 'admin' and inpasswd == 'admin':
    print '管理员登陆成功'
    print '用户信息管理'.center(50, '*')
    while True:
        print """
        1 -添加用户信息
        2 -删除用户信息
        3 -查看用户信息
        4 -退出

        """
        choice = raw_input('请选择你的操作:')
        if choice == '1':
            print '添加用户信息'.center(50, '*')
            addUser = raw_input('添加用户:')
            if addUser in users:
                print '用户%s已经存在' % addUser
            else:
                addPasswd = raw_input('密码:')
                # 把用户名和密码添加到列表中
                users.append(addUser)
                passwds.append(addPasswd)
                print '添加用户%s成功' %addUser

        elif choice == '2':
            print '删除用户信息'.center(50,'*')
            delUser = raw_input('删除的用户:')
            delIndex = users.index(delUser)
            users.remove(delUser)
            passwds.pop(delIndex)
            print '删除用户%s成功' %delUser

        elif choice == '3':
            print '查看用户信息'.center(50,'*')
            print '\t用户名\t密码'
            userCount = len(users) #2
            for i in range(userCount): # 0 ,1
                print '\t%s\t%s' %(users[i],passwds[i])

        elif choice == '4':
            exit()
        else:
            print '请输入正确的选择'
else:
    print '管理员登陆失败!'

题目三:

1.系统里面有用户 用户有密码
    users = ['root','westos']
    passwds = ['123','456']
2.用户登陆(判断用户登陆是否成功)
    1).判断用户是否存在
        2).如果存在:
            判断用户密码是否正确:先找出用户对应的索引,
            根据passwds[索引值]
            如果正确:登陆成功,退出循环
            如果密码不正确:重新登陆 (机会只有三次)
    2).不存在
    重新登陆(机会只有三次)

python中的列表

代码:

users = ['root', 'westos']
passwds = ['123', '456']
trycont = 0
# 尝试登陆的次数
while trycont < 3:
    inuser = raw_input('用户名:')
    inpasswd = raw_input('密码:')
    # 尝试次数+1
    trycont += 1
    if inuser in users:
        # 判断用户密码是否正确
        index = users.index(inuser) # 先找用户对应的索引值
        passwd = passwds[index]
        if inpasswd == passwd:
            print '%s 登陆成功' % inuser
            break
        else:
            print '%s 登陆失败' % inuser
    else:
        print '用户%s 不存在' % inuser
else:
    print '三次机会已经用完'