用python写一个代码, 将一个包含设备名称, 租借人, 开始时间, 结束时间的表格中的数据提取出来, 放入一个列表, 列表中设备作为键, 对应的租借人, 开始时间, 结束时间会有多个
时间: 2023-10-23 22:09:58 浏览: 28
好的,可以使用 Python 中的字典来存储这些数据。可以按照如下代码进行实现:
```python
data = [
["设备1", "张三", "2021-01-01", "2021-01-05"],
["设备1", "王五", "2021-01-06", "2021-01-10"],
["设备2", "李四", "2021-01-02", "2021-01-07"],
["设备2", "赵六", "2021-01-08", "2021-01-12"],
["设备3", "小明", "2021-01-03", "2021-01-08"],
]
result = {}
for item in data:
device_name, renter, start_time, end_time = item
if device_name not in result:
result[device_name] = []
result[device_name].append((renter, start_time, end_time))
print(result)
```
其中,`data` 列表中存储了设备名称、租借人、开始时间、结束时间的信息。代码首先初始化一个空字典 `result`,然后遍历 `data` 列表中的每一个元素,将其中的设备名称、租借人、开始时间、结束时间分别存储到变量 `device_name`、`renter`、`start_time`、`end_time` 中。
然后,判断字典 `result` 中是否已经存在以 `device_name` 为键的条目。如果不存在,则创建一个空列表作为该键的值。然后,将 `(renter, start_time, end_time)` 这个元组添加到该列表中。
最后,输出字典 `result`,其中每个键对应的值是一个列表,列表中存储了该设备的所有租借信息。