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
  removing one list element without using its index paul18fr 7 1,288 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 2,222 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  extract an element of a list into a string alexs 5 4,059 Aug-30-2024, 09:24 PM
Last Post: alexs
  remove duplicates from dicts with list values wardancer84 27 6,095 May-27-2024, 04:54 PM
Last Post: wardancer84
  element in list detection problem jacksfrustration 5 1,940 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  unable to remove all elements from list based on a condition sg_python 3 1,743 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 1,727 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 2,277 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Remove numbers from a list menator01 4 3,816 Nov-13-2022, 01:27 AM
Last Post: menator01
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 3,535 Oct-26-2022, 04:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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