如果我有一个类似的列表:
["1","2","3"]
如何打印列表并同时将单个 1 附加到数字 3 ?
谢谢
尝试这个:
print [x if x != "3" else "1" + "3" for x in ["1","2","3"]]
或这个:
print [x if x != "3" else "3" + "1" for x in ["1","2","3"]]
或这个:
print [x if x != "3" else str(1+int("3")) for x in ["1","2","3"]]
这实际上取决于您对“将单个 1 添加到数字 3”的意思。
也许您正在尝试实现这一目标:
print [str(int(i) + 1) if i == '3' else i for i in ['1', '2', '3']]
>>> ['1', '2', '4']
?