0

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?

August Williams
  • 907
  • 4
  • 20
  • 37

2 Answers2

3

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:]
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • 2
    sir you are too fast for me, I concede my answer and just rather upvote – MooingRawr Oct 26 '16 at 13:31
  • Thank you very much for this. However, for the exponent I did `value[1:8]` as the first digit is the sign bit so I didn't want that in the exponent. Thanks so much for your help! – August Williams Oct 26 '16 at 13:37
  • 1
    @AugustWilliams: in that case you have to do `[1:9]` for taking 8 bits as it takes elemnets upto `9-1` i.e 8. check edit – Moinuddin Quadri Oct 26 '16 at 13:39
  • 1
    Ah, yes. Just realised that in the `print`. Thank you! – August Williams Oct 26 '16 at 13:41
  • 1
    @anonymous stop, xD I was merely just poking at the fun of you being faster than me. I hate being slower than someone and I know there's like millions of people faster than me.... T.T I just need to train better (cue rocky theme music or some training music) I will be better! – MooingRawr Oct 26 '16 at 16:02
1

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.

xZise
  • 2,321
  • 5
  • 22
  • 31
  • I'm very glad you posted this as it has helped me later on in my program. I need to add a radix to a specific point in the mantissa and I was looking to do this with a list. You have helped me a lot! Thanks! – August Williams Oct 26 '16 at 14:22