lambda

Pandas: update column values from another column if criteria [duplicate]

北城以北 提交于 2020-12-30 08:56:31
问题 This question already has answers here : Pandas conditional creation of a series/dataframe column (8 answers) Closed 2 years ago . I have a DataFrame: A B 1: 0 1 2: 0 0 3: 1 1 4: 0 1 5: 1 0 I want to update each item column A of the DataFrame with values of column B if value from column A equals 0. DataFrame I want to get: A B 1: 1 1 2: 0 0 3: 1 1 4: 1 1 5: 1 0 I've already tried this code df['A'] = df['B'].apply(lambda x: x if df['A'] == 0 else df['A']) It raise an error : The truth value of

Flattening a list of elements in Java 8 Optional pipeline

做~自己de王妃 提交于 2020-12-29 13:19:54
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Flattening a list of elements in Java 8 Optional pipeline

你说的曾经没有我的故事 提交于 2020-12-29 13:19:12
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

Flattening a list of elements in Java 8 Optional pipeline

泪湿孤枕 提交于 2020-12-29 13:18:37
问题 I have a id value which can be null . Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list. Currently I have this working code Optional.ofNullable(id) .map(id -> service.findTrades(id)) .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst()) .orElse(... default value...); Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step. Initially I expected to

creating unordered_set with lambda

点点圈 提交于 2020-12-29 10:12:34
问题 How can I make unordered_set with lambda? (I know how to make it with user defined hash struct and operator== ) My current code is : #include <unordered_set> #include <functional> struct Point { float x; float y; Point() : x(0), y(0) {} }; int main() { auto hash=[](const Point& pt){ return (size_t)(pt.x*100 + pt.y); }; auto hashFunc=[&hash](){ return std::function<size_t(const Point&)> (hash); }; auto equal=[](const Point& pt1, const Point& pt2){ return ((pt1.x == pt2.x) && (pt1.y == pt2.y));

Java 8 Stream to find element in list

喜夏-厌秋 提交于 2020-12-29 05:52:25
问题 I have the following class: public class Item { int id; String name; // few other fields, contructor, getters and setters } I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams. public void foobar() { List<Item> items = getItemList(); List<Integer> ids = getIdsToLookup(); int id, i = ids.size() - 1; while (i >= 0) { id = ids.get(i); Optional<Item> item = items .stream() .filter(a -> a.getId() == id)

Java 8 Stream to find element in list

夙愿已清 提交于 2020-12-29 05:51:33
问题 I have the following class: public class Item { int id; String name; // few other fields, contructor, getters and setters } I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams. public void foobar() { List<Item> items = getItemList(); List<Integer> ids = getIdsToLookup(); int id, i = ids.size() - 1; while (i >= 0) { id = ids.get(i); Optional<Item> item = items .stream() .filter(a -> a.getId() == id)

Java 8 Stream to find element in list

依然范特西╮ 提交于 2020-12-29 05:51:09
问题 I have the following class: public class Item { int id; String name; // few other fields, contructor, getters and setters } I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams. public void foobar() { List<Item> items = getItemList(); List<Integer> ids = getIdsToLookup(); int id, i = ids.size() - 1; while (i >= 0) { id = ids.get(i); Optional<Item> item = items .stream() .filter(a -> a.getId() == id)

Python intersection with custom equality

微笑、不失礼 提交于 2020-12-29 04:29:25
问题 I want to build the intersection of two python sets, but i need to have a custom equality to do this. Is there a way to specify "equaliy" directly when doing the intersection? For example due to a lambda-expressions? I know there is way by overriding eq , but i have to do several intersections on the same classes with different "equalities". Thanks! 回答1: Preface What you are trying to do makes perfect mathematical sense by using the right terms. The "equalities" you are referring to are

copy-capturing this with C++ lambdas

ぐ巨炮叔叔 提交于 2020-12-27 07:14:47
问题 Reading up on lambdas, I tried understanding them capturing this and *this . I wrote a small test: #include <iostream> #include <functional> using namespace std; struct A { int x = 0; function<int(void)> get_x_cpy = [=, this]() { return x++; }; function<int(void)> get_x_ref = [&, this]() { return x++; }; }; int main() { A a; cout << a.get_x_cpy() << a.get_x_cpy() << a.get_x_ref() << a.get_x_ref() << '\n'; } and, expecting the get_x_cpy() to make a copy of a I expected to see 0001 or 0101