【发布时间】:2021-11-03 02:22:18
【问题描述】:
如何使用 .join 函数为给定单词组中的每个单词添加前缀?
:param vocab_words: 带前缀的词汇列表。
:return: str of prefix 后跟带有
的词汇 应用前缀,用'::'分隔。
This function takes a `vocab_words` list and returns a string
with the prefix and the words with prefix applied, separated
by ' :: '. "
我知道字符串中的前缀始终是 vocab_words[0]。
我试过了
def make_word_groups(vocab_words):
return ' :: ' .join(vocab_words[0] for i in vocab_words[0:])
它不起作用。我收到 AssertionError。结果 - 有很多前缀,然后是一些带前缀的单词。
【问题讨论】:
-
您愿意提供一些示例输入和输出吗?
-
input_data = ['auto', 'didactic', 'graph', 'mate', 'chrome', 'centric', 'complete', 'echolalia', 'encoder', 'biography' ] input_data = ['en','circle', 'fold', 'close','joy', 'lighten', 'tangle', 'able', 'code', 'culture'] input_data = ['pre' , '服务', '处置', '位置', '必要', 'digest', 'natal', 'addressed', 'adolescent', 'assumption', 'mature', 'compute'] 我有四组不同前缀的单词
-
和 result_data = ('auto :: autodidactic :: autograph :: 自动化 :: autochrome :: ' 'autocentric :: autocomplete :: autoecholalia :: autoencoder :: ' '自传')
-
您的结果似乎不一致。请检查
-
如果我猜对了,那么你的输出应该是
auto :: autodidactic :: autograph :: automate :: autochrome :: autocentric :: autocomplete :: autoecholalia :: autoencoder :: autobiography,对吗?