大鹏

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

内置函数

1、Int的用法

import re
def change_ipaddr(ip):
    ip = ip.split(".")
    res = ''
    for i in ip:
        i = bin(int(i))[2:].rjust(8,'0')
        res +=i
    return int(res,2)

def change_number(number):
    ip_list = []
    data = bin(int(number))[2:].rjust(32,'0')
    data = re.findall(r'.{8}',data)
    for i in data:
        ip_list.append(str(int(i,2)))
    return '.'.join(ip_list)
while True:
    try:
        num = change_ipaddr(input())
        ip = change_number(input())
        print(num)
        print(ip)
    except:
        break
Int的用法
#将数字字符串,通过int来运算
print(int('1101',2))  #13  --字符使用二进制转为10进制

print(int('1101',8)) #577

print(int('1101',10)) #1101

print(int('1101',16))  #4353

int(x, base=10) #base=2|8|10|16
将数字字符串,通过int来运算

 

2、map的用法

mList = (map(int, input().split()))
print(mList)

"""
输入:3 4 3
输出:<map object at 0x00000000028A4C10>
"""

mList = list(map(int, input().split()))
print(mList)

"""
输入:3 4 3
输出:[3, 4, 3]
"""
map用法要点
n = int(input())
mList = list(map(int, input().split()))
xList = list(map(int, input().split()))
lst = []

print(mList)
print(xList)

for i in range(n):
    lst+=[mList[i]]*xList[i] #这里没有想到要用列表的链接
print(lst) #所有的法码

weights = {0}  #这个重量的组合
for a in lst:
    for b in list(weights):
        weights.add(a+b)
print(len(weights))
print(weights)
#####################################################
while True:
    try:
        n = int(input())
        mList = list(map(int, input().split()))
        xList = list(map(int, input().split()))
        lst = []
        for i in zip(mList, xList):
            lst += [i[0]] * i[1]
        weights = {0}
        for i in lst:
            for j in list(weights):
                weights.add(i+j)
        print(len(weights))
    except:
        break
###################################################################

n = int(input())
m = list(map(int, input().split()))
x = list(map(int, input().split()))

map_ = [[m[i] * j for j in range(x[i]+1)] for i in range(n)]
# print(map_)

if n == 1:
    print(len(map_[0]))
else:
    r = set(map_[0])  # 这里如果使用列表,会内存超限
    i = 1
    while i < n:
        r2 = set()
        for j in r:
            for k in map_[i]:
                r2.add(j + k)
        r |= r2
        i += 1
    print(len(set(r)))


###################################################################
HJ41

 

 3、time_list = sorted(dict1.values(), reverse=True)

def strBeauty(s):
    beauty = 0
    dict1 = {}
    # 字典放名字中每种字母对应出现到次数
    for a in set(s):
        k = s.count(a)
        dict1[a] = k
    # print(dict1)

    # 每种字母的出现次数从大到小排列
    time_list = sorted(dict1.values(), reverse=True)
    # print(time_list)

    # 次数从大到小以此乘以26,25,24...
    for i in range(len(time_list)):
        beauty += (26 - i) * time_list[i]
    return beauty


while True:
    try:
        n = int(input())
        sa, sb = input(), input()
        print(strBeauty(sa))
        print(strBeauty(sb))
    except:
        break
sorted倒排字典的健值

 

 

 

 

 

 

 

 

 

 

参考文档:

   1、https://docs.python.org/3/library/functions.html#object

   2、python之路——内置函数和匿名函数

 

posted on 2022-04-28 06:55  pf42280  阅读(24)  评论(0编辑  收藏  举报