Python Forum
can't remove element from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't remove element from a list
#1
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:---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-91-521c272a0c4b> in <module>
27 print(case)
28 else:
---> 29 first_case_num.remove(case)
30
31

AttributeError: 'str' object has no attribute 'remove'
Reply
#2
What is num_case?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
i updated my code , that you can see . and sorry
Reply
#4
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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.
Reply
#6
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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 .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sume Every 10 element in the list and Divided of Sum quest_ 7 148 Nov-27-2020, 10:58 AM
Last Post: perfringo
  Counting Element in Multidimensional List quest_ 1 96 Nov-25-2020, 10:00 PM
Last Post: quest_
  Adding List Element if Second part of the List Elements are the Same quest_ 3 130 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Removing element from list squall 6 189 Nov-22-2020, 09:34 PM
Last Post: jefsummers
  2d Array adds last element to entire list waiteup 2 105 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  How to get index of minimum element between 3 & 8 in list Mekala 2 171 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Remove double quotes from the list ? PythonDev 22 676 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Problem printing last element from a list tester_V 3 200 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Remove specific elements from list with a pattern Xalagy 3 344 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 288 Sep-28-2020, 02:40 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020