In [41]:
def da3(P,Q):
    import numpy as np
    import sys
    
    if isinstance(Q, list) != True and isinstance(Q,np.ndarray) != True:
        print "Input Error!!"
        sys.exit()
        
    if isinstance(P, list):
        P = np.array(P)
        Q = np.array(Q)
    elif isinstance(P, np.ndarray):
        print "ndarray"
    else:
        print "Input Error!!"
        sys.exit()
    N=P.shape[0]
    G=Q.shape[0]
    S=np.tile([-1],N)
    free=range(N)
    n=0
    
    while free:
        print S
        print n
        """
        上のシャープを外すことで男性の婚約状況の一周ごとの推移を表示することができます
        正しくない結果が返ってきている疑いのある時に様子を見るため使いましょう
        """
        A=free.pop() 
        F=P[A,:][n]
        if F==G:
            S[A]=G
            n=0
        else:
            for J in range(N+1):   
                if Q[F,:][J]==A:
                    D=J
                if Q[F,:][J]==N:
                    E=J
            if E<D:
                free.append(A)
                n+=1
            else:
                if F not in S:
                    S[A]=F
                    n=0        
                else:
                    for y in range(N):
                        if S[y]==F:
                            M=y         
                    for z in range(N+1):
                        if Q[F,:][z]==M:
                            Z=z
                        if Q[F,:][z]==A:
                            W=z
                    if W<Z:
                        S[A]=F
                        S[M]=-1
                        free.append(M)
                        n=0
                    else:
                        free.append(A)
                        n+=1
        if len(free)==0:   
            s=list(S)
            L=np.tile([N],G)
            l=list(L)
            for u in s:
                if u==G: continue  
                else:
                    v=s.index(u)
                    l[u]=v
            return s,l
        else: continue

In [42]:
import numpy as np
a=np.random.permutation(5)
b=np.random.permutation(5)
c=np.random.permutation(5)
d=np.random.permutation(5)
e=np.random.permutation(5)
f=np.random.permutation(5)
g=np.random.permutation(5)
h=np.random.permutation(5)
i=np.random.permutation(5)
j=np.random.permutation(5)
n_pref=np.vstack((a,b,c,d,e))
m_pref=np.vstack((f,g,h,i,j))

In [43]:
n_pref.shape[0]


Out[43]:
5

In [44]:
da3(m_pref,n_pref)


ndarray
[-1 -1 -1 -1 -1]
0
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-44-a3aa45e23f2f> in <module>()
----> 1 da3(m_pref,n_pref)

<ipython-input-41-dbe93fe0fd24> in da3(P, Q)
     35         else:
     36             for J in range(N+1):
---> 37                 if Q[F,:][J]==A:
     38                     D=J
     39                 if Q[F,:][J]==N:

IndexError: index 5 is out of bounds for axis 0 with size 5

In [128]:
m_pref


Out[128]:
array([[1, 4, 3, 2, 5, 0],
       [4, 0, 3, 1, 5, 2],
       [4, 3, 0, 1, 5, 2],
       [4, 0, 2, 3, 1, 5],
       [5, 3, 0, 2, 1, 4],
       [3, 0, 1, 2, 5, 4],
       [4, 1, 5, 2, 0, 3],
       [5, 4, 1, 0, 2, 3]])

In [129]:
n_pref


Out[129]:
array([[7, 3, 5, 8, 0, 2, 6, 4, 1],
       [5, 6, 0, 1, 8, 3, 7, 4, 2],
       [2, 4, 5, 0, 6, 7, 1, 8, 3],
       [8, 7, 6, 0, 5, 2, 4, 3, 1],
       [4, 8, 7, 5, 0, 1, 3, 6, 2]])

In [187]:
list(range(8))


Out[187]:
[0, 1, 2, 3, 4, 5, 6, 7]

In [32]:
def da3(P,Q):
    import numpy as np
    import sys
    np.asarray(P)
    np.asarray(Q)
    N=P.shape[0]
    G=Q.shape[0]
    S=np.tile([-1],N)
    free=range(N)
    n=0
    
    while free:
        #print S
        #print n
        """
        上のシャープを外すことで男性の婚約状況の一周ごとの推移を表示することができます
        正しくない結果が返ってきている疑いのある時に様子を見るため使いましょう
        """
        A=free.pop() 
        F=P[A,:][n]
        if F==G:
            S[A]=G
            n=0
        else:
            for J in range(N+1):   
                if Q[F,:][J]==A:
                    D=J
                if Q[F,:][J]==N:
                    E=J
            if E<D:
                free.append(A)
                n+=1
            else:
                if F not in S:
                    S[A]=F
                    n=0        
                else:
                    for y in range(N):
                        if S[y]==F:
                            M=y         
                    for z in range(N+1):
                        if Q[F,:][z]==M:
                            Z=z
                        if Q[F,:][z]==A:
                            W=z
                    if W<Z:
                        S[A]=F
                        S[M]=-1
                        free.append(M)
                        n=0
                    else:
                        free.append(A)
                        n+=1
        if len(free)==0:   
            s=list(S)
            L=np.tile([N],G)
            l=list(L)
            for u in s:
                if u==G: continue  
                else:
                    v=s.index(u)
                    l[u]=v
            return s,l
        else: continue
            
#リスト内包表記は早い

In [33]:
da3(m_pref,n_pref)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-33-a3aa45e23f2f> in <module>()
----> 1 da3(m_pref,n_pref)

<ipython-input-32-6b86f0150f94> in da3(P, Q)
     24         else:
     25             for J in range(N+1):
---> 26                 if Q[F,:][J]==A:
     27                     D=J
     28                 if Q[F,:][J]==N:

IndexError: index 5 is out of bounds for axis 0 with size 5

In [2]:
a={}
for i in range(5):
    a[i]=[]

In [3]:
a


Out[3]:
{0: [], 1: [], 2: [], 3: [], 4: []}

In [ ]: