I'd like to take the list off the list

Asked 2 years ago, Updated 2 years ago, 45 views

For example, if you have [1,2,3,4,3,2] on the list a and [1,2,3] on the list b, if you take out the element of the b list from the list a, you're only going to get [4] But is there a way to make [4,3,2] come out?

python list

2022-09-20 19:59

2 Answers

If the data is a single number, it can be made into a string and easily obtained by replacing it.

a, b = [1, 2, 3, 4, 3, 2], [1, 2, 3]
str_a = ''.join(map(str, a))
str_b = ''.join(map(str, b))
str_a.replace(str_b, '')
'432'


2022-09-20 19:59

It's probably something like this.

I hope I didn't do the homework you should have done instead.

def list_diff_by_whole_match(target, criteria, returnRaw = False) :

    # Simply ignorant, traverses all the way in index units.
    for i in range(0, len(target)) :
        matched = True
        for j in range(0, len(criteria)) :
            matched = matched and target[i + j] == criteria[j]

        # We've confirmed that all the elements given to the criteria appear in order, so we make them None.
        # (Caution not to del)
        if matched :
            for j in range(0, len(criteria)) :
                target[i + j] = None

    # Pulls out and returns only what you need, or returns the original as a result of the inspection.
    return target if returnRaw else list(filter(None, target))

# Testing
v = [1,2,3,4,3,2,1,2,4,1,2,3,4]
w = [1,2,3]
print(list_diff_by_whole_match(v, w)) # [4, 3, 2, 1, 2, 4, 4]


2022-09-20 19:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.