为 Python 中的词组添加前缀
Add prefixes to word groups in Python
如何使用 .join 函数为给定单词组中的每个单词添加前缀?
:param vocab_words: list of vocabulary words with a prefix.
:return: str of prefix followed by vocabulary words with
prefix applied, separated by ' :: '.
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。结果 - 很多前缀,然后只有一些带前缀的单词。
试试这个:
def make_word_groups(vocab_words):
separator = ' :: '
prefix = vocab_words[0]
words = vocab_words[1:]
prefixed_words = [prefix + word for word in words]
result = prefix + separator + separator.join(prefixed_words)
return result
你可以试试这个:
def make_word_groups(vocab_words):
result_data = f'{vocab_words[0]} :: '
result_data += ' :: '.join([vocab_words[0] + word for word in vocab_words[1:]])
return result_data
input_data = ['auto', 'didactic', 'graph', 'mate', 'chrome', 'centric', 'complete', 'echolalia', 'encoder', 'biography']
print(make_word_groups(input_data))
input_data = ['en' ,'circle', 'fold', 'close','joy', 'lighten', 'tangle', 'able', 'code', 'culture']
print(make_word_groups(input_data))
input_data = ['pre', 'serve', 'dispose', 'position', 'requisite', 'digest', 'natal', 'addressed', 'adolescent', 'assumption', 'mature', 'compute']
print(make_word_groups(input_data))
输出:
auto :: autodidactic :: autograph :: automate :: autochrome :: autocentric :: autocomplete :: autoecholalia :: autoencoder :: autobiography
en :: encircle :: enfold :: enclose :: enjoy :: enlighten :: entangle :: enable :: encode :: enculture
pre :: preserve :: predispose :: preposition :: prerequisite :: predigest :: prenatal :: preaddressed :: preadolescent :: preassumption :: premature :: precompute
试试这个代码:
def make_word_groups(vocab_words):
prefix = vocab_words[0]
vocabulary_words = vocab_words[1:]
for i in range(len(vocabulary_words)):
vocabulary_words[i] = prefix + vocabulary_words[i]
vocabulary_words = [prefix] + vocabulary_words
result = ' :: '.join(vocabulary_words)
return result
如何使用 .join 函数为给定单词组中的每个单词添加前缀?
:param vocab_words: list of vocabulary words with a prefix.
:return: str of prefix followed by vocabulary words with
prefix applied, separated by ' :: '.
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。结果 - 很多前缀,然后只有一些带前缀的单词。
试试这个:
def make_word_groups(vocab_words):
separator = ' :: '
prefix = vocab_words[0]
words = vocab_words[1:]
prefixed_words = [prefix + word for word in words]
result = prefix + separator + separator.join(prefixed_words)
return result
你可以试试这个:
def make_word_groups(vocab_words):
result_data = f'{vocab_words[0]} :: '
result_data += ' :: '.join([vocab_words[0] + word for word in vocab_words[1:]])
return result_data
input_data = ['auto', 'didactic', 'graph', 'mate', 'chrome', 'centric', 'complete', 'echolalia', 'encoder', 'biography']
print(make_word_groups(input_data))
input_data = ['en' ,'circle', 'fold', 'close','joy', 'lighten', 'tangle', 'able', 'code', 'culture']
print(make_word_groups(input_data))
input_data = ['pre', 'serve', 'dispose', 'position', 'requisite', 'digest', 'natal', 'addressed', 'adolescent', 'assumption', 'mature', 'compute']
print(make_word_groups(input_data))
输出:
auto :: autodidactic :: autograph :: automate :: autochrome :: autocentric :: autocomplete :: autoecholalia :: autoencoder :: autobiography
en :: encircle :: enfold :: enclose :: enjoy :: enlighten :: entangle :: enable :: encode :: enculture
pre :: preserve :: predispose :: preposition :: prerequisite :: predigest :: prenatal :: preaddressed :: preadolescent :: preassumption :: premature :: precompute
试试这个代码:
def make_word_groups(vocab_words):
prefix = vocab_words[0]
vocabulary_words = vocab_words[1:]
for i in range(len(vocabulary_words)):
vocabulary_words[i] = prefix + vocabulary_words[i]
vocabulary_words = [prefix] + vocabulary_words
result = ' :: '.join(vocabulary_words)
return result