如果我有
LIST=[['SPORTS','NEW YORK JETS']]
并且我想将子列表的第二个元素 (NEW YORK JETS
) 替换为 ___ ____ ____
,我该怎么做?
到目前为止,我已经:
hide = ""
for l in LIST:
if l[1] != " ":
hide = hide + "_"
else:
hide = hide + " "
print(hide)
这没有给我想要的输出。如果有任何建议,我将不胜感激。
谢谢!
最佳答案
您必须在嵌套循环中循环遍历字符串中的字符,并将非空格字符替换为 _
。
result = []
for word1, word2 in LIST:
hide = ""
for c in word2:
if c == " ":
hide += " "
else:
hide += "_"
result.append([word1, hide])
关于python - 如何用破折号替换嵌套的短语列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60355474/