I have some problems comparing Strings/Substring in Python. Here is my code:
spelformer = "V86-1 V75-1 GS75-1".split()
l_count = 0
for line in input:
print(line.split())
print(spelformer)
l_count = 1
if line in spelformer:
print("ja")
else:
print("nej")
break
This is the result:
runfile('G:/Min enhet/Python/Travscript.py', wdir='G:/Min enhet/Python')
['V86-1']
['V86-1', 'V75-1', 'GS75-1']
nej
What I can see, line is equal to V86-1 and spelformer is equal to V86-1 and therefore it should be a match. But the results is still "nej" = "no".
Anyone knows why?
Sorry for newbie questions..
CodePudding user response:
May be you try it this way:
spelformer = "V86-1 V75-1 GS75-1".split()
for s in input().split():
if s in spelformer: print('yes')
else: print('no')
CodePudding user response:
The spelformer string that you are passing is in double quotes (" "), but the string you are passing in input will be taken as single quotes (' '). So, while splitting, the input will be splitted character by character. Also, the () for input is missing.
CodePudding user response:
if line[0] in spelformer:
This should work as now the string values will be compared. Earlier the list ['V86-1'] was comapared to ['V86-1', 'V75-1', 'GS75-1']. Instead what you need is the string 'V86-1' to be compared to ['V86-1', 'V75-1', 'GS75-1'] as all the elements inside the list is string and not another list.