gpt4 book ai didi

python - 从列表中的字符串项中删除一个 substr

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:47 26 4
gpt4 key购买 nike

list = [ 'u'adc', 'u'toto', 'u'tomato', ...]

我想要的是得到这样的列表: list2 = [ 'adc', 'toto', 'tomato'... ]

你能告诉我如何在不使用正则表达式的情况下做到这一点吗?我正在尝试:

for item in list:
list.extend(str(item).replace("u'",''))
list.remove(item)

但这最终会给出某种形式的东西 [ 'a', 'd', 'd', 'm'...]

在列表中我可以有任意数量的字符串。

最佳答案

你可以像这样将它编码为“utf-8”:

list_a=[ u'adc', u'toto', u'tomato']
list_b=list()
for i in list_a:
list_b.append(i.encode("utf-8"))
list_b

输出:

['adc', 'toto', 'tomato']

或者你可以使用str函数:

list_c = list()
for i in list_a:
list_c.append(str(i))
list_c

输出:

['adc', 'toto', 'tomato']

关于python - 从列表中的字符串项中删除一个 substr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com