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
  Remove all \n from end of list items JackMack118 4 478 Dec-27-2019, 08:34 AM
Last Post: perfringo
  Why does pop() removes an element from more than one list? albufork 5 150 Nov-17-2019, 08:01 AM
Last Post: buran
  get the one element from 1-list or 1-set Skaperen 1 101 Nov-05-2019, 11:50 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 193 Oct-12-2019, 09:25 AM
Last Post: konsular
  weird result trying to remove numbers from a list Exsul 6 312 Aug-27-2019, 05:10 AM
Last Post: perfringo
  How to remove multiples in a list jasper100125 6 393 Aug-20-2019, 06:38 AM
Last Post: buran
  Select a part of an element of a list with Index BollerwagenIng 0 335 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng
  Remove \n from list of values within a pandas columns klllmmm 2 795 Jun-24-2019, 05:16 AM
Last Post: klllmmm
  Remove \n at the end of a character from a list judkil 2 331 Jun-24-2019, 12:15 AM
Last Post: DeaD_EyE
  Replace a list or string element with a random one.. pianistseb 3 373 May-09-2019, 08:24 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)