简体   繁体   中英

How to turn a list of integers to integers only (python)

在不使用循环的情况下,有没有办法将诸如 [88, 88, 88] 之类的整数字符串转换为 88 88 88 (仅用空格分隔的整数)?

You contradict yourself: at one point, you say you have a list of integers, and later, you say you have a string "[88, 88, 88]". Here's how to make both into your desired string.

In case of list:

string = " ".join(list)

Should suffice. The string method join() returns a string with all the elements in a list, separated by another string (in this case, " ").

In case of string:

import re
new_string = re.sub('[\[\]\,]', '', old_string)

Good ol' regex comes in handy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM