Lambda表达式和字符串

时间:2019-07-02 00:51:56

标签: python lambda

我只是在学习lambda表达式,我想知道如何使用它来计算字符串中的元音数量。例如, 我正在使用以下内容:

result = lambda i, y: i + 1 for x in y if x in "aeoiuAEIOU"
print(result(0,s)

但是,我收到语法错误。 另外,为了澄清我的理解,lambda表达式返回冒号后面的表达式。有人可以指出正确的方向吗?

4 个答案:

答案 0 :(得分:2)

这只是模棱两可的解析。考虑

result = lambda i, y: (i + 1 for x in y if x in "aeoiuAEIOU")

当然,现在它将返回一个生成器对象,该对象不会很好地显示。如果您想要列表,可以尝试

result = lambda i, y: [i + 1 for x in y if x in "aeoiuAEIOU"]

答案 1 :(得分:1)

对我来说,这是对library(data.table) library(microbenchmark) library(dplyr) df1 <- structure(list(Patent = c("4683202", "4683195", "4800159", "4965188", "4994368", "5328824", "4879214", "4921794", "4983728", "4994372", "5066584", "5075216", "5091310", "5093245", "5132215", "5185243", "5409818", "5409818", "6107023", "4994370", "5001050", "5023171", "5035996", "5035996", "5043272", "5045450", "5055393", "5085983", "5106729", "5106729"), subclass = c("435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.21", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.21", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.2", "435/91.21"), AppYear = c(1985L, 1986L, 1986L, 1987L, 1987L, 1987L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L) ) , class = "data.frame" , row.names = c(NA, -30L) , .Names = c("Patent", "subclass", "AppYear")) # dt join method ---------------------------------------------------------- dt <- as.data.table(df1) setorder(dt, AppYear, Patent) dt[, app := rleid(AppYear), by = .(subclass)] dt[unique(dt[, .(lagging_year = shift(AppYear) ,lagging_app = shift(app) + 1), by = subclass]) , on = .(subclass ,app = lagging_app ) , lag_year := lagging_year] dt # dt rep rleid method ----------------------------------------------------------- dt <- as.data.table(df1) setorder(dt, AppYear, Patent) dt[, app := rleid(AppYear), by = .(subclass)] dt[, app_cnt := .N, by = .( AppYear, subclass)] dt[, lag_year := rep(shift(unique(AppYear)), unique(app_cnt)), by = .(subclass)] dt[, app_cnt := NULL] dt # dt rep and .GRP --------------------------------------------------------- dt <- as.data.table(df1) setorder(dt, AppYear, Patent) dt[, `:=` (app = .GRP, app_cnt = .N), by = .( AppYear, subclass)] dt[, lag_year := rep(shift(unique(AppYear)), unique(app_cnt)), by = .(subclass)] dt[, app_cnt := NULL] dt # benchmarks -------------------------------------------------------------- microbenchmark(M_M_dplyr = { df1 %>% arrange(AppYear, Patent) %>% group_by(subclass) %>% group_map(~mutate(.,app=group_indices(.,AppYear), lag_year = rep(lag(unique(.$AppYear)), count_(., "AppYear")$n)), keep = T) %>% bind_rows() %>% arrange(AppYear, Patent) } , cole_dt_join = { dt <- as.data.table(df1) setorder(dt, AppYear, Patent) dt[, app := rleid(AppYear), by = .(subclass)] dt[unique(dt[, .(lagging_year = shift(AppYear) ,lagging_app = shift(app) + 1), by = subclass]) , on = .(subclass ,app = lagging_app ) , lag_year := lagging_year] } ,cole_dt_rep = { dt <- as.data.table(df1) setorder(dt, AppYear, Patent) dt[, `:=` (app = .GRP, app_cnt = .N), by = .( AppYear, subclass)] dt[, lag_year := rep(shift(unique(AppYear)), unique(app_cnt)), by = .(subclass)] dt[, app_cnt := NULL] } ,cole_dt_rep_rleid = { dt <- as.data.table(df1) dt[, app := rleid(AppYear), by = .(subclass)] dt[, app_cnt := .N, by = .( AppYear, subclass)] dt[, lag_year := rep(shift(unique(AppYear)), unique(app_cnt)), by = .(subclass)] dt[, app_cnt := NULL] } ) 的不当使用,因为lambda被认为是没有名称或多或少具有抛弃功能的函数定义。另外,出现语法错误的原因是生成器表达式不正确,请对此类使用lambda

sum

您甚至可以为此使用地图:

result = lambda y: sum(1 for x in y if x in "aeoiuAEIOU")

但是使用实际的函数定义可以更清晰,更正确地执行imo,并且可以包含更多语句

result = lambda y: sum(map(y.count, "aeoiuAEIOU"))

您也可以为此使用#more appropriately named count_vowels vs result and s vs y def count_vowels(s): _s = s.lower() return sum(map(_s.count, 'aeiou') (regex)

re

import re def count_vowels(s): return len(re.findall("[aeiou]", s, re.I)) 忽略大小写,因此无需担心capsvs取消上限

答案 2 :(得分:1)

谢谢西尔维奥(Silvio)的帮助!像您建议的那样填充一个列表,这给了我一个答案,答案并不尽如人意

[1, 1, 1, 1, 1]

生成器返回时

<generator object <lambda>.<locals>.<genexpr> at 0x0000028884166C00>

最后,我使用生成器上的sum来获得

result = lambda i, y: sum(i + 1 for x in y if x in "aeoiuAEIOU")
print(result(0, s))

哪个给了我5的答案。

答案 3 :(得分:1)

您也可以只在lambda中使用一个参数:

result = lambda y: sum(x in "aeoiuAEIOU" for x in y)

并且语法错误在lambda中不是特殊的:

In [10]: def bar(i, y):
    ...:     return i + 1 for x in y if x in "aeoiuAEIOU"
  File "<ipython-input-10-96fcd0959145>", line 2
    return i + 1 for x in y if x in "aeoiuAEIOU"
                   ^
SyntaxError: invalid syntax

真正的问题是i + 1 for x in y if x in "aeoiuAEIOU"在Python中是无效的表达式。