I want to delete the list of 2D arrays

Asked 2 years ago, Updated 2 years ago, 17 views

I am having trouble deleting the elements of the 2D array.I would like to delete the [] part without numbers.
Thank you for your cooperation.

 list=[0,1],[],[],[],[1,0],[],[],[1,1],[1,2],[],[],[],[]

python

2022-09-29 22:25

6 Answers

 li=[0,1],[],[],[],[1,0],[],[],[1,1],[1,2],[],[],[],[]
li = [x for x in li if x ]

That's enough.
Also, in Python 3, filter() returns the iterator.


2022-09-29 22:25

If you really want to remove elements from the list

for i in range(len(ll)-1,-1,-1):
  if not l[i]:
    dell[i]

You can do it with

However, memory may save some money, but it may not be beneficial because it costs a lot of money to delete elements.As you can see in the other answers, I think it would be easier to write a new list using list inclusion.


2022-09-29 22:25

I was concerned about the speed difference between methods, so I measured the execution time by referring to your answers and comments.
If you don't care about memory, you'd better make a new list.

  • by_filter
    The shortest run time was using filter.
  • by_compression
    The inclusion is a little slower than filter, but it doesn't change much.
  • by_loop
    The way to add it in the loop is very slow, perhaps because append is slow.
  • by_loop_inplace
    Removing elements without creating a new list is much slower.
    The amount of time you want to delete (empty list) will greatly affect the execution time.
    However, even if there are few objects to delete, it seems to be slightly slower than filter and inclusion.

Below is the result of 1/10th of 500,000 elements being an empty list.

$./del_empty.py 
         450015 function calls in 29.540 seconds

   Ordered by —cumulative time

   ncalls total time percall filename —lineno (function)
        127.775 27.775 27.775 27.775 27.775 del_empty.py:16(by_loop_inplace)
        40.000.000 1.582 0.395 del_empty.py:22(make_list)
        4 1.582 0.395 1.582 0.395 del_empty.py:23(<listcomp>)
        10.0950.0950.1380.138 del_empty.py:3(by_loop)
   450000 0.044 0.000 0.044 0.000 {method'append' of 'list' objects}
        10.000.000.0.03000.030del_empty.py:10(by_comprehension)
        10.03000.033000.030del_empty.py:11(<listcomp>)
        10.0150.0150.0150.015del_empty.py:13(by_filter)
        10.000.000.000.000.000 {method'disable' of '_lsprof.Profiler' objects}
        10.000.000.000.000.000 {build-in methodlen}

del_empty.py

#!/usr/bin/python3

def by_loop(target_list):
    new_list = [ ]
    for vin target_list:
        if v:
            new_list.append(v)
    return new_list

def by_compression(target_list):
    return [ x for x intarget_list if x ]

def by_filter(target_list):
    return list (filter(None, target_list))

def by_loop_inplace(target_list):
    for i in range(len(target_list)-1,-1,-1):
        if not target_list[i]:
            del target_list[i]
    return target_list

def make_list():
    return [[x,0] if x%10 else[] for x in range (500000)]


if__name__=="__main__":
    import cProfile
    pr=cProfile.Profile()
    pr.enable()

    by_loop(make_list())
    by_compression(make_list())
    by_filter(make_list())
    by_loop_inplace(make_list())

    pr.disable()
    pr.print_stats(sort="cumtime")


2022-09-29 22:25

Python 2.7

>>>l=[0,1],[],[],[1,0],[],[1,1],[1,2],[],[],[],[],[]]
>> filter (None, l)
[[0, 1], [1, 0], [1, 1], [1, 2]]


2022-09-29 22:25

Python 2.7

>>>list=filter (lambdax:x!=[], list)
>> list
[[0, 1], [1, 0], [1, 1], [1, 2]]


2022-09-29 22:25

list is standard for Python, so you should avoid overwriting it.
(It's just a variable, so I don't mind if you know it.)
Below is "li".

Note: In response to Kenji Noguchi's comment, we stopped comparing it to [] and made use of the empty sequence as False.

Basically, we will create a new list and add it to it, and if possible, we will tie "li" to that new list.

 li=[0,1],[],[],[],[1,0],[],[],[1,1],[1,2],[],[],[],[]
new_li = [ ]

for vin li:
    if v:
        new_li.append(v)
li = new_li

Included version (I'm doing almost the same thing as argus's answer)

 li=[0,1],[],[],[],[1,0],[],[],[1,1],[1,2],[],[],[],[]

li = [x for x in li if x ]

If you don't make a new list, it will be messy as follows, and I don't think this will improve the performance.(I don't know if the memory is tight.)

Note: Tatz Sekine's answer introduces how to look at the end of the list.That one is definitely better.
Note: Execution time has been measured.I chose a different answer because it was long, but the deletion of the elements was much slower than I expected.

 li=[0,1],[],[],[],[1,0],[],[],[1,1],[1,2],[],[],[],[]

del_count = 0
for i in range (len(li)) :
    i-=del_count
    if not li [i]:
        delli[i]
        del_count+=1


2022-09-29 22:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.