0

I need to split strings containing LC call numbers. Splitting at the space, I want to separate each chunk into its own variable. See sample LC call numbers below:

B2430 .R553 D813 1991
CB351 .C58 1983
D570.33 369th .N456 2009
DA 685 .B65 B45 1995

Using Python, is there a way to create as many holding variables as there are chunks? If yes, using .split method?

E.g.-

B2430 .R553 D813 1991 - var0= B2430, var1= .R553, var2= D813, var4= 1991
CB351 .C58 1983 - var0= CB351, var1= .C58, var2= 1983

Thanks in advance for any help.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • The list returned by split() already is a "holding variable". After `var = "B2430 .R553 D813 1991".split()`, var[0] is 'B2430', var[1] is '.R553', and etc... Is there any advantage to giving them names such as 'var0'? – tdelaney Jul 03 '13 at 18:14

2 Answers2

1

Use a dictionary instead. You don't want to do what you're explaining:

s = "B2430 .R553 D813 1991 CB351 .C58 1983 D570.33 369th .N456 2009 DA 685 .B65 B45 1995"
mydict = {'var{}'.format(i):j for i, j in enumerate(s.split())}

mydict is now:

{'var5': '.C58', 'var4': 'CB351', 'var7': 'D570.33', 'var6': '1983', 'var1': '.R553', 'var0': 'B2430', 'var3': '1991', 'var2': 'D813', 'var9': '.N456', 'var8': '369th', 'var15': '1995', 'var14': 'B45', 'var11': 'DA', 'var10': '2009', 'var13': '.B65', 'var12': '685'}

But if you must:

for i, j in enumerate(s.split()):
    globals()['var{}'.format(i)] = j
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • 2
    You can use `enumerate(s.split())` ;) – Ashwini Chaudhary Jul 03 '13 at 12:49
  • 1
    Changing the result of `local()` isn't defined. It might not do what you expect, for instance in several cases it does not change anything in the current local variable scope. – Alfe Jul 03 '13 at 12:51
  • @Alfe Should I have used `globals()`? – TerryA Jul 03 '13 at 12:53
  • @Haidro, yes. globals will give you much more consistent behavior. – John Jul 03 '13 at 12:54
  • No, unfortunately there is no proper way to change the variable scope. It works in some cases (most dominantly in the interactive interpreter) but it does not in all cases, neither using `locals()` nor using `globals()` nor any other way I know of. The only (really ugly way) I know is using `exec` like `exec varName + ' = ' + value`. But that has several drawbacks, and I probably would not recommend it. – Alfe Jul 03 '13 at 12:56
  • @Alfe The problem with using `globals()` in the first place ;) – TerryA Jul 03 '13 at 13:00
  • As I said, changing the result of `globals()` may work but is not defined to work. And might not work in all cases (within a class definition, within a function, a method, a classmethod, using a different interpreter like `pypy`, …). And of course cluttering your global scope is not good in any case. So I cannot recommend that. Using `exec` will work in all cases, but is ugly. – Alfe Jul 03 '13 at 13:02
  • Other questions elaborate more on that topic, e. g. http://stackoverflow.com/questions/8799446/is-it-possible-to-dynamically-create-local-variables-in-python – Alfe Jul 03 '13 at 13:16
1

In Python 3 you can at least create a given set of variables and collect the rest in a list:

a, b, c, *rest = range(10)

a, b, c, rest will now be (0, 1, 2, [3, 4, 5, 6, 7, 8, 9]).

Alfe
  • 56,346
  • 20
  • 107
  • 159