惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
Vercel News
Vercel News
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG

博客园 - J.D Huang

TinyDO:可能是WP首个支持中文语音识别的待办事项APP Internet TV 影音娱乐新生活 Windows Phone Developer Tools CTP 发布了! Python - 默认参数的一次性求值 Python小技巧 - 子串查找 - J.D Huang 新的个人博客@ http://thinkbot.info MeeGo:下一个Android? Windows Mobile 6.5.3 Developer Tool Kit 发布了 塞班(Symbian)开源了(包括Symbian 3和S60等) Windows Mobile 6.5 SDK 发布了 (2月17日更新) C#4.0新特性之(四)新的LINQ扩展方法-Zip() C#4.0新特性之(三)协变与逆变 C#4.0新特性之(二)命名参数,可选参数与COM互操作 C#4.0新特性之(一)动态查找 Android手机防盗工具DroidGuard Simple HostMonitor 实用的网管小工具 Office Mobile 2010 Beta 发布了! - J.D Huang 试了一下.Net Fx 4.0中的Parallel - J.D Huang Lambda演算与科里化(Currying)
Python小技巧 – True or False
J.D Huang · 2010-03-04 · via 博客园 - J.D Huang

  在昨天关于substring的blog中有如下一段代码:

image

也许你已经发现,在Python 3中其实有办法只用一行完成函数:

>>> def isSubstring2(s1,s2):
	return True if s2.find(s1)!=-1 else False

但是。。。还可以更简单吗?

如何更简单使用Python表达条件语句呢,just for fun :)

一种做法是使用列表索引:

>>> def isSubstring2(s1,s2):
	return [False,True][s2.find(s1)!=-1]

原理很简单,布尔值True被索引求值为1,而False就等于0. 还可以更简单么?留言给我吧 ;-)