0

I have these two arrays

strings1 = ['text1a','text1b']
strings1 = ['text2a','text2b']

I want to use string.join with a dash dynamically to get this.

text1a - text1b # "-".join(strings1)
text2a - text2b # "-".join(strings2)

However, I don't make that with these two lines above commented. I'd like to make this dynamically so:

array_vars = [
    'strings1','strings2' # referencing every array
]

# and make a loop
for x in array_vars:
    print("-".join(x))  # in this line I hope evaluate x like a var (strings1 or string2, or stringsN.

And, is it possible to get name of every item from list?

strings1 = ['football','tennis']
strings2 = ['cards','dominoe']

texto ='I am playing dominoe'

array_vars = [
    strings1,
    strings2
]

# and make a loop
for x in array_vars:
    print(f'{x}', re.search('|'.join(x), texto, re.I))  # 

And get item's name instead its values.

strings2 instead ['cards', 'dominoe']

for x in array_vars:
    print(f'{x}', re.search('|'.join(x), texto, re.I))  # 

# I got  this...
# ['football', 'tennis'] None
# ['cards', 'dominoe'] <re.Match object; span=(13, 20), match='dominoe'>

Any idea or suggestion? Yes... this.

I just changed the array to a dict

array_vars = {
    "strings1":strings1,
    "strings2":strings2
}

# and make a loop
for x in array_vars:
    print(x, re.search('|'.join(array_vars[x]), texto, re.I))  # 

I got

strings1 None
strings2 <re.Match object; span=(13, 20), match='dominoe'>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Eday Gonzalez
  • 310
  • 1
  • 2
  • 13

0 Answers0