Cho Python 2.5 trở lên:
>>> [[a, b, c] for a in [1,2,3] for b in ['a','b'] for c in [4,5]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
Đây là phiên bản đệ quy của product[]
[chỉ là một minh họa]:
def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
Example:
>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
Tạo một đối tượng sản phẩm mới ..
ID tùy chọn. Một định danh sẽ được tạo ngẫu nhiên bởi sọc. ....
Tên cần thiết. Tên của sản phẩm, có nghĩa là có thể hiển thị cho khách hàng ..
Python3
Các
‘
>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
4 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7product[]
0 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6product[]
3product[]
4=
product[]
6product[]
7 product[]
8product[]
9
def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
5product[]
7 test_list
2product[]
9>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7test_list
7 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6=
0Output:
Danh sách ban đầu: [1, 4, 6, 7]
Tuple ban đầu: [1, 3]
Sản phẩm Cartesian là: [[1, 1], [1, 4], [1, 6], [1, 7], [3, 1], [3, 4], [3, 6], [3 , 7]]
Sử dụng itertools.product [] & nbsp;
Nhiệm vụ này cũng có thể được thực hiện bằng cách sử dụng chức năng duy nhất thực hiện nội bộ nhiệm vụ trả lại sản phẩm Cartesian cần thiết, ở đây chúng tôi đang sử dụng itertools.product [].
Python3
=
1 =
2=
3 =
4
Các
‘
>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
4 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7product[]
0 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6product[]
3product[]
4=
,
7,
8
>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7test_list
7 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6=
0
Output:
Danh sách ban đầu: [1, 4, 6, 7]
Tuple ban đầu: [1, 3]
Sản phẩm Cartesian là: [[1, 1], [1, 4], [1, 6], [1, 7], [3, 1], [3, 4], [3, 6], [3 , 7]]
Sử dụng itertools.product [] & nbsp;
Nhiệm vụ này cũng có thể được thực hiện bằng cách sử dụng chức năng duy nhất thực hiện nội bộ nhiệm vụ trả lại sản phẩm Cartesian cần thiết, ở đây chúng tôi đang sử dụng itertools.product [].
Python3
=
1 =
2=
3 =
4
Các
‘
4
7
def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
05def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
06>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
4 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7product[]
0 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6product[]
3product[]
4=
,
7,
8
Các
‘
>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
4 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
7>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7product[]
0 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6product[]
3def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
56=
,
7def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
59>>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
2def product[*args]:
if not args:
return iter[[[],]] # yield tuple[]
return [items + [item,]
for items in product[*args[:-1]] for item in args[-1]]
7test_list
7 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
5 >>> list[product[[1,2,3], ['a','b'], [4,5]]]
[[1, 'a', 4], [1, 'a', 5], [1, 'b', 4], [1, 'b', 5], [2, 'a', 4],
[2, 'a', 5], [2, 'b', 4], [2, 'b', 5], [3, 'a', 4], [3, 'a', 5],
[3, 'b', 4], [3, 'b', 5]]
>>> list[product[[1,2,3]]]
[[1,], [2,], [3,]]
>>> list[product[[]]]
[]
>>> list[product[]]
[[]]
6=
0Output:
Danh sách ban đầu: [1, 4, 6, 7]
Tuple ban đầu: [1, 3]
Sản phẩm Cartesian là: [[1, 1], [1, 4], [1, 6], [1, 7], [3, 1], [3, 4], [3, 6], [3 , 7]]