Python USES yield to implement a full array array

  • 2020-04-02 14:40:52
  • OfStack

This example shows how python implements a full array array through yield. Share with you for your reference. Specific analysis is as follows:

Taking m (m Or less n) elements from n different elements and arranging them in a certain order is called taking m elements from n different elements. All permutations when m is equal to n are called all permutations.
This code USES the yield method, which doubles the speed of the full array


def perm(arr, pos = 0):
  if pos == len(arr):
    yield arr
  for i in range(pos, len(arr)):
    arr[pos], arr[i] = arr[i], arr[pos]
    for _ in perm(arr, pos + 1): yield _
    arr[pos], arr[i] = arr[i], arr[pos]
for i in perm([1,2,3,4]):
  print i

I hope this article has helped you with your Python programming.


Related articles: