# 数据分组操作

代码：

```python
from itertools import groupby
from operator import itemgetter

# 希望按日期分组,(如果things中分组key不连续，需要先排序，因为groupby是按照连续项进行分组)
things=[('2012-05-21',11),('2012-05-21',3),
        ('2012-05-22',10),('2012-05-22',4),('2012-05-22',22),
        ('2012-05-23',33)]

#日期，itemgetter(0)为group key，此时要保证其是排序好的
for key ,items in groupby(things,itemgetter(0)):
    print('group key:',key)
    print('items:')
    for subitem in items:
        print(subitem)
    print('-'*20)
```

结果：

```python
group key: 2012-05-21
items:
('2012-05-21', 11)
('2012-05-21', 3)
--------------------
group key: 2012-05-22
items:
('2012-05-22', 10)
('2012-05-22', 4)
('2012-05-22', 22)
--------------------
group key: 2012-05-23
items:
('2012-05-23', 33)
--------------------
```

视频：

```
wait
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/2.3.9-fen-zu-cao-zuo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
