I have a string (string = 'OBEQAMXITWA'
), and I want to add it to an empty list, always a condition is satisfied. A certain letter should not be in a determined position of the string, but it is on the other positions. For instance, the letter A
should not be in the position 5
but should be in any other position. If this condition is satisfied, I append the string to the list; if not, I don't append it.
string = 'OBEQAMXITWA'
if 'A' != string[5] and ('A' in string[0:5] or 'A' in string[6:len(string)]):
word_list.append(string)
On the other hand, I want to check if the letter Z
is not in position 6
that is True
, but I would add only if Z
is in any other position that is not position 6
. In this case, Z
is not in the string, so I would not add it to the list.
I want to do this iteratively, defining a dictionary (letter:position
) and checking all the letters and positions added to the dictionary. For instance, the whole code to do this manually would be something like this:
string = 'OBEQAMXITWA'
word_list=[]
letter_change_pos = {'A':5,'T':3,'Z':6}
if 'A' != string[5] and ('A' in string[0:5] or 'A' in string[6:len(string)]):
word_list.append(string)
if 'T' != string[3] and ('T' in string[0:3] or 'T' in string[4:len(string)]):
word_list.append(string)
if 'Z' != string[6] and ('Z' in string[0:6] or 'Z' in string[7:len(string)]):
word_list.append(string)
print(word_list)
How could I do this using a for loop?
CodePudding user response:
The question requires that (1) loop on each character of the dictionary, (2) check if the character is in the string and not in the specified index, (3) if yes, append the string in the list, and (4) otherwise, don't append the string.
A quick solution using loops can be:
# The required string to check.
string = 'OBEQAMXITWA'
# Create an empty list.
wordList = []
# The dictionary rules for the string.
letterChangePos = {'A': 5, 'T': 3, 'Z': 6}
# Loop on each character of the dictionary.
for key in letterChangePos.keys():
# Check if the character is in the string.
if (key in string):
# Check if the key is not the same as the character in the specified index.
if (key != string[letterChangePos[key]]):
wordList.append(string)
# Print the list.
print(wordList)
Using the comprehensive lists approach, the solution would be:
# The required string to check.
string = 'OBEQAMXITWA'
# The dictionary rules for the string.
letterChangePos = {'A': 5, 'T': 3, 'Z': 6}
wordList = [
string for key in letterChangePos.keys()
if ((key in string) and (key != string[letterChangePos[key]]))
]
# Print the list.
print(wordList)
CodePudding user response:
def check_list(test_string, position_dict):
word_list = []
for key, value in position_dict.items():
if key != test_string[value - 1] and (key in string[0:value-1] or key in string[value:]):
word_list.append(test_string)
return word_list
if __name__ == '__main__':
string = 'OBEQAMXITWA'
letter_change_pos = {'A': 5, 'T': 3, 'Z': 6}
print(check_list(string, letter_change_pos))
CodePudding user response:
I'm not really sure what the title "Iterate using keys and values of a dictionary" has to do with your question.
You could write a function that checks every character in a string like this:
def check_invalid_character_positions(string, invalid_positions):
for idx, char in enumerate(string):
if invalid_positions.get(char) == idx:
return False
# Now check that the characters in invalid_positions are present
# elsewhere in the string
return not set(invalid_positions).difference(string)
Use it like:
>>> string = 'OBEQAMXITWA'
>>> invalid_positions = {'A': 5, 'T': 3, 'Z': 6}
>>> if check_invalid_character_positions(string, invalid_positions):
... word_list.append(string)
Per this comment you could also technically do this with a regexp I think, but that would be more complicated.