据说是面试题

如题

听说是面试题,我就把课上听到的一些题总结,如下

1. 一行代码换值,python容错高

1一行代码交换ab值
a = 1
b = 2
a, b = b, a
print(a, b)
2. 自动识别
a, b = [1, 2]
print(a, b)
a, b = (1, 20)
print(a, b)
a, b = [1, 2], ['dsd', 3]
print(a, b)

2.计算字符串中整数的个数

# 计算字符串中整数的个数
info = '1dsd12a1'
for i in info:
    if i.isalpha():
        info = info.replace(i, ' ')
l = info.split()
print(len(l))
content = 'sd2d1dsdsda1 fdsdfasf11dw'
# content = content + 's'
count = 0
dig_count = 0
while count < len(content):
    if content[count].isdigit():
        while content[count].isdigit():
            count += 1
            if count == len(content):
                break
        dig_count += 1
    else:
        count += 1
print(dig_count)

猜你喜欢

转载自www.cnblogs.com/wan2-0/p/10547290.html