我遇到了将列表:list=['1','2','3']
转换为' "1","2","3" '
。
我的代码是:
str(",".join('"{}"'.format(i) for i in list))
但我的输出是:
"1","2","3"
代替' "1","2","3" '
我可以提出你的建议吗?感谢。
答案 0 :(得分:2)
我看到你不想只是将所需的子串(单引号和空格)添加/附加到关键字符串。
这是一个带有var tmpij = [];
// Set 7 empty arrays in tmpij
for(var x = 0; x < 7; x++){
tmpij.push([]);
}
// Now the array contains 7 arrays at index positions 0 - 6 and you can now access them with [][] syntax
var i = 0, j = 1;
tmpij[0][i] = "testing i";
tmpij[0][j] = "testing j";
console.log(tmpij);
功能的技巧:
str.replace()
输出:
lst = ['1','2','3']
result = "' - '".replace('-', ",".join('"{}"'.format(i) for i in lst))
print(result)
' "1","2","3" '
- 充当占位符或与其他"' - '"
函数调用相同:
str.format()