python3版本800行的代码_单行代码 之Python 3.5版

==========================================================================

在网上看到有python2.7的单行代码(链接如上):

自学python3.x中,于是简单的想将2.x版翻译成3.x版。其中第7条实现还未成功。待了解xml后再尝试。

==========================================================================

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

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

==========================================================================

2、求列表中的所有元素之和

print(sum(range(1,101)))

==========================================================================

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

wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"]

tweet= "This is a example tweet talking about scala and sbt"

print(list(map(lambda x: x in tweet.split(),wordlist)))

==========================================================================

5、祝你生日快乐!

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

==========================================================================

6. 过滤列表中的数值

from functools importreduce

reduce(lambda a,c: (a[0]+[c],a[1]) if c > 60 else (a[0],a[1]+[c]), [49, 58, 76, 82, 88, 90],([],[]))

==========================================================================

7. 获取XML web service数据并分析

待定

from xml.dom.minidom importparse, parseStringimporturllib.request#注意,我将它转换成XML格式化并打印出来

print(parse(urllib.request.urlopen("http://search.twitter.com/search.atom?&q=python")).toprettyxml(encoding="utf-8"))

==========================================================================

8. 找到列表中最小或最大的一个数字

print(min([14,35,-7,46,98]))print(max([14,35,-7,46,98]))

==========================================================================

9. 并行处理

importmultiprocessingimportmathprint(list(multiprocessing.Pool(processes=4).map(math.exp,range(1,11))))

==========================================================================

10. “Sieve of Eratosthenes”算法

n = 50 #We want to find prime numbers between 2 and 50

print(sorted(set(range(2,n+1)).difference(set((p * f) for p in range(2,int(n**0.5) + 2) for f in range(2,int(n/p)+1)))))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值