Write a Python program to get all possible combinations of the elements of a given list

  • برمجة
  • برمجة بايثون

Write a Python program to get all possible combinations of the elements of a given list.

الأجوبة

def combinations_list(colors):
    if len(colors) == 0:
        return [[]]
    result = []
    for el in combinations_list(colors[1:]):
        result += [el, el+[colors[0]]]
    return result
colors = ['orange', 'red', 'green', 'blue']
print("Original list:")
print(colors)
print("\nAll possible combinations of the said list’s elements:")
print(combinations_list(colors))
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...