Reputation: 3
I'm having troubles creating a list from the numbers I made using my code.
ListA = [5,1,3,8,4]
LengthB = (len(ListA))
obj0 = ListA[0]
obj1 = ListA[1]
obj2 = ListA[2]
obj3 = ListA[3]
obj4 = ListA[4]
obj01 = int(obj0)
obj11 = int(obj1)
obj21 = int(obj2)
obj31 = int(obj3)
obj41 = int(obj4)
obj010 = obj01+obj11
obj110 = obj01+obj11+obj21
obj210 = obj11+obj21+obj31
obj310 = obj21+obj31+obj41
obj410 = obj31+obj41
ListBnum0 = (obj010 / 2)
ListBnum1 = obj110 / 3
ListBnum2 = obj210 / 3
ListBnum3 = obj310/ 3
ListBnum4 = obj410 / 2
print(ListBnum0)
print(ListBnum1)
print(ListBnum2)
print(ListBnum3)
print(ListBnum4)
FinalForm1 = str(ListBnum0)
FinalForm2 = str(ListBnum1)
FinalForm3 = str(ListBnum2)
FinalForm4 = str(ListBnum3)
FinalForm5 = str(ListBnum4)
Basically this program takes the ListA
numbers and computes the average of the one behind the number, the number and number after if applicable. My real question is how can I take either ListBnum(0)
to ListBnum(4)
and create another list out of the numbers?
Why does this return an error below?
ListB = list[ListBnum0,ListBnum1,ListBnum2,ListBnum3,ListBnum4]
Upvotes: 0
Views: 76
Reputation: 366213
The direct answer to your question is:
new_list = [ListBnum0, ListBnum1, ListBnum2, ListBnum3, ListBnum4]
The reason you get an error when doing this:
ListB = list[ListBnum0,ListBnum1,ListBnum2,ListBnum3,ListBnum4]
… is that list
is a function, and function calls need parentheses to call them. You could write it like this:
ListB = list([ListBnum0,ListBnum1,ListBnum2,ListBnum3,ListBnum4])
However, there's no reason to do so. What the list
function does is to take any iterable as an argument, and return a list with the same values. But [ListBnum0, …]
is already a list, so you're just making a copy for no reason.
Meanwhile, this whole design is clunky. Better to process the whole list at a time, than to split it into 5 separate variables and process them one by one and merge them back into a list. For example:
ListA = [5,1,3,8,4]
List0 = [int(i) for i in ListA]
def avg(x):
return sum(x) / len(x)
ListB = [avg(List0[max(i-1, 0):i+2]) for i in range(len(List0))]
ListFinal = [str(i) for i in ListB]
Upvotes: 4