文章目录

  • 列表 list
  • 列表的嵌套使用
  • 列表的特性
  • 索引 index
  • 切片 slide
  • 重复 repeat
  • 连接 link
  • 成员操作符
  • 迭代
  • 列表元素的添加
  • 普通方法:
  • 追加
  • 扩展
  • 插入
  • 列表元素的删除
  • .pop()方法 弹出
  • .remove 删除
  • del 在内存中删除
  • 列表元素的修改
  • 可通过索引的方式对单个元素进行重新赋值
  • 通过切片的方式对多个元素进行重新赋值
  • 列表的查看
  • .count()查看元素出现次数
  • .index()查看元素最小索引值
  • 列表的排序
  • 元组 tuple
  • 元素的定义:
  • 元组的特性
  • 索引 index
  • 切片 cut
  • 重复 repeat
  • 连接 link
  • 成员操作符
  • 迭代
  • 元组中常用的内置方法
  • .index()
  • .count()
  • 元组的使用(赋值)
  • 集合 set
  • 集合的定义
  • 空集合的定义
  • 集合的特性
  • 迭代
  • 枚举
  • 集合的常用方法
  • .add()
  • .update()
  • .pop()
  • .remove
  • 集合的交集
  • 集合的并集
  • 集合的差集
  • 对等差分
  • 超集和子集
  • 字典
  • 空字典的定义:
  • 字典的嵌套
  • 字典的特性
  • 成员操作符
  • 迭代
  • 字典元素的增加
  • 单个元素的添加
  • 多个元素的添加
  • 添加key值 .setdefault ()
  • 字典中元素的删除
  • del
  • .pop()方法
  • .popitem 删除最后一组键值对
  • .clear() 清空字典内容
  • 字典元素的查看
  • .keys() .values()
  • .items()
  • 查看对应key的value值:
  • 通过循环遍历查看


列表 list

列表就像整型,字符型一样,本身就是一个数据类型。
python中的列表类似于c语言中的数组。
但是C语言中的数组只能定义的一组相同数据类型的数据。
而list就相当于高级版的数组,它可以定义不同类型的数据。
列表的表示为中括号’[ ]’

messages = ['xng',18,True,0.2 ] ##(可以包含整型,浮点型,字符型,bool型)
print(type(messages))

输出结果为:

python 数组按文本存储 python数组的数据类型_元组

列表的嵌套使用

messages = ['xng',18,True,0.2,[1,2,3,4,5,6,7]]
print(messages)
print(type(messages))

输出结果:

python 数组按文本存储 python数组的数据类型_元组_02


演示:(索引)打印第一个清单中的第二个元素:

service = [['http','ftp'],['firewall','ssh']]
print(service[0][1])

运行结果:

python 数组按文本存储 python数组的数据类型_linux_03


演示:(切片)打印第二个列表中的所有元素:

service = [['http','ftp'],['firewall','ssh']]
print(service[1][:])

运行结果:

python 数组按文本存储 python数组的数据类型_元组_04

列表的特性

索引 index

service = ['http','ftp','firewall','ssh']
print(service[0])
print(service[-1])

运行结果:

python 数组按文本存储 python数组的数据类型_linux_05

切片 slide

service = ['http','ftp','firewall','ssh']
print(service[0:])
print(service[2:])
print(service[::-1])

运行结果:

python 数组按文本存储 python数组的数据类型_linux_06

重复 repeat

service = ['http','ftp','firewall','ssh']
print(service * 3)

运行结果:

python 数组按文本存储 python数组的数据类型_linux_07

连接 link

service = ['http','ftp','firewall','ssh']
service1 = ['samba','Mysql' ]
print(service + service1)

运行结果:

python 数组按文本存储 python数组的数据类型_python_08

成员操作符

service = ['http','ftp','firewall','ssh']
service1 = ['samba','Mysql' ]
print('Mysql' in service)
print('Mysql' in service1)

python 数组按文本存储 python数组的数据类型_迭代_09

