Skip to main content
 首页 » 编程设计

python之在 Python 中编辑列表内容

2025年05月04日12softidea

我有一个变量data:

data = [b'script', b'-compiler', b'123cds', b'-algo', b'timing'] 

我需要转换它以删除列表中所有出现的“b”。 我该怎么做?

请您参考如下方法:

不确定它是否有帮助 - 但它适用于您的示例:

initList = [b'script', b'-compiler', b'123cds', b'-algo', b'timing'] 
resultList = [str(x) for x in initList ] 

或者在 P3 中:

resultList = [x.decode("utf-8") for x in initList ] # where utf-8 is encoding used 

在 decode function. 上查看更多信息

您可能还想看看 the following related SO thread.