python - 奇怪的 .Join() 行为
问题描述
出于某种原因,我的代码中的“”.join() 似乎在不应该有的地方添加了额外的空格。抱歉,如果这是一个非常菜鸟的问题,但我不知道尽管通常能够弄清楚这样的事情。
有问题的代码(只是一个片段,但其余部分不相关)
def translate(stringinput):
if all(c in string.printable for c in stringinput):
output = ""
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
sentences = sent_detector.tokenize(stringinput.strip())
for sentence in sentences:
sentence = shuffle(sentence)
output = output + " " + sentence_translate(sentence)
print(output.split())
" ".join(output.split())
return output.strip()
else:
print("Input does not entirely consist of ASCII Characters. Offending characters were:")
print([c for c in stringinput if c not in string.printable])
stringinput = "Ulysses, Ulysses - Soaring through all the galaxies. In search of Earth, flying in to the night. Ulysses, Ulysses - Fighting evil and tyranny, with all his power, and with all of his might. Ulysses - no-one else can do the things you do. Ulysses - like a bolt of thunder from the blue. Ulysses - always fighting all the evil forces bringing peace and justice to all."
print(translate(stringinput))
writer(folder / "final.json", dict)
有问题的行是
print(output.split())
" ".join(output.split())
return output.strip() -> which is then printed out via print(translate(stringinput))
这两个的打印输出是:
['kwmuo', 'kwmuo', 'jhhdd', 'zzazyayb', 'ptictte', 'igbo', 'tkaty', 'puiq.', 'xpaiuc', 'ftucqtze', 'ossjjh', 'ywwuh', 'rpauuqqz', 'fddu', 'pfhqys', 'igbo', 'kwmuo', 'qpousq,', 'zaapyuwq,', 'zqaoys,', 'histje', 'kwmuo', 'uzzaa', 'ptictte', 'eczt', 'rkmwy', 'uzzaa,', 'zaapyuwq,', 'ptictte,', 'xpaiuc,', 'eczt,', 'rssjj', 'kwmuo', 'hydymw', 'mfusq', 'gotsejz', 'igbo', 'mkpwhu', 'mkpwhu', 'os', 'gooss', 'teezc', 'kwmuo', 'dyyww', 'gtokb.', 'xpaiuc', 'cxxppu,', 'uqqzzan', 'igbo', 'gooss', 'kwmuo', 'hdyyyy', 'itfe.', 'uqqlos', 'ptictte', 'igbo', 'zqaoys', 'ywwhuyq', 'zaapyuwq', 'hdyyyy', 'osgjhhy', 'ptictte', 'rpauuqqz']
和
kwmuo kwmuo jhhdd zzazyayb ptictte igbo tkaty puiq. xpaiuc ftucqtze ossjjh ywwuh rpauuqqz fddu pfhqys igbo kwmuo qpousq, zaapyuwq, zqaoys, histje kwmuo uzzaa ptictte eczt rkmwy uzzaa, zaapyuwq, ptictte, xpaiuc, eczt, rssjj kwmuo hydymw mfusq gotsejz igbo mkpwhu mkpwhu os gooss teezc kwmuo dyyww gtokb. xpaiuc cxxppu, uqqzzan igbo gooss kwmuo hdyyyy itfe. uqqlos ptictte igbo zqaoys ywwhuyq zaapyuwq hdyyyy osgjhhy ptictte rpauuqqz
例如,如果您在tkaty和puiq之间查看,它们的两个数组条目显然都没有尾随或前导空格,那么为什么连接的版本之间显然有两个空格?这在整个输出中偶尔持续,没有明显的模式。这是可重现的,我已经多次运行代码,结果完全相同。
有任何想法吗?
解决方案
您必须将join
方法的输出分配给某些东西,它不能就地工作:
print(output.split())
" ".join(output.split())
return output.strip()
应该
print(output.split())
output = " ".join(output.split())
return output.strip()
推荐阅读
- javascript - Javascript从元素中选择不同的名称
- knapsack-problem - 查找总价值大于或等于目标值的最便宜的项目组合
- java - 在控制器 Java 中成功操作后,如何在屏幕上显示成功消息?
- kotlin - 如何在 Kotlin Native 中设置协程调度程序的线程优先级
- flutter - 如何使用 Flutter Gallery 启动应用程序启动 Flutter 项目?
- php - 无法从 Drupal Commerce 2 中的当前会话购物车获取 order_id
- python - 为什么从带有 for 循环的 pandas 数据框创建数组时,我的数组的每个元素都包含在 array([]) 中?
- docker - 未找到 RabbitMQ 指标
- python - 在 Python Flask 中获取用户的位置
- .net - My.Computer.FileSystem.WriteAllText 在文件打开时不会出现任何异常