Skip to content
Advertisement

Python – How to loop through each index position in a list?

Given a list [[["source1"], ["target1"], ["alignment1"]], ["source2"], ["target2"], ["alignment2"]], ...] , I want to extract the words in the source that align with the words in the target. For example, in the English-German sentence pair The hat is on the table . – Der Hut liegt auf dem Tisch ., I want to print the following:

JavaScript

So I have written the following:

JavaScript

This prints, as expected, the words in index position 0 of src_sent and tgt_sent:

JavaScript

Now, I don’t know how I can print the words of all index positions of src_sent and tgt_sent. Obviously, I could manually update align_index to a new index position for each position in the sentence pair, but on the full dataset, some sentences will have up to 25 index positions. Is there a way to possibly for-loop through each index position? When I try:

JavaScript

I get a TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' It’s clear that align_index can’t be a list, but I’m not sure how to convert it into something that will do what I want it to do. Any advice or help would be greatly appreciated. Thank you in advance.

Advertisement

Answer

You are forgetting to loop over your hyphen_split list:

JavaScript

See the last two lines, updated from your code.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement