字符串变形
swapcase()方法将字符串大小写转换;split()方法将字符串以括号内的符号分隔并以列表形式返回
python">s=input()
l=s.split(" ")
l=l[::-1]
s=""
for i in l:a=i.swapcase()s+=as+=" "
print(s[0:len(s)-1])
压缩字符串
很巧妙的方法
python">def zip(s):lst=[]for i in s:if not lst or lst[-2]!=i:#空列表或者第一次出现lst.append(i)lst.append(1)else:#出现过lst[-1]=lst[-1]+1return lst
s=input()
l=zip(s)
res=""
for i in l:if i!=1:res+=str(i)
print(res)
三个数的最大乘积
两种情况,全是正数时,最大的三个乘积;有正有负时,最小的两个负数和最大的正数乘积
python">lst=[1,7,45,25,12,-28,-15,0,25]
lst.sort()
def mul(l):return max(l[0]*l[1]*l[-1],lst[-1]*lst[-2]*lst[-3])
print(mul(lst))
判定字符是否唯一
python">s=input()
def only(s):for i in range(len(s)):if s[i] in s[i+1:]:return Falsereturn True
print(only(s))
IP地址转换
'{:08b}'.format()实现转2进制高位补0,8表示8位,b表示二进制;int()函数中的2表示2进制,表示把2进制数转换为10进制
python">s=input()
l=s.split(".")
res=""
for i in l:res +='{:08b}'.format(int(i))
print(int(res,2))