迭代

service = ['http','ftp','firewall','ssh']
service1 = ['samba','Mysql' ]
for se in service:
    print(se)

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_10


列表演示:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_11

name = ['fentiao','fendai','fensi','apple']
print('I have %s,%s,%s and %s.'%(name[0],name[1],name[2],name[3]))  ##方法1
print('I have ' + (',').join(name[:-1]) + ' and ' + name[-1]+'.')  ##方法2

运行结果:

python 数组按文本存储 python数组的数据类型_linux_12

列表元素的添加

普通方法:

service = ['http','ftp','ssh']
print(service + ['firewalld'])

运行结果:

python 数组按文本存储 python数组的数据类型_python_13

追加

内置方法:追加
使用.append()方法,追加单个元素:
适合使用在循环语句

service = ['http','ftp','ssh']
print(service + ['firewalld'])
service.append('firewalld')
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_迭代_14

扩展

使用.extend()方法,追加多个元素:(扩展)

service = ['http','ftp','ssh']
service.extend(['firewalld','samba'])
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_python_15

插入

前面说到的.append() 和 .extend()都只添加到列表的最后一个元素。

service = ['http','ftp','ssh']
service.insert(0,'samba')  ##将samba插入到service第一个元素
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_16

列表元素的删除

.pop()方法 弹出

最后一个元素开始弹出

service = ['http','ftp','ssh']
a = service.pop()  ##一般在弹出的时候赋值
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_linux_17


注意:在元素弹出时,当前列表就没有该元素。但是该元素在内存中依然存在,不能被重新赋值使用。所以一般在弹出的同时对其赋变量。

也可以给.pop()传一个参数,使其能够弹出指定的元素:

service = ['http','ftp','ssh']
a = service.pop(1)   ##弹出第二个元素
print(a)
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_python_18

.remove 删除

service = ['http','ftp','ssh']
service.remove('ftp')
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_19

del 在内存中删除

service = ['http','ftp','ssh']
print(service)
del service
print(service)

python 数组按文本存储 python数组的数据类型_linux_20

列表元素的修改

可通过索引的方式对单个元素进行重新赋值

service = ['http','ftp','ssh']
service[0] = 'samba'
print(service)

python 数组按文本存储 python数组的数据类型_linux_21

通过切片的方式对多个元素进行重新赋值

service = ['http','ftp','ssh']
service[:2] = 'samba','firewalld'
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_linux_22

列表的查看

.count()查看元素出现次数

service = ['http','ftp','ssh','ftp']
print(service.count('ftp'))

运行结果:

python 数组按文本存储 python数组的数据类型_迭代_23

.index()查看元素最小索引值

service = ['http','ftp','ssh','ftp']
print(service.index('ftp'))

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_24


可以添加索引点范围:

service = ['http','ftp','ssh','ftp','firewalld']
print(service.index('firewalld',0,5))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_25

列表的排序

service = ['http','ftp','ssh','ftp','firewalld']
service.sort()  ##默认按照ASCII进行排序,不区分大小写
print(service)

运行结果:

python 数组按文本存储 python数组的数据类型_python_26

演示:
输出随机数列表:

import random
li = list(range(10))
print(li)
random.shuffle(li)
print(li)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_27


python 数组按文本存储 python数组的数据类型_python 数组按文本存储_28


演示:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_29

user = ['root','westos']
password = ['123','456']
trycount = 0
while trycount < 3:
    inuser = input('Username:')
    inpass = input('Password:')
    if inuser in user:
        index = user.index(inuser)
        if inpass == password[index]:
            print('Login Success!')
            break
        else:
            print(' Password error!')
            trycount += 1
    else:
        print('%s is not exist!'%inuser)
        trycount += 1
else:
    print('no more chance!')

运行结果:

三次输入不存在:

python 数组按文本存储 python数组的数据类型_python_30


输入正确:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_31


演示:

python 数组按文本存储 python数组的数据类型_python_32


