Elementary praticing

1.计算数字最后有几个零

(1)字符串函数rstrip():删除字符串末尾指定字符

def end_zeros(num:int) -> int:
	return len(s:=str(num))-len(s.rstrip('0'))

(2)re.search():使用正则表达式

def end_zeros(num:int) -> int:
    import re
    return len(re.search('0*$', str(num)).group())

2. 字符串转置

(1)字符串的切片:[开始索引:结束索引:步长]

def end_zeros(val:str) -> str:
	return val[::-1]

(2)列表函数insert():指定位置插入

def end_zeros(val:str) -> str:
	new = []
	for i in val:
		new.insert(0,i)
	return ''.join(new)

3.列表截取

For the illustration we have a list [1, 2, 3, 4, 5] and we need to remove all elements that go before 3 - which is 1 and 2.
We have two edge cases here: (1) if a cutting element cannot be found, then the list shoudn’t be changed. (2) if the list is empty, then it should remain empty.
Input: List and the border element.
Output: Iterable (tuple, list, iterator …).

def remove_all_before(items, border):
    try:
        return items[items.index(border):]
    except ValueError:
        return items
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值