使用python函数 实现事务数据转化为事务表
时间: 2023-05-30 21:04:56 浏览: 69
假设我们有如下的事务数据:
```
T1: A, B, C
T2: A, C, D
T3: B, C, E
```
我们可以使用Python函数将其转化为事务表:
```python
def transaction_to_table(transactions):
items = set()
for transaction in transactions:
for item in transaction:
items.add(item)
items = sorted(list(items))
table = []
for transaction in transactions:
row = []
for item in items:
if item in transaction:
row.append(1)
else:
row.append(0)
table.append(row)
return table
```
使用该函数,我们可以将上述事务数据转化为事务表:
```python
transactions = [
['A', 'B', 'C'],
['A', 'C', 'D'],
['B', 'C', 'E']
]
table = transaction_to_table(transactions)
print(table)
```
输出结果为:
```
[[1, 1, 1, 0, 0], [1, 0, 1, 1, 0], [0, 1, 1, 0, 1]]
```
其中,每一行表示一个事务,每一列表示一个事务项,1表示该事务包含该事务项,0表示该事务不包含该事务项。