<1>添加会员信息

<2>删除会员信息

<3>查看会员信息

<4>退出

管理员登录

s = """<1>添加会员信息
<2>删除会员信息
<3>查看会员信息
<4>退出
"""
users = ['westos','root']
passwords = ['123','456']

print('管理员登录'.center(50,'*'))
name = input('Username:')
password = input('Password:')
if name != 'admin' or password != 'admin':
    print('Username or Password error !')
    else:
    print('Login successfully !')
    print('会员信息管理'.center(50,'*'))
    while True:
        print(s)
        want = input('yout want choose:')
        if want == '1':
            print('添加会员信息'.center(50,'*'))
            new_menber = input('New member:')
            new_password = input('New password:')
            if new_menber in users:
                print('member %s already exsit'%new_menber)
            else:
                users.append(new_menber)
                passwords.append(new_password)
                print('member %s alreay added') 
        elif want == '2':
            print('删除会员信息'.center(50, '*'))
            del_menber = input('DEL menber:')
            if del_menber not in users:
                print('please check you input,no %s member'%del_menber)
            else:
                index = users.index(del_menber)
                users.pop(index)
                passwords.pop(index)
                print('member %s already delet!')
        elif want == '3':
            print('查看会员信息'.center(50, '*'))
            print('menber         password')
            usercount = len(users)
            for i in range(usercount) :
                print('%s         %s'%(users[i],passwords[i]))
        elif want == '4':
            print('Logout Sccessfully!')
            exit()
        else:
            print('Please check your input !')

运行结果:

登录密码错误:

python 数组按文本存储 python数组的数据类型_迭代_33

登录密码正确:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_34


菜单序号输入不正确结果:

python 数组按文本存储 python数组的数据类型_迭代_35

添加-查看:

python 数组按文本存储 python数组的数据类型_元组_36

python 数组按文本存储 python数组的数据类型_迭代_37


添加已经存在的会员:

python 数组按文本存储 python数组的数据类型_linux_38

删除-查看:

python 数组按文本存储 python数组的数据类型_python_39


python 数组按文本存储 python数组的数据类型_python 数组按文本存储_40


删除不存在的会员:

python 数组按文本存储 python数组的数据类型_linux_41


退出:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_42

元组 tuple

元组为阉割版的列表,元组本身为不可变数据类型,无增删改查功能。但元组内可以存储任意数据类型。
元组的表示: 小括号 ‘( )‘

元素的定义:

a = (1,3.14,'xng',True)
print(a)
print(type(a))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_43


若元组内只有一个元素时,须在单个元素后加’,:

a = ('westos')
b = ('westos',)
print(a)
print(type(a))
print(b)
print(type(b))

输出结果:

python 数组按文本存储 python数组的数据类型_元组_44

元组的特性

索引 index

name = ('root','xng','westos')
password = ('123','345','456')
print(name[1])

输出结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_45

切片 cut

name = ('root','xng','westos')
password = ('123','345','456')
print(name[1:])
print(name[:])
print(name[::-1])

运行结果:

python 数组按文本存储 python数组的数据类型_元组_46

重复 repeat

name = ('root','xng','westos')
password = ('123','345','456')
print(name * 2)

输出结果:

python 数组按文本存储 python数组的数据类型_linux_47

连接 link

name = ('root','xng','westos')
password = ('123','345','456')
print(name +('hello','linux'))

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_48

成员操作符

name = ('root','xng','westos')
password = ('123','345','456')
print('root' in name)
print('hello' in password)

运行结果:

python 数组按文本存储 python数组的数据类型_python_49

迭代

name = ('root','xng','westos')
password = ('123','345','456')
for i in name:
    print(i)

运行结果:

python 数组按文本存储 python数组的数据类型_python_50

元组中常用的内置方法

.index()

查看元素最小索引值:

name = ('root','xng','westos','hello','root')
print(name.index('root'))

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_51

.count()

查看元组中元素的个数

