gpt4 book ai didi

python - 如何从字符串列表中删除连字符

转载 作者:行者123 更新时间:2023-12-04 11:51:24 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to delete all instances of a character in a string in python?

(6 个回答)


4年前关闭。



['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']

如何删除数字之间的所有连字符?

最佳答案

您可以使用 for 循环进行迭代,并将连字符的每个实例替换为空白。

hyphenlist = ['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
newlist = []

for x in hyphenlist:
newlist.append(x.replace('-', ''))

这段代码应该给你一个没有连字符的新列表。

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

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