print('[{}]'.format(' '.join(map(str, output_list))))改写代码,输出不要有逗号
时间: 2023-08-18 09:10:07 浏览: 21
你可以将以上代码修改为以下形式,以去除输出中的逗号:
```
print('[{}]'.format(' '.join(map(str, output_list))))
```
这将使用空格作为分隔符将 `output_list` 中的元素连接成一个字符串,并将其包装在方括号中,从而生成所需的输出,且不会出现逗号。
相关问题
print('[{}]'.format(' '.join(map(str, output_list))))改写代码,输出末尾不要有逗号
你可以将以上代码修改为以下形式,以去除输出末尾的逗号:
```
print('[{}]'.format(' '.join(map(str, output_list))), end='\n')
```
这将使用空格作为分隔符将 `output_list` 中的元素连接成一个字符串,并将其包装在方括号中,从而生成所需的输出。`end='\n'` 指定在输出结束时添加换行符,以避免在输出末尾添加逗号。
print('[{}]'.format(' '.join(map(str, output_list))))输出末尾有逗号,不要逗号
可以使用字符串切片来去掉末尾的逗号,如下所示:
```
output_str = ' '.join(map(str, output_list))
if output_str.endswith(','):
output_str = output_str[:-1]
print('[{}]'.format(output_str))
```
这样就可以在输出时去掉末尾的逗号了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)