Bottom Page

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'
Quote
#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.
Quote
#3
i updated my code , that you can see . and sorry
Quote
#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.
Quote
#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.
Quote
#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.
Quote
#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 .
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding an element in a 1d list in a 2d array lionrocker221 0 230 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  removing dictionary element in list using (key, value) MelonMusk 3 244 Jun-13-2020, 02:37 PM
Last Post: buran
  using element on a list as condition statement glennford49 11 548 May-21-2020, 12:53 PM
Last Post: hussainmujtaba
  count each element in a list glennford49 9 483 May-01-2020, 11:20 AM
Last Post: glennford49
  Alpha numeric element list search rhubarbpieguy 1 225 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  Remove all \n from end of list items JackMack118 4 22,853 Dec-27-2019, 08:34 AM
Last Post: perfringo
  Why does pop() removes an element from more than one list? albufork 5 357 Nov-17-2019, 08:01 AM
Last Post: buran
  get the one element from 1-list or 1-set Skaperen 1 279 Nov-05-2019, 11:50 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 420 Oct-12-2019, 09:25 AM
Last Post: konsular
  weird result trying to remove numbers from a list Exsul 6 579 Aug-27-2019, 05:10 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)