name = ('root','xng','westos','hello','root')
print(name.count('root'))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_52

元组的使用(赋值)

元组中有多少个元素,就用多少个变量去接收。可以一次定义多个变量:

t = ('xng',18,78,85 )
name,age,Englishi,Math = t
print(name)
print(Englishi)

运行结果:

python 数组按文本存储 python数组的数据类型_python_53

集合 set

集合的定义

集合是无序的数据类型,使用’{}‘可以定义集合。
特点:集合中的元素不可重复,故常被用到去重。

a = {1,2,2,3,3,4,4,5}
print(a)
print(type(a))

输出结果:

python 数组按文本存储 python数组的数据类型_linux_54

空集合的定义

在定义空集合时,须使用内置方法色图set([]),否则定义不为集合:

a = {}
print(a)
print(type(a))
b = set([])
print(b)
print(type(b))

输出结果:

python 数组按文本存储 python数组的数据类型_元组_55


演示:

对列表去重:

a = [1,2,2,3,3,4,5,6,6,7]
print(list(set(a)))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_56

集合的特性

集合只支持成员操作符和迭代,不支持索引,重复,切片,连接。

迭代

a = (1,2,3,4)
for i in a:
    print(i)

运行结果:

python 数组按文本存储 python数组的数据类型_linux_57

枚举

a = (1,2,3,4)
for i,v in enumerate(a):
    print(i,v)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_58

集合的常用方法

.add()

.add()可以给集合添加单个元素:

a = {1,2,3,4}
a.add(6)
print(a)

python 数组按文本存储 python数组的数据类型_迭代_59

.update()

.update()方法可以给集合添加多个元素:
注意:并不是在集合最后边添加

a = {1,7,3,6}
a.update({2,4,8,9})
print(a)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_60

.pop()

使用.pop()可以随机弹出集合中的一个元素:

a = {1,3,7,6}
a.pop()
print(a)

python 数组按文本存储 python数组的数据类型_linux_61

.remove

.remove()可以删除集合中指定的元素:

a = {1,3,7,6}
a.remove(7)
print(a)

运行结果:

python 数组按文本存储 python数组的数据类型_linux_62

集合的交集

两种方法:

a = {1,2,3,4}
b = {2,3,4,5}
print(a & b)
print(a.intersection(b))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_63

集合的并集

两种方法:

a = {1,2,3,4}
b = {2,3,4,5}
print(a|b)
print(a.union(b))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_64

集合的差集

a = {1,2,3,4}
b = {2,3,4,5}
print(b.difference(a)) ##b和a的差集,也就是a-a&b
print(a.difference(b)) ##a和b的差集,也就是b-a&b

python 数组按文本存储 python数组的数据类型_linux_65

对等差分

对等差分=并集-差集、
两种方法:

a = {1,2,3,4}
b = {2,3,4,5}
print(b.difference(a))
print(a.difference(b))

运行结果:

python 数组按文本存储 python数组的数据类型_python_66

超集和子集

a所有的元素都在b中包含,且b中有a不含有的元素 b就叫做a的超集。也就是说,a是b的子集。

可以使用内置方法.issuperset()和.issubset()进行判断:

a = {2,3,4}
b = {2,3,4,5}
print(b.issuperset(a))  ##判断b是否为a的超集
print(a.issubset(b))    ##判断a是否为b的子集

运行结果:

python 数组按文本存储 python数组的数据类型_迭代_67

演示:
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个0~1000之间的随机整数(N<=1000),N为用户输入。
对于其中重复的数字,只保留一个,把其余相同的数字去掉,不同的数对应着不同的学生的学号,然后把这些数从小到大排序,按照排好的顺序去找同学做调查,请你帮助命名完成”去重“与”排序“工作。

import random
a = set([])
N = int(input('N:'))
for i in range(N):
    b = random.randint(0, 1000)
    a.add(b)
print(sorted(a))

运行结果:

python 数组按文本存储 python数组的数据类型_元组_68

字典

字典是一个无序的数据集合,通常使用print输出字典的顺序和定义的顺序是不一样的。
之前在学asibl时,可以知道,字典就是键值对。中间以冒号隔开,除了结尾的键值对,其余键值对后加逗号。
一个key值可以对应多个value值,且value值类型不限

字典的定义:

a = {
    'xiaoming': [80,87,90,98],
    'xng': [77,98,87,85],
    'teacher': 'westos'
}
print(a)
print(type(a))

运行结果:

python 数组按文本存储 python数组的数据类型_linux_69


若要定义多个key值对应同一个value值,可以使用.fromkeys()方法:

.fromkeys()用于创建一个新的字典,并以可迭代对象中的元素为键,所有键对应同一个值,默认为None。

python 数组按文本存储 python数组的数据类型_迭代_70

空字典的定义:

方法一:

a= {}         
print(a)      
print(type(a))

运行结果:

python 数组按文本存储 python数组的数据类型_linux_71


方法二:;工厂函数

a = dict()           
print(a)             
print(type(a))

运行结果:

python 数组按文本存储 python数组的数据类型_迭代_72

字典的嵌套

student = {                           
    'xng':{                           
        'id' :'05178205',             
        'Math':89,                    
        'school':'youdian'            
    } ,                               
    'xiaoming':{                      
        'id':'08171111',              
        'Math':85,                    
        'school':'youdian'            
    }                                 
}                                     
print(student['xng']['id'])   ##通过key值获取value值

运行结果:

python 数组按文本存储 python数组的数据类型_linux_73

字典的特性

字典不支持索引和切片。
因为字典的key值是唯一的,所以重复和连接对于字典来说没有意义。

成员操作符

d = {           
    '1':'a',    
    '2':'b',    
    '3':'c'     
}               
print('1' in d) 
print('a' in d)

输出结果:

python 数组按文本存储 python数组的数据类型_linux_74

迭代

默认循环遍历key值,并不遍历value值

d = {         
    '1':'a',  
    '2':'b',    
    '3':'c'       
}             
for i in d :  
    print(i)

输出结果:

python 数组按文本存储 python数组的数据类型_迭代_75

加上key值,就能看到对应关系:

d = {
    '1':'a',
    '2':'b',
    '3':'c'
}
for i in d :
    print(i,d[i])

输出结果:

python 数组按文本存储 python数组的数据类型_元组_76

字典元素的增加

单个元素的添加

可以使用定义的方式进行添加:
当集合中有该key值时,对该key值对应的value值进行修改,
当集合中无该key值时,添加该key值和value值。

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
server['servere'] = 'firewalld'
server['servera'] = 'DNS'

print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_linux_77

多个元素的添加

使用.uopdate()进行更新多个元素:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp'
}
new_server = {
    'server1': 'http',
    'server2':'mariadb',
    'server3':'firewalld'
}
server.update(new_server)
print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_78


或者:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp'
}
server.update(serverc='mariadb',serverd='nfs')
print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_linux_79

添加key值 .setdefault ()

使用.setdefault()方法可以添加字典的key值。
若key值存在,不做变化,若key值不存在,对字典进行更新

server = {
    'servera' : 'samba',
    'serverb' : 'ftp'
}
server.setdefault('serverb','firewalld')
server.setdefault('servere','nfs')
print(server)

运行结果:

python 数组按文本存储 python数组的数据类型_python_80

字典中元素的删除

两种方法:del和.pop()

del

del可以直接删除键值对。

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
del server['serverb']
print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_元组_81

.pop()方法

.pop()方法删除时,如果字典里有该键,则删除键和对应的值,如果没有该键,直接报错:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
server.pop('servera')
print(server)

运行结果:

字典中有该键情况:

python 数组按文本存储 python数组的数据类型_迭代_82


字典中无该键的情况:直接报错

python 数组按文本存储 python数组的数据类型_迭代_83

.popitem 删除最后一组键值对
server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
server.popitem()
print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_linux_84

.clear() 清空字典内容
server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
server.clear()
print(server)

输出结果:

python 数组按文本存储 python数组的数据类型_迭代_85

字典元素的查看

.keys() .values()

.keys()为字典中元素的所有key值
.values()为字典中所有的value值

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server.keys())      ##打印所有key值
print(server.values())    ##打印所有value值

输出结果:

python 数组按文本存储 python数组的数据类型_迭代_86

.items()

.items()可以字典中查看键值对的对应关系:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server.items())

输出结果:

python 数组按文本存储 python数组的数据类型_linux_87

查看对应key的value值:
  • 直接通过key值查看对应value值:
server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server['serverb'])   ##如果字典中有该key值,则输出value值。若没有该值,直接报错。

输出结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_88

  • 可以通过 .get() 方法获取对应key的value值
    在key值存在的情况下:
server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server.get('serverb'))

输出结果:

python 数组按文本存储 python数组的数据类型_python_89


key值不存在的情况下:

当在key值不存在的情况下:若无指定返回值,默认返回None。

若指定返回值,则返回该值:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server.get('servere','DNS')) ## servere值并不存在,返回DNS

输出结果:

python 数组按文本存储 python数组的数据类型_linux_90


不进行指定返回值:

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
print(server.get('servere'))

输出结果为None:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_91

通过循环遍历查看

在循环遍历时,若是直接遍历该字典,默认遍历该字典的key值,若是想要遍历key值和value值,须遍历.items()

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
for key,value in server.items():
    print('key:',key,'value:',value)

输出结果:

python 数组按文本存储 python数组的数据类型_元组_92


或者

server = {
    'servera' : 'samba',
    'serverb' : 'ftp',
    'serverc' : 'http'
}
for key in server:
    print('key:',key,'value:',server[key])

输出结果:

python 数组按文本存储 python数组的数据类型_linux_93

演示:

python 数组按文本存储 python数组的数据类型_迭代_94


方法1:

import random  ## 导入随机数
a= []         ##定义空列表进行接收随机数
for i in range(1000):
    b = random.randint(20,100)
    a.append(b)  ## 将随机数添加进列表
s = sorted(a)  ##对生成的列表进行排序
print(s)       ##打印排序过的列表
c = list(set(a))  ##将列表转化为集合进行去重后又转化为列表
for p in c:   ##对去重过的列表进行循环遍历
    print(p ,'出现了',a.count(p),'次') ##输出

运行结果:

python 数组按文本存储 python数组的数据类型_python_95

方法2:

import random
all_nums = []
for item in range(1000):
    all_nums.append(random.randint(20,100)
# print(all_nums)
sorted_nums = sorted(all_nums)
num_dict = {}

for num in sorted_nums:
    if num in num_dict:
        num_dict[num] += 1
    else:
        num_dict[num] = 1
print(num_dict)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_96

演示:

python 数组按文本存储 python数组的数据类型_元组_97


python 数组按文本存储 python数组的数据类型_python 数组按文本存储_98


方法一:

a = input('Please input:')
b = a.split(' ')
c = set(b)
for i in c:
    print(i,b.count(i))

输出结果:

python 数组按文本存储 python数组的数据类型_元组_99


方法二:

s = input('Please input:')
s_li = s.split( )
word_dict = {} ##定义空字典来保存 单词和出现的次数
for item in s_li:
    if item not in word_dict:
        word_dict[item] = 1
    else:
        word_dict[item] += 1
print(word_dict)

运行结果:

python 数组按文本存储 python数组的数据类型_元组_100

演示:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_101

import random
a = []
b = random.randint(0,100)
for i in range(100):
    a.append('6102009%.3d'%(i+1))
c = {}.fromkeys(a,'redhat')
print('卡号\t\t\t\t\t\t\t\t 密码')
for k,v in c.items():
    print(k,'\t\t\t\t\t\t',v )

运行结果:

python 数组按文本存储 python数组的数据类型_python 数组按文本存储_102