-3

I have two strings:

var_1 = 'ebro EBI 310 TE Temperature data logger'
var_2 = 'EBRO EBI 310 TE USB-LOGGER'

How can I (without regex and long loops) create a third variable that contains the matching characters from both the first and second variables? For example, the output would be;

var_3 = 'EBRO EBI 310 TE'

Can I compare four or more variables in the same way and find the part of the string that occurs in all variables and where it does not occur?

Madison Courto
  • 1,231
  • 13
  • 25
Lemon Tree
  • 63
  • 5
  • 5
    Your question is more complicated than you perhaps realise. For example, for strings like `abbcdbcde` and `abcadef` - would you expect the result `abcde`? Or just `ab`? Do you only care if it *starts* with the part it has in common? (and is case-insensitive?) Is the second string always the shorter one? – Grismar Sep 20 '22 at 23:55
  • @Grismar in this case, it should not be, because `abcadef` is not included in `abbcdbcde` (and vice versa).The full occurrence of the word (in the string) is implied, isn't it obvious from my example? – Lemon Tree Sep 22 '22 at 08:13
  • 1
    You provided one example, that matched your much broader description. If I tell you to "shoot all the white birds" and show you a picture of a swan, do you understand that to mean to only shoot the swans, or did I just show you a picture of a swan to illustrate the broader question? No, it is not obvious. You should always be specific when providing requirements to code. – Grismar Sep 22 '22 at 12:40

2 Answers2

2

Here is an example of how to compare four or more vars as you have stated at the exact position and any position,

var_1 = 'ebro EBI 310 TE Temperature data logger'
var_2 = 'EBRO EBI 310 TE USB-LOGGER'
var_3 = 'EBRO EBI 310 TE USB-THINGY'
var_4 = 'EBRO EBI 310 TE USB-THUGY'
# create a function to return only the characters that are the same in x amount of string arguments at the same position
def compare(*args):
    # convert args to lower case
    args = [x.lower() for x in args]
    return ''.join([x[0] for x in zip(*args) if all(y== x[0] for y in x)]).upper()

compare_result = compare(var_1, var_2, var_3, var_4)
print(compare_result)

# create a function to return only the characters that are the same in x amount of string arguments at any position
def compare_any(*args):
    # convert args to lower case
    args = [x.lower() for x in args]
    return ''.join([x[0] for x in zip(*args) if any(y.lower() == x[0].lower() for y in x)]).upper()
compare_any_result = compare_any(var_1, var_2, var_3, var_4)
print(compare_any_result)

Output:

EBRO EBI 310 TE 
EBRO EBI 310 TE TEMPERATU
Madison Courto
  • 1,231
  • 13
  • 25
  • in this case, if I send only two variables, an extra "R" character creeps in. `compare(var_1, var_2)` must be `EBRO EBI 310 TE` and not `EBRO EBI 310 TE R` – Lemon Tree Sep 21 '22 at 11:27
0

It's not a very long loop...

new_string = []
for i, char in enumerate((var_1 if var_1 > var_2 else var_2)): 
  if var_1[i] == var_2[i]: new_string.append(char)
  else: break
new_string = ''.join(new_string)
walker
  • 444
  • 2
  • 12