【问题标题】:How can I remove same word from the list?如何从列表中删除相同的单词?
【发布时间】:2019-08-27 14:30:13
【问题描述】:

我想从列表中的所有元素中删除相同的单词score

scorelist = ['100score', '200score', '300score'] 

scorelist = ['100', '200', '300']

【问题讨论】:

  • 你使用什么语言?
  • python 编程

标签: python string list


【解决方案1】:

根据您的 cmets,您使用的是 Python。 How about.replace()?

scorelist = ['100score', '200score', '300score']
newlist = []
for score in scorelist:
  newlist.append(score.replace('score',''))

print(newlist)

Try it here!

在 python 中,字符串是不可变的——这意味着它们不能被改变。仅仅做score.replace('score', '') 是不够的——这个函数返回一个字符串,然后我们可以捕获并使用它。

祝你好运!

【讨论】:

    【解决方案2】:

    在 PHP 中

    function test_alter(&$item1, $key, $prefix)
    {
    
        //  replace string
        $item1= str_replace($prefix, "", $item1);
    
    }
    
    array_walk($scorelist, 'test_alter', 'score');
    

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2020-01-28
      • 2013-04-01
      • 1970-01-01
      • 2018-09-28
      • 2016-03-13
      • 2011-03-31
      • 1970-01-01
      • 2017-02-09
      相关资源
      最近更新 更多