Python Problem with python code. Combinations of 2-state vectors

AI Thread Summary
The discussion revolves around generating all combinations of a four-element vector consisting of only 1's and -1's. The user initially describes an inefficient method involving a while loop that checks for duplicates in a list of vectors. The approach involves randomly changing the sign of an element in the vector `A` and checking if it already exists in the list `vectors`. If it does, the sign is changed again; if not, the new vector is added to the list. However, the user's code fails to produce new combinations, resulting in an infinite loop. The user acknowledges the inefficiency of their method but prioritizes learning Python over using built-in functions. Ultimately, they mention resolving the issue with assistance.
wyosteve
Messages
23
Reaction score
0
I am trying to find all combinations of a four element vector that contains only 1's and -1's.
Ex (1,1,1,1),(-1,1,1,1),...(-1,-1,-1,-1) etc
My idea to do this is pretty inefficient I am sure but I can't think of another way to do it, so here is what I was trying to do.
I found how many total vectors there would be, then created that many empty lists. I began with vector `A` and then compared this to each list in `vectors`. If `A` matched any of the lists then I randomly changed the sign of an element of `A`, and then again checked the new list `A` against `vectors`. If `A` didn't find a match then it replaced on of the lists in `vectors`, and the while loop was incremented by 1. This should proceed until all possible combinations have been found and printed.
However my code is just spitting out the first change in `vectors` then continually looping without adding any new `A`'s to `vectors`. Can anyone spot what in my code isn't doing what I intended it to do and/or point me in the right direction? Thanks

Code:
import random
    numvec = 2**4 # number of macrostates for a 4 component 2-state system

    vectors = [[0,0,0,0] for i in range(numvec)] #initializing the numvec vectors 
    A = [1,1,1,1]
    i = 0
    while i < 16:
        if any(x == A for x in vectors):
            y = random.randrange(0, 3)
            A[y] = A[y] * -1
        else:
            vectors[i] = A
            print vectors[i] 
            i += 1
      
    print vectors

Oh and again I realize this method is incredibly inefficient but as this is a homework assignment I am more concerned with being able to get python to do what I want it to do, then using a bunch of built in functions to do the work for me. Thanks again.
 
Technology news on Phys.org
never mind, figured it out (with help)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
4K
Replies
3
Views
1K
Replies
5
Views
1K
Replies
2
Views
1K
Replies
4
Views
2K
Replies
11
Views
1K
Replies
2
Views
956
Back
Top