【Python】2019年二级书编程练习(第六章 组合数据类型 简例+习题)

#第6章:组合数据类型(能表示多个数据的类型)
#序列类型:如字符串、列表和元组( )
#集合类型 { }:数据无序而不重复,可用于去重
#映射类型:如字典,键和值一一对应,每个元素是一个键值对
#集合类型是一个具体的数据类型名称,而序列和映射类型是一类总称
#遍历输出,并设定print()的end参数
for c in (1,2,3):
    print(c,end='月 ')#1月 2月 3月

#函数里多个返回值构成元组
def f(x):
    return x,x+1,x+2

print(f(1))
print(type(f(1)))
#(1, 2, 3)   <class 'tuple'>
print(type(f))#<class 'function'>

#列表 基本操作函数
ls=[1010,'1010',[1010,'1010'],1010]
print(1010 in ls)#True
print(ls+[1,2,3])#[1010, '1010', [1010, '1010'], 1010, 1, 2, 3]
print(ls*2)#[1010, '1010', [1010, '1010'], 1010, 1010, '1010', [1010, '1010'], 1010]
print(len(ls))#4 长度
print(ls.index(1010))#0 1010首次出现的位置
print(ls.count(1010))#2 1010出现的次数

#列表 索引
ls=[1010,'1010',[1010,'1010'],1010]
print(ls[-2])#[1010, '1010'] 头从0,尾从-1
#print(ls[5])——IndexError: list index out of range
for i in ls:
    print(i*2)
    '''
2020
10101010
[1010, '1010', 1010, '1010']
2020
    '''

#列表 切片(切片后也是列表类型)
#表示区间用冒号,如切片   表示枚举用逗号,如列表
ls=[1010,'1010',[1010,'1010'],1010]
print(ls[1:4])#['1010', [1010, '1010'], 1010]
print(ls[-1:-3])#[]  N>M时,返回空列表
print(ls[-3:-1])#['1010', [1010, '1010']]
print(ls[0:4:2])#[1010, [1010, '1010']]

#列表 基本函数
# len(ls)列表ls的元素个数(长度)
#list()转为列表类型
print(list('python'))#['p', 'y', 't', 'h', 'o', 'n']
print(list({'a','b','c'}))#['c', 'b', 'a']
print(list({'a':'1','b':'2','c':'3'}))#['a', 'b', 'c']
'''6.6实例——文本词频统计,并输出前十个高频词 所用数据
A double-deck suspension bridge with the longest span in the world opened to traffic in Wuhan, capital of central China's Hubei Province, on Tuesday.
The first double-deck road bridge over the Yangtze River, with a 1,700-meter-long main span, stretches 4.13 km in total length.
The top deck of the 10th Yangtze River bridge has six lanes with a designed speed of 80 kph while the bottom deck also has six lanes but with a designed speed of 60 kph.
On the top deck there are also two sightseeing sidewalks and on the bottom deck there are two cycle ways together with two sidewalks.
"The Yangsigang Yangtze River Bridge is the world's longest-spanning double-deck suspension bridge," said Xu Gongyi, chief designer of the structure.
'''
#获取文本
def getText():
    txt=open('data.txt','r').read()
    txt=txt.lower()
    for ch in '!@#$%^&*()_+-=[]\;''",./<>?{}':
        txt=txt.replace(ch,' ')#将上述特殊字符替换为空格
    return txt
lizi=getText()#调用“获取文本”函数
words=lizi.split()#分词,split()默认以空格分割

#计数
counts={}
for word in words:
    if word in counts:
        counts[word]+=1
    else:
        counts[word]=1
    #上述循环的简洁表达:counts[word]=counts.get(word,0)+1

#词频排序
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
#lambda保留字:用于定义匿名函数,超纲了。只要记住sort这行用于对第二列排序

#遍历输出
for i in range(10):
    word,count=items[i]
    print("{0:<10}{1:>5}".format(word,count))

#第六章习题
#1、英文字符频率统计。编程统计字符串中 a~z字母 出现的频率,忽略大小写,降序输出。
p=input('请输入字符串:')
words=[]
#逐个字母判断,转为小写
for i in p:
      if 'a'<=i<='z' or 'A'<=i<='Z':
            i=i.lower()
            words.append(i)
counts={}

#统计词频
for word in words:
      counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
print(len(items))

#格式化输出
for i in range(len(items)):
      word,count=items[i]
      print('{0:<10}{1:>5}'.format(word,count))


#2、中文字符频率统计,降序输出。
p=input('请输入字符串:')
words=[]
for i in p:
    words.append(i)
'''中英文通用
for i in p:
      if 'a'<=i<='z' or 'A'<=i<='Z':
            i=i.lower()
            words.append(i)
      else:
            words.append(i)
'''
counts={}
for word in words:
      counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
print(len(items))
for i in range(len(items)):
      word,count=items[i]
      print('{0:<10}{1:>5}'.format(word,count))


#3、随机密码生成,由字母和数字组成的8位密码,十个。
import random
m=[chr(i) for i in range(ord('a'),ord('z')+1)]#小写字母
n=[chr(i) for i in range(ord('A'),ord('Z')+1)]#大写字母
c=m+n

for i in range(1,10):#数字
      c.append(str(i))
string=''

for i in range(10):#大小写字母+数字
      for j in range(8):
            string+=random.choice(c)
      print(string)
      string=''


#4、5、重复元素判定:若无重复数据,则返回True;否则,返回False
def True_False(li):
    if len(set(li)) == len(li):#利用集合的无重复性,使代码更简洁
        return True
    else:
        return False

p = [1, 2, 'sd',5, 4, 1]
print(True_False(p))

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值