給定一個 tuple items,我得到了 powerset itertools:
items = tuple(('a', 'b', 'c'))
combos = powerset(items)
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return tuple(chain.from_iterable(combinations(s, r) for r in range(1, len(s) 1)))
我的目標是轉換 powerset 輸出combos:
#combos =
(('a',),
('b',),
('c',),
('a', 'b'),
('a', 'c'),
('b', 'c'),
('a', 'b', 'c'))
成np.array對于每一行,其中,每個物品放置在與該專案對應的原始元組中的位置的列cols。
想要的結果:
#res =
[['a', '', ''],
['', 'b', ''],
['', '', 'c'],
['a', 'b', ''],
['a', '', 'c'],
['', 'b', 'c'],
['a', 'b', 'c']]
#shape = (7,3)
我能夠使用下面的代碼實作這一點。但是,與我嘗試遍歷陣列并將每個單獨的專案放在結果陣列中的正確列中相比,是否有一種更 Pythonic、更快/記憶體效率更高的方式來轉換輸出?
完整代碼:
from itertools import combinations, chain
import numpy as np
def columnizePowerset(powerset, items):
combo_arr = np.array([list(x) for x in powerset], dtype=object) # Convert to np.array
res = np.empty([len(powerset), len(items)], dtype=str) # Initialize result array
for i in range(len(combo_arr)): # Loop through rows
for j in range(len(combo_arr[i])): # Loop through items in row
col = np.where(np.array(items) == combo_arr[i][j]) # Identify column for item
res[i][col] = combo_arr[i][j] # Place item in correct column
return res
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return tuple(chain.from_iterable(combinations(s, r) for r in range(1, len(s) 1)))
if __name__ == "__main__":
items = tuple(('a', 'b', 'c')) # Given
combos = powerset(items) # Perform powerset
res = columnizePowerset(combos, items) # Convert powerset to columnized array
print(res)
uj5u.com熱心網友回復:
如果設定了值,您可以檢查所有位掩碼,這意味著我們可以將其包含在當前串列中,否則忽略它。
def powerset(s: list):
res = []
for mask in range(1, 2 ** len(s)):
temp = ['' for _ in range(len(s))]
for j in range(len(s)):
if ((mask >> j) & 1) == 1: # jth bit set in mask
temp[j] = s[j]
res.append(temp)
return res
print(powerset(['a', 'b', 'c']))
[['a', '', ''],
['', 'b', ''],
['a', 'b', ''],
['', '', 'c'],
['a', '', 'c'],
['', 'b', 'c'],
['a', 'b', 'c']]
uj5u.com熱心網友回復:
順序與powerset產量不完全相同,但模式讓我想起了二進制——你可以通過查看遞增整數的位來獲得相同的專案......
我不是 NumPy 向導,但這似乎是合理的 numpy-er。(有關更糟糕的解決方案,請參閱后期編輯歷史記錄。)
import numpy as np
def powerset_numpy(items):
n = len(items)
bit_indexes = np.arange(1, 2 ** n)
n_bits = bit_indexes.shape[0]
item_bits = np.repeat(np.arange(0, n), n_bits).reshape((n, -1))
item_values = np.repeat(items, n_bits).reshape((n, -1))
n_bits = (bit_indexes & (1 << item_bits)).astype(bool)
return np.where(n_bits, item_values, '').T
print(powerset_numpy(['a', 'b', 'c']))
列印出來
[['a' '' '']
['' 'b' '']
['a' 'b' '']
['' '' 'c']
['a' '' 'c']
['' 'b' 'c']
['a' 'b' 'c']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345540.html
下一篇:根據兩列的值添加另一列
