Avatar ·

How to multiply lists in Python 3.x

📁 List, Advanced Python Topics

There is a text file with the following content ""D11i14V17A8n7R11H16r18P18E5h18i18x8O16Y5s13d1X6u4a20C2p2O14J5I11L5K6y4X19n10s17h1E17Q5Z1U1K6U8E8L11n14k6"". I wrote code that splits letter values into one list and numeric values into another. How to correctly multiply the data from the list so that the final result is D * 11 = DDDDDDDDDDD (Letter D 11 times). Code below:

import rec = []d = []result = 0with open('dest.txt', 'r') as s:    for line in s:        line = line.strip()        lst = re.split('(\\d*)'                    
Avatar ·

For this construct:

for i in my_list:    i*=5

Python creates a new variable i into which the values from the list are sequentially placed. As a result, you are changing the value of the variable i, the list remains unchanged.

In [136]: my_list=[1,2,3,4,5,6]     ...: for i in my_list:     ...:     i*=5     ...:     print(i, my_list)     ...:5                                

Log in to leave an answer

Blogs