I am trying to create a binary32 floating point to decimal converter, which is made up of eight bits for the exponent and twenty four bits for the mantissa. I have exponent = []
and mantissa = []
. If the user inputs 010000111111101101000000000000000
I want indexes one to eight from value
to be added to exponent
and indexes nine to thirty two from value
to be added onto mantissa
. I currently have the following spaghetti code to do this:
print ("Welcome to August's floating point value to decimal converter!")
value = input("Please enter 32 bit floating value to convert.")
exponent = []
mantissa = []
exponent.append(value[1])
exponent.append(value[2])
exponent.append(value[3])
exponent.append(value[4])
exponent.append(value[5])
exponent.append(value[6])
exponent.append(value[7])
exponent.append(value[8])
print (exponent)
mantissa.append(value[9])
mantissa.append(value[10])
mantissa.append(value[11])
mantissa.append(value[12])
mantissa.append(value[13])
mantissa.append(value[14])
mantissa.append(value[15])
mantissa.append(value[16])
mantissa.append(value[17])
mantissa.append(value[18])
mantissa.append(value[19])
mantissa.append(value[20])
mantissa.append(value[21])
mantissa.append(value[22])
mantissa.append(value[23])
mantissa.append(value[24])
mantissa.append(value[25])
mantissa.append(value[26])
mantissa.append(value[27])
mantissa.append(value[28])
mantissa.append(value[29])
mantissa.append(value[30])
mantissa.append(value[31])
mantissa.append(value[32])
print (mantissa)
So rather than appending each index individually I was wondering if there is a way to add them to the list all in one line. I have tried the following extend
methods:
exponent.extend(value[1, 2, 3, 4, 5, 6, 7, 8])
and without commas too
exponent.extend(value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], )
which I then realised extend
only take one argument.
exponent.extend(value[1-8])
which seem to of subtracted the one and eight.
I tried exponent = {}
which is a set I believe? And then tried exponent.update
followed by the multiple indexes with commas. Which then told me it only supports one argument.
Any other suggestions on how to add multiple indexes from value
to a list?
Same code can be written using string slicing as:
value = input("Please enter 32 bit floating value to convert.")
exponent, mentissa= value[1:9], value[9:]
Although your question was answered with a neat solution I wanted to actually answer your original question. You can add a list to a list by using either of them:
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b # c is now [1, 2, 3, 4, 5, 6]
a.extend(b) # a is now [1, 2, 3, 4, 5, 6]
Notice that list.extend
actually modifies the list while the addition itself does not. You can do a += b
however and achieve the same result as a.extend(b)
.
So you can do the following:
exponent.extend(value[1:9])
If you use list.extend
you can actually use any iterable (like tuple or set) but when you use a += b
or c = a + b
they usually both need to be the same type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.