请考虑以下课程:
import pandas as pd
from io import StringIO
x = StringIO("""A,apple,102
A,orange,103
B,banana,101
C,peach,102
B,orange,104""")
# read file and drop duplicates, replace x with 'file.csv'
df = pd.read_csv(x, names=['letter', 'fruit', 'value'])\
.drop_duplicates('fruit', keep=False)
# export to output csv
df.to_csv('file_out.csv', index=False, header=False)
print(df)
letter fruit value
0 A apple 102
2 B banana 101
3 C peach 102
如何编写与第一种方法(getRemainingCompanyBalance)相同的Java流“管道”。此代码将导致错误。
该列表包含每个部门的单独列表。子列表中的每个条目都是该类的一个实例。名称/费用:
de#1first” / 9900, de#1second“ / 8700, de#2first” / 8500, de#2second“ / 10000, de#3first” / 7800, de#3second“ / 6900
public class PersonalExpense {
private String name;
private int currentExpenses;
...
我正在尝试收取费用,然后从余额中减去费用
答案 0 :(得分:0)
您收到了一些不必要的方法调用。您不需要过滤Stream
,因为它已经只包含PersonalExpense
实例,并且您不应该将其收集到List
,因为这会阻止您将其映射到IntStream
并计算总和。
public static long getRemainingCompanyBalanceLambda ( long initialBalance, List<ArrayList<PersonalExpense>> total) {
return initialBalance - total
.stream()
.flatMap(Collection::stream)
.mapToInt(PersonalExpense::getCurrentExpenses)
.sum();
}
答案 1 :(得分:0)
public static long getRemainingCompanyBalanceLambda ( long initialBalance, List<ArrayList<PersonalExpense>> total) {
int sum = total.stream()
.flatMap(List::stream)
.mapToInt(PersonalExpense::getCurrentExpenses)
.sum();
return initialBalance - sum;
}