gpt4 book ai didi

python - 如何 for 循环遍历两个嵌套循环内的字符串列表并避免外部循环造成的冗余?

转载 作者:行者123 更新时间:2023-12-01 07:56:00 25 4
gpt4 key购买 nike

screenshot

我想做的是一次打印每个翻译,但现在打印的只是每个循环中每次翻译项目的完整列表。

"""
pure_data = string
transliterate_input = string
stemmed_tokens = list of dictionaries, each dictionary: key:string, value:list
translation: list of strings
"""

def parse_results(pure_data, transliterated_input, stemmed_tokens, translation):
try:
formatted_strings = []
footer = "-"*50
for d in stemmed_tokens:
for k, v in d.items():
found_stem = uni2ma(k)
token_string = f"[{' + '.join([token for token in v])}]"
translated = f"{[t for t in translation]}"
word_string = f"original input:\n{pure_data}\nTransliterated Input:\n{transliterated_input}\nFound stem:\n{found_stem}\nparsed_results:\n {token_string}\n {translated}\n{footer}\n"
formatted_strings.append(word_string)
return '\n'.join(formatted_strings)
except:
final_str = 'There was a problem retrieving that information'

编辑:假设我有一个输入:纸牌屋。

它被分成 3 个字符串,这三个不同的输入,每个字符串都进行相应的处理。但是当涉及到“翻译”部分时,不要每次只打印第一个、第二个或第三个值,例如

Translation: House
Translation: of
Translation: cards

它打印:

Translation: house of cards
Translation: house of cards
Translation: house of cards

我希望现在一切都清楚了。

最佳答案

我认为您可以通过使用索引来获取翻译中的相应元素来简单地实现您的目标,而不是:

translated = f"{[t for t in translation]}"

更改为:

idx = 0
for d in stemmed_tokens:
for k, v in d.items():
...
translated = translation[idx]
...
idx += 1

不知道是否有误会。

关于python - 如何 for 循环遍历两个嵌套循环内的字符串列表并避免外部循环造成的冗余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55983417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com