Python question
Thanks for the tips I appreciate it, I will look into those very soon. I still have the problem of switching it into 8-bit binary. Look I need to turn this...
List1=(0,1,1,0,1,0,0,1,0,0,0,0,1,1,1,1)
Into this....
List2=(01101001,00001111)
But I can't get them to do that what I've tried so far only gives me things like this....
Badlist=((0)(1)(1)(0))
And just giving me back a duplicate list that is the same as what I put in...
So you said they should be represented as strings and not integers?
Oh, my first post had some code that should group the items in list1 by eight and put them in list2. Just remove the int() part--leave the "".join(...) part, though--and your list2 should look like: ['01101001', '00001111', ...]
And yes, binary values should be represented as strings in Python, because it doesn't have a special built-in binary number type. E.g., if you use the bin() function to convert something to binary, it will return a string: bin(4) returns the literal string "0b100". You don't strictly need the 0b at the front, but it can't hurt to add it.
Here's the loop from my first post, storing binary values as strings:
foo = [1,1,0,0,1,0,1,0]final_digits = []# Store blocks of 8 digits as strings in final_digitsfor i in range(0, len(foo), 8): temp = "".join(str(j) for j in foo[i:i+8]) final_digits.append(temp)# Or, read the strings as binary numbers, and convert to base 10.for i in range(0, len(foo), 8): temp = int("".join(str(j) for j in foo[i:i+8]), 2) final_digits.append(temp)
A bit more in-depth info on what this is doing:
- range(0, len(foo), 8) creates a range object, which has every eighth number between 0 and len(foo). So, 0, 8, 16, etc.
- "".join(...) joins whatever strings of text are between the parentheses, putting whatever is before the .join() between each item. In this case, it's putting an empty string--""--between each one, so it just concatenates whatever values are inside the parentheses into a single string.
- str(j) for j in foo[i:i+8] is Python's wonderful list comprehension syntax. It takes the value i, which is whatever number the for loop is currently using from the range() object (so, every eighth number from 0 to len(foo)), takes a slice of the list foo starting at that number and going forward eight more values, and converts each item into a string. Then it makes those all into a list. This is equivalent to
foo = [1, 1, 0, 0, ...]final digits = []for i in range(0, len(foo), 8): temp1 = foo[i:i+8] temp2 = "" # empty string for k in temp1: temp2.append(str(k))
- int(..., 2) takes the resulting string of ones and zeros, interprets it as a base 2/binary number, and converts it into base 10.
You should use the int(..., 2) bit if you need to do actual math on these values, like multiplying and dividing. You can leave them as strings (i.e., not use int(..., 2)) if you just need to count how often they appear.
Not using int(..., 2) will give you a final_digits list that looks like ['11001100', '00100101', ...], while using int(..., 2) gives a final_digits list that looks like [203, 56, 48, ...].

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now