从列表中删除特定单词

时间:2020-03-18 10:33:03

标签: python-3.x pandas pandas-groupby

我的列表如下-

[__init__.py:1566] - /edx/app/edxapp/venvs/edxapp/lib/python3.5/site-packages/pkg_resources/__init__.py:1158: DeprecationWarning: Use of .. or absolute path in a resource path is not allowed and will raise exceptions in a future release.

我希望我的最终名单应该像-

mylist = [("aaaa8"),("bb8_null"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),("abc_null"),
         ("tre_null"),("fdsf"),("ccc8"),("dddddd8")]

我到目前为止已经完成了-

final_list = [("aaaa8"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),
         ("fdsf"),("ccc8"),("dddddd8")]

但是它不起作用

1 个答案:

答案 0 :(得分:2)

好像您需要str.endswith

例如:

mylist = [("aaaa8"),("bb8_null"),("ccc8"),("dddddd8"),
         ("aaaa8"),("hsd"),("ccc8"),("abc_null"),
         ("tre_null"),("fdsf"),("ccc8"),("dddddd8")]
final_list = [i for i in mylist if not i.endswith('_null')]
print(final_list)
# --> ['aaaa8', 'ccc8', 'dddddd8', 'aaaa8', 'hsd', 'ccc8', 'fdsf', 'ccc8', 'dddddd8']