我将如何按照两个不同的字段对对象列表进行分组?
例如
struct Item
{
public string Name;
public int Age;
public int Score;
}
List<Item> items = new List<Item>();
items.Add(new Item(){ "Jeff", 35, 50};
items.Add(new Item(){ "Annie", 22, 45};
items.Add(new Item(){ "Pierce", 60, 90};
items.Add(new Item(){ "Pierce", 60, 10};
items.Add(new Item(){ "Abed", 28, 22};
items.Add(new Item(){ "Troy", 22, 45};
items.Add(new Item(){ "Troy", 22, 65};
items.Add(new Item(){ "Troy", 22, 80};
items.Add(new Item(){ "Troy", 21, 2};
items.Add(new Item(){ "Troy", 21, 5};
var grouped = items.GroupBy(/*Group by age then name*/);
Run Code Online (Sandbox Code Playgroud)
分组后将是:
"Troy", …
Run Code Online (Sandbox Code Playgroud) 我试图通过对几列进行分组来创建数据透视表:用户 ID、姓名、周数和日期名称。当前的请求没有给出期望的结果。我需要帮助。
这是我的桌子:
user_id name week_number day_name price
2 Luc 8 Sunday 10
2 Luc 8 Monday 15
2 Luc 8 Tuesday 8
2 Luc 8 Wednesday 2
2 Luc 8 Thursday 9
2 Luc 8 Friday 9
2 Luc 8 Saturday 11
2 Luc 9 Saturday 1
2 Luc 9 Friday 13
3 Mathieu 8 Sunday 22
3 Mathieu 8 Monday 13
3 Mathieu 8 Tuesday 9
3 Mathieu 8 Wednesday 3
Run Code Online (Sandbox Code Playgroud)
这是我当前的请求:
SELECT *
FROM crosstab(
'SELECT user_id, …
Run Code Online (Sandbox Code Playgroud) 如何按一系列可能的值对列表进行分组?如果我有一个类 Foo
class Foo{
int value;
}
Run Code Online (Sandbox Code Playgroud)
和一个 foosList<Foo>
列表然后
list.stream().collect(Collectors.groupingBy(Foo::getValue));
Run Code Online (Sandbox Code Playgroud)
将我的具有相同价值的 foos 收集到一组中。但我想将一系列值归为一组。例如
values < 5 in group "A"
values > 5 && < 25 in group "B"
values >= 25 in group "C"
Run Code Online (Sandbox Code Playgroud)
或者另一个例子:假设我有一个整数列表:
List<Integer> list = List.of(2,3,4,9,11,17,28,29,32);
list.stream().collect(Collectors.groupingBy(
classifier,
Collectors.toList()));
Run Code Online (Sandbox Code Playgroud)
我试图把类似的东西放到分类器
i < 5 ? "A": i >= 5 && i < 25 ? "B" : "C";
Run Code Online (Sandbox Code Playgroud)
这给出了编译错误。怎么做?
我想分析一个日志文件。它有几个操作,每个操作包含一组子操作。我想提取按操作分组的子操作数。这在 sql 中很容易,但我在 bash 中陷入困境。
这是该文件的简化版本:
[21:30:21.538Z #a9a.012 DEBUG - - ] c.h.c.w.j.JobTrackingWorkerReporter: Reporting bulk completion: Partition: tenant-xla; Job: ingestion-4759-9-13-41; Tasks: [ingestion-4759-9-13-41.1.43, ingestion-4759-9-13-41.1.44, ingestion-4759-9-13-41.1.41]
otherlogs stuff ...
[21:31:21.538Z #a9a.012 DEBUG - - ] c.h.c.w.j.JobTrackingWorkerReporter: Reporting bulk completion: Partition: tenant-xla; Job: ingestion-4757-10-17-4; Tasks: [ingestion-4757-10-17-4.1.2, ingestion-4757-10-17-4.1.1, ingestion-4757-10-17-4.1.3, ingestion-4757-10-17-4.1.4]
otherlogs stuff ...
[21:31:21.690Z #a9a.012 DEBUG - - ] c.h.c.w.j.JobTrackingWorkerReporter: Reporting bulk completion: Partition: tenant-xla; Job: ingestion-4757-10-18-3; Tasks: [ingestion-4757-10-18-3.1.137, ingestion-4757-10-18-3.1.139, ingestion-4757-10-18-3.1.138, ingestion-4757-10-18-3.1.140, ingestion-4757-10-18-3.1.136, ingestion-4757-10-18-3.1.141]
Run Code Online (Sandbox Code Playgroud)
每个操作都是点之前的部分,其余部分属于任何子操作。
我正在寻找类似以下的结果,例如,我可以将其存储在文件中:
operationName suboperationCount
ingestion-4757-10-18-3 3
ingestion-4757-10-18-4 4
ingestion-4757-10-18-3 6 …
Run Code Online (Sandbox Code Playgroud) 我有对象数组
{
"agent_name": "AgentName",
"analytics": [
{
"date": "Tue, 1 Aug 2021 00:00:00 GMT",
"intents_count":[
{
"count": 5,
"intent": "intent1"
},
{
"count": 1,
"intent": "intent2"
},
{
"count": 0,
"intent": "intent3"
},
]
},
{
"date": "Tue, 2 Aug 2021 00:00:00 GMT",
"intents_count":[
{
"count": 5,
"intent": "intent1"
},
{
"count": 1,
"intent": "intent2"
},
{
"count": 0,
"intent": "intent3"
},
]
},
... the same for the next days of month
]
}
Run Code Online (Sandbox Code Playgroud)
我需要获取按日期分组的每个意图的计数总和。
结果应该是这样的:
[10, 2, 0] …
Run Code Online (Sandbox Code Playgroud) 我是 KDB 的新手,正在努力创建查询。将不胜感激任何帮助。
我有一个字符串表,需要获取表中所有字符串中所有特定子字符串的计数。
所以,我们假设我有字符串:
[
string1: Apple is green, cherry is red,
string2: Ququmber is green, banana is yellow
]
Run Code Online (Sandbox Code Playgroud)
我想计算所有子字符串中“Apple”和“green”的数量。我想要的结果是像这样进行分组:
{
Apple: 1,
green: 2
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我不知道如何进行这样的分组。我已经弄清楚如何获取至少包含一个所需子字符串的字符串:
"select count(text) from data where any text like/: (\"*$Apple*\";\"*$green*\")"
Run Code Online (Sandbox Code Playgroud)
但这返回了所有找到的 Apple 和 green 字符串的累积结果,没有任何分组:
{
text: 3
}
Run Code Online (Sandbox Code Playgroud)
它不允许区分每个特定子串的数量。
我将非常感谢任何帮助。
我正在研究一个项目,我坚持这个我的问题是我有一个数组,如下所示
$arr1 = array(
array
(
'id' => '1',
'city' => 'A.bad',
),
array
(
'id' => '2',
'city' => 'Pune',
),
array
(
'id' => '1',
'city' => 'Mumbai',
)
);
Run Code Online (Sandbox Code Playgroud)
我必须通过id比较这个,我想要输出如下.
$result = array(
array(
'id'='1',
'city'='A.bad','Mumbai'
),
array(
'id'='2',
'city'='Pune'
)
);
Run Code Online (Sandbox Code Playgroud)
如果我们拥有与第一个相同的ID,A.bad
那么它将采用它,而在第三个中它具有id 1和city,mumbai
因此它将组合为id 1和city as a.bad
,mumbai
并且其他记录以相同的方式过滤.
我的 groupingBy 方法有问题。我举个例子:假设有一个List<Person>
wherePerson.class
和City.class
are like this
class Person{
String name;
String surname;
City city;
}
class City{
String name;
String postalCode;
}
Run Code Online (Sandbox Code Playgroud)
我会将列表分组city.postalcode
并执行如下操作:
List<Person> myList = ...some persons here...
Map<City,List<Person>> groupMap = myList.stream().collect(
Collectors.groupingBy(person -> person));
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为groupingBy
不知道如何按对象分组。我会做类似的事情Collectors.groupingBy(person -> person.city.postalcode)
,但是这样会产生一个类型的地图Map<String,List<Person>>
,而不是 Map<City,List<Person>>
谢谢你的帮助。
更新
为了groupingBy
在类上使用,它必须简单地正确实现 equals
并且hashcode
我想按键对地图对象进行分组。我尝试使用此代码,但出现编译错误:
Non-static method cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我的代码:
Map<String, List<A>> getAMap() {
return Arrays.stream(SomeArray.values())
.map(map -> createObject())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
private Map<String, A> createObject()
final A a = new A(some attributes);
Map<String, A> map = new LinkedHashMap<>();
map.put(some key, a);
.... // add another values.
return map;
}
Run Code Online (Sandbox Code Playgroud)
我需要类似的东西
{
"a", {a1, a2, a3},
"b", {a4, a5, a6},
}
Run Code Online (Sandbox Code Playgroud) 我希望与之相反flatMap
.我有以下代码:
List<String> list = Arrays.asList("A", "C", "B", "AA", "BB", "CC");
Stream<String> stream = list.stream();
Collection<List<String>> val = stream.sorted().collect(groupingBy(String::length)).values();
System.out.println("Values " + val);
Run Code Online (Sandbox Code Playgroud)
这确实有效,但类型是Collection<List<String>>
.我怎么做到的List<List<String>>
?
我可以迭代Collection
或使用new Arraylist()
.我正在寻找Arrays.asList
更简洁和不可变的东西.
如果我演员,我得到java.lang.ClassCastException
.
此外,这将对整个列表进行排序,然后进行分组.有没有办法先分组,然后对较小的列表进行排序,这样会更有效率.
这个想法基本上是在数组中没有相似值的重复值。
示例数组包含值
input = [1,2,2,2,2,3,4,5,6,7,8,9]<br/>
Run Code Online (Sandbox Code Playgroud)
预期输出有一些东西
likeoutput = [1,2,3,2,4,2,5,2,6,2,7,8,9]<br/>
Run Code Online (Sandbox Code Playgroud)
我尝试将其放在for循环中,在其中检查下一项,如果相同,则交换值。问题是当我有连续的相似值时。
我有一个像这样的对象数组:
entries: [
{
id:1,
text: "Lorem ipsum",
timeStamp:"Thu, 01 Jun 2018"
},
{
id:3,
text:"Lorem ipsum",
timeStamp:"Thu, 24 May 2018"
},
{
id:4,
text: "Lorem ipsum",
timeStamp:"Thu, 24 May 2018"
}
]
Run Code Online (Sandbox Code Playgroud)
现在我希望能够将它们分组到“存档”数组中,如下所示:
archive: [
{
monthyear: May 2018,
entries: 2
},
{
monthyear: June 2018,
entries: 5
}
]
Run Code Online (Sandbox Code Playgroud)
想知道我应该使用什么样的数组函数来获得预期的结果。
在datafreame中,我有2个变量,一个是发送的免费样本数,另一个是产生的购买数.我想将自由样本变量分组为0,1到5,5到10,大于10的间隔.然后累计来自购买数列的观察结果,每个间隔以表格形式呈现.
任何帮助将不胜感激
grouping ×13
arrays ×4
java ×4
java-stream ×3
javascript ×3
count ×2
java-8 ×2
.net ×1
aggregate ×1
awk ×1
bash ×1
c# ×1
collectors ×1
crosstab ×1
grep ×1
intervals ×1
kdb ×1
lambda ×1
linq ×1
list ×1
lodash ×1
php ×1
pivot ×1
pivot-table ×1
postgresql ×1
r ×1
select ×1
sorting ×1
substring ×1
typescript ×1