4 回答
TA贡献1886条经验 获得超2个赞
这是在 itertools(在 python3 中)中提供的,用于迭代,带有 zip_longest,您可以使用 zip(*) 正常反转它,如果您更喜欢它而不是迭代器,则将其传递给列表。
import itertools
from pprint import pprint
sents = [["Hello","World"],["Where","are","you"],["I","am","doing","fine"]]
pad_token = "Hi"
padded = zip(*itertools.zip_longest(*sents, fillvalue=pad_token))
pprint (list(padded))
[['Hello', 'World', 'Hi', 'Hi'],
['Where', 'are', 'you', 'Hi'],
['I', 'am', 'doing', '美好的']]
TA贡献1810条经验 获得超4个赞
以下是如何使用str.ljust()填充每个字符串,并使用max()keylen来查找填充每个字符串的数字:
lst = ['Hello World', 'Good day!', 'How are you?']
l = len(max(lst, key=len)) # The length of the longest sentence
lst = [s.ljust(l) for s in lst] # Pad each sentence with l
print(lst)
输出:
['Hello World ',
'Good day! ',
'How are you?']
TA贡献1811条经验 获得超4个赞
假设:
输出应与 OP 输出相同(即每个子列表中的单词数相同)。
输入:
sents = [["Hello","World"],["Where","are","you"],["I","am","doing","fine"]]
pad_token = "Hi"
以下 1-liner 产生与 OP 代码相同的输出。
sents_padded = [sent + [pad_token]*(max_length - len(sent)) for sent in sents]
print(sents_padded)
# [['Hello', 'World', 'Hi', 'Hi'], ['Where', 'are', 'you', 'Hi'], ['I', 'am', 'doing', 'fine']
TA贡献1836条经验 获得超3个赞
当我计时时,这似乎更快:
maxi = 0
for sent in sents:
if sent.__len__() > maxi:
maxi = sent.__len__()
for sent in sents:
while sent.__len__() < maxi:
sent.append(pad_token)
print(sents)
添加回答
举报