infile=open("Integers.txt", "r")
ListIntegers=infile.readline().strip("\n").split(";")
def avgFirstThreeDigits(mylist):
list=[]
for i in range(0,len(mylist)):
sumFirstThreeDigits=(int(mylist[i][0])+int(mylist[i][1])+int(mylist[i][2]))
avg=sumFirstThreeDigits/3
list.append(avg)
print(list[::-1])
avgFirstThreeDigits(ListIntegers)
Asked
Active
Viewed 333 times
-2
-
(Sorry for the code) – Atakan Ak Nov 14 '16 at 19:21
-
Select the code and press "ctrl+k" to format it. – Brian Rodriguez Nov 14 '16 at 19:22
-
don't be sorry. edit and format it correctly. – njzk2 Nov 14 '16 at 19:22
-
also, consider using the "search" feature. – njzk2 Nov 14 '16 at 19:23
-
1@AtakanAk. What's wrong with `[::-1]`? Is this a homework question? – ekhumoro Nov 14 '16 at 19:27
-
Ok thanks. Yes its a homework question we can't use [::-1] – Atakan Ak Nov 14 '16 at 19:38
2 Answers
1
You can manually reverse a list with the following:
a = ['a', 'b', 'c', 'd']
b = [a[len(a) - i - 1] for i in range(len(a))]
>>> b
>>> ['d', 'c', 'b', 'a']

Jeff Mandell
- 863
- 7
- 16
0
If you just want to get a list of tuples/vectors averages, you can just do something like this:
list_averages = [(sum(v3) / len(v3)) for v3 in list_integers]
Or just using another of the multiple ways to calculate the arithmetic mean average will do
About an alternative to [::-1]
, you can just use list.reverse
But be aware list.reverse
will modify in place, if you don't want to you could use list(reversed(list_averages))
. In any case, I don't know why you don't want to use [::-1], that one is a pretty fast choice.