'How to check the specific element in string are equal in python?

There is a two input first input represent the index of the second input.I need to find the 'X' position of second input are same or not?

input:

123X56XX
Ahjutruu

Output:
  True

Example: X in input one at 4,7,8

position of 4,7,8 in second input is 'u' all are same so i need to print "true"

I tried using enumerate function but not came:

a="123X56XX"
b="Ahjutruu"
xi=[]

for i,val in enumerate(a):
  if val=='X':
    xi.append(i)
print(xi)  
#next step only i dont know how to doo please help

Next i dont know how to check



Solution 1:[1]

Put all values of given indexes of xi in set, the length of this set must equal to 1:

xi = [i for i, c in enumerate(a) if c == 'X']
len(set([b[i] for i in xi])) == 1

Solution 2:[2]

you can do like this

a = "123X56XX"
b = "Ahjutruu"

status = len({j for i,j in zip(a,b) if i=='X'}) == 1
print(status)

Solution 3:[3]

An optional solution:

a="123X56XX"
b="Ahjutruu"

c = list([b[i] for i in range(len(a)) if a[i] == "X"]) # a sublist of the characters from b that correspond to "X" in a
s = set(c) # a set with the elements of c; a set has no duplicated elements, so if all of c's items are the same, s will only have one element
print(len(s) == 1)

Solution 4:[4]

the logic is added for matching the X places in a

a = "123X56XX"
b = "Ahjutruu"
xi = []
allSame = True
for i, val in enumerate(a):
    if val == 'X':
        xi.append(i)#saving the indices
allSame = len(a) == len(b)  # length must be same
prev = None
for v in xi:
    if not allSame:
        break
    if prev is None:
        prev = b[v]  # if previous value is not set setting it
    else:
        allSame = prev == b[v]  # matching with previous value in b's list

print(allSame)

Solution 5:[5]

Minor addition to your code.

a="123X56XX"
b="Ahjutruu"
xi=[]
result = False
for i,val in enumerate(a):
  if val=='X':
    xi.append(i)
print(xi[0])

for index, value in enumerate(xi):
    if index < len(xi) - 1:
        if b[value] == b[xi[index+1]]:
            result = True
        else:
            result = False
print(result)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Orius
Solution 4 Udesh Ranjan
Solution 5 Shreyas Singh