python - List.remove method not applicable as the function with the map builtin?

2024/9/21 5:33:59

Question

Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin?

%%timeit
import random
m = _max = 10000
n = random.randint(1, m)outer = random.sample(population=range(1, 2*n+1), k=2*n)
inner = random.sample(population=range(1, n+1), k=n)
print(f"outer is {outer}")
print(f"inner is {inner}")# 622 ms ± 216 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# [o for o in outer if o not in inner]# 347 ms ± 139 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
for i in inner:outer.remove(i)
print(outer)
---outer is [16, 5, 9, 2, 8, 12, 1, 4, 7, 13, 14, 10, 3, 15, 6, 11]
inner is [4, 1, 5, 8, 6, 7, 2, 3]
[16, 9, 12, 13, 14, 10, 15, 11]

I thought it was applicable with the map but it returned a list of None. Please advise if it can be applied in some way, or the reason not applicable. Please point out if I made a mistake.

#%%timeit
import random
m = _max = 10000
n = random.randint(1, m)outer = random.sample(population=range(1, 2*n+1), k=2*n)
inner = random.sample(population=range(1, n+1), k=n)
print(f"outer is {outer}")
print(f"inner is {inner}")print(outer.remove)
print(list(map(outer.remove, inner)))
---outer is [11, 3, 1, 5, 12, 9, 8, 4, 10, 6, 7, 2]
inner is [1, 3, 4, 6, 5, 2]
<built-in method remove of list object at 0x7f470b6fbd00>
[None, None, None, None, None, None]
Answer

It works as expected. remove affects the initial list but returns nothing (None). Print outer again and it should be shorter.

https://en.xdnf.cn/q/119823.html

Related Q&A

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…

Join Operation for Dictionary in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to nest a list based on increasing sequences and ignore left overlapping ranges

This is my input mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]from 2 to 11, its increasing, so we need to grab the min max [2, 11] from 7 to 10 its increasing, but we need to ignore it because the …

Float comparison (1.0 == 1.0) always false

Im using the following function in Python 2.7.3 and Kivy 1.8.0 to fade-in a Grid widget:def __init__(self, **kwargs):# ...Init parent class here...self.grid.opacity = 0.0Clock.schedule_interval(self.sh…