#آموزش 3 📚
#Itertools
ماژول itertools در پایتون :
این ماژول تمام ترکیبات ممکن k تایی در یک لیست را نشان می دهد.
در مثال زیر k=2 می باشد.
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 2))
print(b)
Output:
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
اگر k=3 باشد داریم :
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 3))
print(b)
Output:
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4),
(1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5),
(2, 4, 5), (3, 4, 5)]
@raspberry_python
#Itertools
ماژول itertools در پایتون :
این ماژول تمام ترکیبات ممکن k تایی در یک لیست را نشان می دهد.
در مثال زیر k=2 می باشد.
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 2))
print(b)
Output:
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
اگر k=3 باشد داریم :
import itertools
a = [1,2,3,4,5]
b = list(itertools.combinations(a, 3))
print(b)
Output:
[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4),
(1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5),
(2, 4, 5), (3, 4, 5)]
@raspberry_python