从每个元素的数组中删除最后一个字符(蟒蛇) [英] Removing last character from each element in array (Python)

查看:94
本文介绍了从每个元素的数组中删除最后一个字符(蟒蛇)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组find_words这是

I have an array find_words which is

[u'Duration$', u'Noun$', u'Adjective$']

我想删除所有的$,因此它看起来像

I would like to remove all the '$' so it looks like

[u'Duration', u'Noun', u'Adjective']

我怎么去呢?此外,我怎么重新添加$为好。

How do I go about this? Also, how do I re-add the '$' as well.

推荐答案

您可以做到这一点简单地用<一个href=\"http://www.youtube.com/watch?v=t85uBptTDYY&list=UUAuqj5Bs5mTTl1mIVDmuAlw&index=1&feature=plcp\">list COM prehension 和 str.rstrip()

You can do this simply with a list comprehension and str.rstrip():

[word.rstrip("$") for word in words]

或添加它们:

[word+"$" for word in words]

例如:

>>> words = ['Duration$', 'Noun$', 'Adjective$']
>>> words = [word.rstrip("$") for word in words]
>>> words
['Duration', 'Noun', 'Adjective']
>>> [word+"$" for word in words]
['Duration$', 'Noun$', 'Adjective$']

这篇关于从每个元素的数组中删除最后一个字符(蟒蛇)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