can't remove element from a list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: can't remove element from a list (/thread-22277.html) |
can't remove element from a list - yokaso - Nov-06-2019 i have a list and i want to remove all non digit element from it,but got some problem. there is my code , can you help me: match = re.compile(r'ROL N°.[0-9][0-9].') case_title = re.sub(match,'',expression) num_case= match.search(expression) first_case_num=[] first_case_num = num_case.group() second_case_num= num_case.group() for case in first_case_num: if case.isdecimal(): pass else: first_case_num.remove(case) Quote:--------------------------------------------------------------------------- RE: can't remove element from a list - perfringo - Nov-06-2019 What is num_case? RE: can't remove element from a list - yokaso - Nov-06-2019 i updated my code , that you can see . and sorry RE: can't remove element from a list - perfringo - Nov-06-2019 As of error I think that this is pretty straightforward: it's shows the row and the problem: first_case_num is string object, not list and therefore Python can't execute this. But what is your input list and what is expected output? RE: can't remove element from a list - yokaso - Nov-06-2019 i really confused, in my code i want to get 10 first element of a string(in this code it's "expression") and affect them to first_case_num , after that i will remove from first_case_num any element who is not numeric. RE: can't remove element from a list - perfringo - Nov-06-2019 I address the task which is described as 'i want to get 10 first element of a string /../ remove /../ any element who is not numeric.' To get first 10 elements from string one can use slice (just to remind that non-printable characters/elements in string are as their printable counterparts also characters/elements): >>> s = '123abc456def789' >>> [char for char in s[:10] if char.isnumeric()] ['1', '2', '3', '4', '5', '6'] >>> ''.join(char for char in s[:10] if char.isnumeric()) '123456' RE: can't remove element from a list - yokaso - Nov-06-2019 thank you for your response, and it help to get see new idea how to solve the problem. maybe i didn't explain you well and i am sorry. when i told you 10 characters it was just to give you an example and simplify . i need to use regex because the expression that i use to extract the digits can contain white spaces or other special characters . |