重複した場合は元の Key : Value 優先タイプ
A = [1, 3, 5, 2, 4, 6, 7, 9, 11, 8, 10, 12, 13, 14, 15, 27, 11, 28, 30, 32, 33, 34, 35, 47]
sz = 8
lsk = [A[i] for i in range(0, len(A), sz)]
# lst = [A[i:i+sz] for i in range(0, len(A), sz)]
# tmp_dict = {lst[i][0]:lst[i][1:] for i in range(0, len(lst)) if lst[i][0] not in lsk[:i] }
tmp_dict = {A[i]:A[i+1:i+sz] for i in range(0, len(A), sz) if A[i] not in lsk[:i//sz] }
print(tmp_dict) # {1: [3, 5, 2, 4, 6, 7, 9], 11: [8, 10, 12, 13, 14, 15, 27]}
重複した場合は後の Key : Value で上書きするタイプ
A = [1, 3, 5, 2, 4, 6, 7, 9, 11, 8, 10, 12, 13, 14, 15, 27, 11, 28, 30, 32, 33, 34, 35, 47]
sz = 8
tmp_dict = {A[i]:A[i+1:i+sz] for i in range(0, len(A), sz)}
print(tmp_dict) # 上書き {1: [3, 5, 2, 4, 6, 7, 9], 11: [28, 30, 32, 33, 34, 35, 47]}
重複した場合は Value を連結するタイプ
def list_to_dict(lst, sz):
from collections import defaultdict
lst = [lst[i:i+sz] for i in range(0, len(lst), sz)]
dic = defaultdict(list)
for ls in lst:
dic[ls[0]] += ls[1:]
return dict(dic)
A = [1, 3, 5, 2, 4, 6, 7, 9, 11, 8, 10, 12, 13, 14, 15, 27, 11, 28, 30, 32, 33, 34, 35, 47]
tmp_dict = list_to_dict(A, 8)
print(tmp_dict) # 連結 {1: [3, 5, 2, 4, 6, 7, 9], 11: [8, 10, 12, 13, 14, 15, 27, 28, 30, 32, 33, 34, 35, 47]}
lst = [lst[i:i+sz] for i in range(0, len(lst), sz)]
は
lst = list(map(list, list(zip(*[iter(A)]*sz))))
と性能比較した方が良いかもしれません。
7 * 重複数
の長さのリストになります。