例如我有以下列表:
contents= ["i have two pens","prices = 5$","made in ____ and ____"]
我想将它们分开,使其具有与以下内容相同的内容:
array[0]= ["i", "have", "two", "pens", ""]
array[1]= ["prices", "=", "5$", " ", " "]
array[2]= ["made", "in", "____", "and" ,"____"]
这意味着每个数组具有相同数量的内容(此处为 5)。我正在使用的代码是:
array = [phrase.split() for phrase in contents]
但可以肯定的是,它不会以相同的内容分割它们。谁能建议我如何使用 python 解决这个问题?
您需要检查哪个是最长的数组,然后填充其他数组,如下所示:
array = [phrase.split() for phrase in contents]
x = max(len(i) for i in array)
result = [i + [''] * (x - len(i)) for i in array]
很复杂,但可以为您提供所需的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)