Код: Выделить всё
+------------+-------------------+---------+--------+----------+
| process_no | process_durations | columns | orders | customer |
+------------+-------------------+---------+--------+----------+
| 0 | 3 | [0] | [3109] | [0] |
+------------+-------------------+---------+--------+----------+
| 1 | 100 | [11] | [5855] | [0] |
+------------+-------------------+---------+--------+----------+
| 2 | 81 | [8] | [5304] | [0] |
+------------+-------------------+---------+--------+----------+
Код: Выделить всё
from itertools import combinations
from operator import itemgetter
data = pd.DataFrame({
'process_no': [0, 1, 2],
'process_durations': [3, 100, 81],
'columns': [[0], [11], [8]],
'orders': [[3109], [5855], [5304]],
'customer': [[0], [0], [0]]
})
vals = data.values.tolist()
cross_combine = list(combinations(vals, r=2))
sorted_cross_combine = sorted(
[
(
x,
-(len(x[0][3]) + len(x[1][3])),
len(set(x[0][2] + x[1][2])),
list(set(x[0][4] + x[1][4]))
)
for x in cross_combine
],
key=itemgetter(1, 2)
)
print(sorted_cross_combine)
[(([0, 3, [0], [3109], [0]], [1, 100, [11], [5855], [0]]), -2, 2, [0]),
(([0, 3, [0], [3109], [0]], [2, 81, [8], [5304], [0]]), -2, 2, [0]),
(([1, 100, [11], [5855], [0]], [2, 81, [8], [5304], [0]]), -2, 2, [0])]
Код: Выделить всё
+-----------------------------+-----------------------------+-------------+--------------+-----------+
| x1 | x2 |order_length |column_length | customers |
+-----------------------------+-----------------------------+-------------+--------------+-----------+
| [0, 3, [0], [3109], [0]] | [1, 100, [11], [5855], [0]] | -2 | 2 | [0] |
+-----------------------------+-----------------------------+-------------+--------------+-----------+
| [0, 3, [0], [3109], [0]] | [2, 81, [8], [5304], [0]] | -2 | 2 | [0] |
+-----------------------------+-----------------------------+-------------+--------------+-----------+
| [1, 100, [11], [5855], [0]] | [2, 81, [8], [5304], [0]] | -2 | 2 | [0] |
+-----------------------------+-----------------------------+-------------+--------------+-----------+
Код: Выделить всё
orders => [3109] and [5855]
columns => [0] and [11]
customer => [0] and [0]
Код: Выделить всё
order_length => -len([3109] + [5855])
column_length => len(set([0] + [1]))
customers => list(set([0] + [0]))
Могу ли я сделать это, пока процесс объединения еще продолжается, при условии, что он более эффективен? Например, я знаю, что такой функции нет, но представляю себе что-то вроде этого:
Код: Выделить всё
def calc(x, y):
return (
x + y,
-(len(x[3]) + len(y[3])),
len(set(x[2] + y[2])),
list(set(x[4] + y[4]))
)
cross_combine = list(combinations(vals, r=2, func=calc))
Вы можете скачать примеры данных по ссылке. Вам необходимо привести тип данных следующим образом:
Код: Выделить всё
import ast
data = pd.read_csv('a.csv')
for col in ['columns', 'orders', 'customer']:
data[col] = data[col].apply(ast.literal_eval)
Подробнее здесь: https://stackoverflow.com/questions/785 ... t-with-its