Эти решения прекрасно работают как в python
, так и в IronPython
.
Императивное решение:
A = ["a", "b", "c", "d"]
B = ["a1", "a2", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3", "d4"]
results = []
for prefix in A:
matches = []
results.append((prefix, matches))
for el in B:
if el.startswith(prefix):
matches.append(el)
for res in results:
print res
Функциональное решение:
A = ["a", "b", "c", "d"]
B = ["a1", "a2", "b1", "b2", "b3", "c1", "c2", "c3", "d1", "d2", "d3", "d4"]
groups = [(x,[y for y in B if y.startswith(x)]) for x in A]
for group in groups:
print group
РЕЗУЛЬТАТ:
('a', ['a1', 'a2'])
('b', ['b1', 'b2', 'b3'])
('c', ['c1', 'c2', 'c3'])
('d', ['d1', 'd2', 'd3', 'd4'])