查看原文
其他

有趣的超短Python代码

让列表的每个元素都乘以2

print (list(map(lambda x: x*2,range(1,11))))


求列表中所有元素之和

print (sum(range(1,100))) #range(1,100)代表从1100(不包含100)


判断一个字符串中是否存在某些词

wordlist = ["scala","akka","play framework","sbt","typesafe"]
tweet = "This is an example tweet talking about scala an sbt"
print (list(map(lambda x:x in tweet.split(),wordlist)))

Python split()通过指定分隔符对字符串进行切片,这里默认为空格


读取txt文件

for line in open("D:\\test.txt"):print(line)


祝你生日快乐

print (list(map(lambda x: "happy birthday to "+("YOU" if x!=2 else "dear Name"),range(4))))

本文代码由陈鸥辉老师修改,参考代码:

作者:榴莲Xtt 

参考链接:https://blog.csdn.net/windows_2015/article/details/51893405?utm_source=blogxgwz2

继续滑动看下一个
向上滑动看下一个

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存