【问题标题】:How can I access and modify an element in a list if the element is a sentence?如果元素是句子,如何访问和修改列表中的元素?
【发布时间】:2022-07-27 01:36:59
【问题描述】:

假设我在 python 中有以下列表

List= ['Car XXX_2010 Type', 'Length: 14.7', 'Width: 5.8', 'Height: 5.6']

如何从列表的第一个元素中删除“类型”一词,同时保留元素的其余部分?

换句话说,我怎样才能得到这样的输出:

List= ['Car XXX_2010', 'Length: 14.7', 'Width: 5.8', 'Height: 5.6']

【问题讨论】:

  • List[0] = List[0].replace(" Type", "")

标签: python


【解决方案1】:

您可以像这样简单地使用.replace() 函数:List[0] = List[0].replace("Type", "")

.replace 接受两个参数:要替换的字符串和要替换的字符串。然后它返回一个带有替换的新列表

您可以阅读更多关于.replacehere的信息

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多