PTA的Python练习题(五)

昨天耽搁了一天,今天继续

从  第3章-5 字符转换 开始

1.

a=input()
b=""
for i in a:
    if i >='0' and i <='9':
        b=b+iprint(int(b))
#(写成b=i+b会有倒置输出的问题)

2.

知识点:

python 列表统计元素频数、频率

描述

count() 方法用于统计某个元素在列表中出现的次数。

语法

count()方法语法:list.count(obj)

看这网上也没有简单易懂的答案,都是拿着字典算的题目,就拿着别人的代码修改:

这里def两个函数,一个计算出现频率最多的数字,另一个统计频率,最后一起调用print出来

def max_list(lt):
    temp=0
    for i in lt:
        if lt.count(i) > temp:
            max_str = i
            temp = lt.count(i)
    return max_str
def max_count(lt):
    a=0
    for i in lt:
        if lt.count(i) > a:
            max_str = i
            a = lt.count(i)
    return a

n = input().split()
print(max_list(n),max_count(n))

3.

 知识点:

max() 方法返回给定参数的最大值,参数可以为序列

n = int(input())
nums = list( map(int, input().split()[:n]   )  )
mmax = max(nums)
index = -1
for i in range(n):
    if nums[i] == mmax:
        index = i
        break

print("%d %d" % (mmax, index))

猜你喜欢

转载自www.cnblogs.com/echoDetected/p/12275740.html