由买买提看人间百态

topics

全部话题 - 话题: takewhile
(共0页)
r****t
发帖数: 10904
1
itertools 的 takewhile 和 dropwhile 对这个问题也很好用
def n_common(str1, str2):
it = takewhile(lambda x: x[0]==x[1], izip(str1, str2))
return len(list(it))
idx1 = n_common(str1, str2)
idx2 = -n_common(str1[::-1], str2[::-1])
这样应该就行了
>>> str2[idx1:idx2]
'MNO'
A*******t
发帖数: 443
2
来自主题: Programming版 - scala写个loop老难了
如果想要每一个元素占一行的话,这么写
mapM_ print (takeWhile (<10000) [x^2 | x <-[1..]])
or
mapM_ print $ takeWhile (<10000) [x^2 | x <-[1..]]
a*****e
发帖数: 1700
3
来自主题: Programming版 - scala写个loop老难了
看你要这个循环来干什么了,Haskell 里面也就是一句话的事儿
let l = 1 : map (*m) l in takeWhile (<=n) l
A*******t
发帖数: 443
4
来自主题: Programming版 - scala写个loop老难了
haskell下面就一行
someFunction n = takeWhile (<=n) [2^x | x <- [0..]]
对不起,不太会Scala
A*******t
发帖数: 443
5
来自主题: Programming版 - scala写个loop老难了
print $ takeWhile (<10000) [x^2 | x <-[1..]]
(共0页)