繁体   English   中英

Python - 如何遍历列表中的每个索引 position?

[英]Python - How to loop through each index position in a list?

给定一个列表[[["source1"], ["target1"], ["alignment1"]], ["source2"], ["target2"], ["alignment2"]], ...] ,我想要提取源中与目标中的单词对齐的单词。 例如,在英语-德语句子对中 The hat is on the table。 - Der Hut liegt auf dem Tisch。 ,我想打印以下内容:

The - Der
hat - Hut
is - liegt
on - auf
the - dem
table - Tisch
. - . 

所以我写了以下内容:

en_de = [
[['The', 'hat', 'is', 'on', 'the', 'table', '.'], ['Der', 'Hut', 'liegt', 'auf', 'dem', 'Tisch', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'], 
[['The', 'picture', 'is', 'on', 'the', 'wall', '.'], ['Das', 'Bild', 'hängt', 'an', 'der', 'Wand', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'], 
[['The', 'bottle', 'is', 'under', 'the', 'sink', '.'], ['Die', 'Flasche', 'ist', 'under', 'dem', 'Waschbecken', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6']
]

for group in en_de:
    src_sent = group[0]
    tgt_sent = group[1]
    aligns = group[2]

    split_aligns = aligns.split()

    hyphen_split = [align.split("-") for align in split_aligns]

    align_index = hyphen_split[0]

    print(src_sent[int(align_index[0])],"-", tgt_sent[int(align_index[1])])

正如预期的那样,这将打印src_senttgt_sent的索引 position 0 中的单词:

The - Der
The - Das
The - Die

现在,我不知道如何打印src_senttgt_sent的所有索引位置的单词。 显然,我可以为句子对中的每个 position 手动将align_index更新为新索引 position,但在完整数据集上,某些句子最多有 25 个索引位置。 有没有办法通过每个索引 position 进行循环? 当我尝试:

align_index = hyphen_split[0:]
print(src_sent[int(align_index[0])],"-", tgt_sent[int(align_index[1])])

我得到一个TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'很明显align_index不能是列表,但我不确定如何将其转换为那会做我想做的事。 任何建议或帮助将不胜感激。 先感谢您。

IIUC 你想要这个:

en_de = [
    [['The', 'hat', 'is', 'on', 'the', 'table', '.'], ['Der', 'Hut', 'liegt', 'auf', 'dem', 'Tisch', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'],
    [['The', 'picture', 'is', 'on', 'the', 'wall', '.'], ['Das', 'Bild', 'hängt', 'an', 'der', 'Wand', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'],
    [['The', 'bottle', 'is', 'under', 'the', 'sink', '.'], ['Die', 'Flasche', 'ist', 'under', 'dem', 'Waschbecken', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6']
]


for sentences in en_de:
    for en, de in zip(*sentences[:2]):
        print(f'{en} - {de}')

为每个句子打印成对的英语和德语。 如果他们总是成对的,这应该可以。 因此,如果 alignment 始终是线性的,则根本没有必要拥有它。

如果 alignment 并不总是线性的,您也需要考虑到这一点:

en_de = [
    [['The', 'hat', 'is', 'on', 'the', 'table', '.'], ['Der', 'Hut', 'liegt', 'auf', 'dem', 'Tisch', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'],
    [['The', 'picture', 'is', 'on', 'the', 'wall', '.'], ['Das', 'Bild', 'hängt', 'an', 'der', 'Wand', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6'],
    [['The', 'bottle', 'is', 'under', 'the', 'sink', '.'], ['Die', 'Flasche', 'ist', 'under', 'dem', 'Waschbecken', '.'], '0-0 1-1 2-2 3-3 4-4 5-5 6-6']
]


for sentences in en_de:
    # alternative to the below for loop
    # alignment = [(int(a), int(b)) for a, b in [p.split('-') for p in sentences[2].split()]]
    alignment = []
    for pair in sentences[2].split():
        e, g = pair.split('-')
        alignment.append((int(e), int(g)))

    english = [sentences[0][i] for i, _ in alignment]
    german = [sentences[1][i] for _, i in alignment]
    for en, ge in zip(english, german):
        print(f'{en} - {ge}')

这个方法很复杂,我想你应该研究一下 python 字典。

您忘记遍历hyphen_split列表:

for group in en_de:
    src_sent = group[0]
    tgt_sent = group[1]
    aligns = group[2]

    split_aligns = aligns.split()

    hyphen_split = [align.split("-") for align in split_aligns]

    for align_index in hyphen_split:
        print(src_sent[int(align_index[0])],"-", tgt_sent[int(align_index[1])])

请参阅从您的代码更新的最后两行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM