# How to optimize my code in Python?

As in the cycle to replace the one digit so that the type "string" has become a type variable and output the contents of the variable?
To compile at the end of the post will understand what I mean.
Well, the joint question of how to optimize the beard you see?))
``````list_iter = []
O = 1
b = 2.7
rou = 3
c0 = round(O/(b-1), rou)
c1 = round((O + c0) / (b-1), rou)
c2 = round((O + c0 + c1) / (b-1), rou)
c3 = round((O + c0 + c1 + c2) / (b-1), rou)
c4 = round((O + c0 + c1 + c2 + c3) / (b-1), rou)
c5 = round((O + c0 + c1 + c2 + c3 + c4) / (b-1), rou)
c6 = round((O + c0 + c1 + c2 + c3 + c4 + c5) / (b-1), rou)
c7 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6) / (b-1), rou)
c8 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7) / (b-1), rou)
c9 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8) / (b-1), rou)
c10 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9) / (b-1), rou)
c11 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10) / (b-1), rou)
c12 = round((O + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11) / (b-1), rou)

x = 0
while x != 12:
list_iter.append('c%s' % x)
x += 1

print(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12)
print(list_iter)``````

>>> Conclusion:
0.588 0.934 1.484 2.356 3.742 5.944 9.44 14.993 23.812 37.819 60.066 95.399 151.516
['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'c11']
July 2nd 19 at 17:41
July 2nd 19 at 17:43
Solution
``````list_iter = []
o = 1 # 'O' is one of the most unfortunate names a variable (in other things, 'o' is not much better :))
b = 2.7
rou = 3
for c in range(13):
list_iter.append(round((o+sum(list_iter))/(b-1), rou))
>>> list_iter
[0.588, 0.934, 1.484, 2.356, 3.742, 5.944, 9.44, 14.993, 23.812, 37.819, 60.066, 95.399, 151.516]``````
slice can and can not do - Garnett_Deckow commented on July 2nd 19 at 17:46
: by the way, Yes, I thought :) Thanks) - vinnie_Quigley commented on July 2nd 19 at 17:49
Comrades, what is the cutoff? - jimmy.Lemke commented on July 2nd 19 at 17:52
: https://habrahabr.ru/post/89456/ - Garnett_Deckow commented on July 2nd 19 at 17:55
: Thanks! - jimmy.Lemke commented on July 2nd 19 at 17:58
July 2nd 19 at 17:45
Solution
``````result = [0] * 13
initial_value = 1
b = 2.7
divider = b - 1
round_value = 3

for i in range(13):
if result:
initial_value += result[i - 1]
result[i] = round(initial_value / divider, round_value)

for i in range(13):
print('c{0:d} = {1:.3f}'.format(i, result[i]))``````
If not difficult explain please, what is "d" and ".3f" in the dictionary? - Garnett_Deckow commented on July 2nd 19 at 17:48
: The arguments to substitute values in a string. Read more about the format method. d - decimal integer f - floating point number, .3 - number of digits after the decimal point - vinnie_Quigley commented on July 2nd 19 at 17:51
: Thanks! - jimmy.Lemke commented on July 2nd 19 at 17:54
July 2nd 19 at 17:47
Solution
``````>>> count = 13
>>> list_iter = []
>>> O = 1
>>> b = 2.7
>>> rou = 3
>>> for im in xrange(count):
... list_iter.append(round((O+sum(list_iter))/(b-1), rou))
...
>>> list_iter
[0.588, 0.934, 1.484, 2.356, 3.742, 5.944, 9.44, 14.993, 23.812, 37.819, 60.066, 95.399, 151.516]``````

To convert to variables, you can use a crutch:
``````>>> for im in xrange(count):
... exec 'c'+str(im)+'='+str(list_iter[im])
...
>>> c3
2.356
>>> c0
0.588
>>> c12
151.516``````
July 2nd 19 at 17:49
thank you - Garnett_Deckow commented on July 2nd 19 at 17:52

Find more questions by tags Python