0

I have seveval str like bellow:

str1 = "Seagate SATA___1000"
str2 = "Seagate SATA___1200"
str3 = "Seagate SSD___512"
str4 = "Seagate SSD___512"
str5 = "Seagate SSD___512"
str6 = "Seagate SSD___512"
str7 = "Seagate SSD___512"
str8 = "Seagate SSD___512"
str9 = "Seagate SSD___512"
str10 = "Seagate SSD___512"

I want to calculate the total size of them, so I constructed the bellow function:

def get_all_size():

    total_size = 0

    for i in range(1, 11):

        str_tmp = 'str'
        if i != 1:
            str_tmp += str(i)

        total_size += int(re.findall(r"\d+\.?\d*", str_tmp)[-1])  # there how to let the `str_tmp` to be the variable? 

But, obviously, the str_tmp is not the variable, it is a string.

How to realize my requirement ?

user7693832
  • 6,119
  • 19
  • 63
  • 114
  • 1
    You can hack it through `locals` but the better approach will be to store the strings in a list rather than separate variables, then you can simply iterate the list – DeepSpace Jun 25 '19 at 10:25
  • How about you put those str1..10 in a list and iterate through them? – chakwok Jun 25 '19 at 10:25
  • The javascript can do this. – user7693832 Jun 25 '19 at 10:28
  • 1. Variable variables are a bad idea in any language. 2. Even in Javascript you'll have a pretty hard time doing this without `eval`. 3. Just because another language "can do it" doesn't mean it's a good idea to do it in *this* language. – deceze Jun 25 '19 at 10:40

0 Answers0