I have this homework problem, and I'm new to python. I have this list of tuples:
[('the, this is me', 'the night'), ('the night', 'me'), ('me', 'the store')]
My code doesn't work when I'm trying to write to target_bigrams with only the tuples that have "the" in position [0]. Please help.
target_bigrams = ()
bigrams_length = len(bigrams)
for i in range(bigrams_length):
if i[0] == target_word:
target_bigrams.append(i[0])
CodePudding user response:
I think this is what you need;
bigrams = [('the, this is me', 'the night'), ('the night', 'me'), ('me', 'the store')]
target_word = 'the'
target_bigrams = []
bigrams_length = len(bigrams)
for i in range(bigrams_length):
if bigrams[i][0].startswith(target_word):
target_bigrams.append(bigrams[i][0])
CodePudding user response:
The question is not clear, but i believe that you want to separate the tuples which has "the" in the first position. If that is the case, here is the sample code for your reference
lst = [('the, this is me', 'the night'), ('the night', 'me'), ('me', 'the store'), ('the', 'the store'),("the","How are you")]
target_list = []
target_word = "the"
for i in range(len(lst)):
if target_word == lst[i][0]:
target_list.append(lst[i])
for i in target_list:
print(i)
Output:
('the', 'the store')
('the', 'How are you')