Compare values of two arrays in python

2024/9/27 17:05:16

How can i check if item in b is in a and the found match item in a should not be use in the next matching?

Currently this code will match both 2 in b.

a = [3,2,5,4]
b = [2,4,2]for i in b:if i in a:print "%d is in a" % i

This is the required output:

2 => 2 is in a
4 => 4 is in a
2 =>

EDIT: Example 2:

a = [3,2,2,4]
b = [2,4,2]

output should be

2 => 2 is in a
4 => 4 is in a
2 => 2 is in a
Answer

(long post but read it entirely, solution is at the end).

Remove the found value or register it in another dict.

Better though is to count the number of apparitions inside each array and test how many are common.

For the second case, you'd have

  • for a:

    3 appears 1 times 2 appears 1 times 5 appears 1 times 4 appears 1 times

  • for b:

    2 appears 2 times 4 appears 1 times

Keep these values in dictionaries:

a_app = {3:1, 2:1, 5:1, 4:1}
b_app = {2:2, 4:1}

And now, it is simple:

for i in b:if a_app.has_key(i) and a_app[i] > 0:a_app[i] -= 1

The b_app dictionary would be used in other case.

Here is a test script I wrote (testing all testcases issued here):

def f(a, b):a_app = {}for i in a:if not a_app.has_key(i):a_app[i] = 0a_app[i] += 1print a_appfor i in b:print i, '=>',if a_app.has_key(i) and a_app[i] > 0:a_app[i] -= 1print i, ' is in a',print '.'f([1,1,2],[1,1])
f([3,2,5,4],[2,4,2])
f([3,2,2,4],[2,4,2])
f([3,2,5,4],[2,3,2])

And here is the output:

$ python 1.py
{1: 2, 2: 1}
1 => 1  is in a .
1 => 1  is in a .
{2: 1, 3: 1, 4: 1, 5: 1}
2 => 2  is in a .
4 => 4  is in a .
2 => .
{2: 2, 3: 1, 4: 1}
2 => 2  is in a .
4 => 4  is in a .
2 => 2  is in a .
{2: 1, 3: 1, 4: 1, 5: 1}
2 => 2  is in a .
3 => 3  is in a .
2 => .

Everything is perfect and no order is lost :)

Edit: Updated with @Avaris's suggestions, this script looks like:

import collectionsdef f(a, b):a_app = collections.Counter(a)for i in b:print i, '=>',if i in a_app and a_app[i] > 0:a_app[i] -= 1print i, ' is in a',print '.'print ''f([1,1,2],[1,1])
f([3,2,5,4],[2,4,2])
f([3,2,2,4],[2,4,2])
f([3,2,5,4],[2,3,2])
https://en.xdnf.cn/q/71418.html

Related Q&A

How to count the number of digits in numbers in different bases?

Im working with numbers in different bases (base-10, base-8, base-16, etc). Im trying to count the number of characters in each number. ExampleNumber: ABCDEF Number of digits: 6I know about the method …

Pandas KeyError using pivot

Im new to Python and I would like to use Python to replicate a common excel task. If such a question has already been answered, please let me know. Ive been unable to find it. I have the following p…

Not found: Container localhost does not exist when I load model with tensorflow and flask

I am a newbie research Deeplearning. I load a saved model with tensorflow and made a API with flask but I get error Container localhost does not exist. when I predict, please help me fix it. Thank you.…

Python: simplifying nested FOR loop?

I am wondering if there is a way to simplify the nested loop below. The difficulty is that the iterator for each loop depends on things from the previous loops. Here is the code:# Find the number of co…

NLTK Data installation issues

I am trying to install NLTK Data on Mac OSX 10.9 . The download directory to be set, as mentioned in NLTK 3.0 documentation, is /usr/share/nltk_data for central installation. But for this path, I get …

Why does the simplest requests_mock example fail with pytest?

I have a peculiar problem with requests_mock. I want to use it with pytest to test my API wrapper library.Ive tried to use the first example in the requests_mock docs, except I put it in a test_mock()-…

Error installing PyCurl

I tried installing pycurl via pip. it didnt work and instead it gives me this error.running installrunning buildrunning build_pyrunning build_extbuilding pycurl extensiongcc-4.2 -fno-strict-aliasing -f…

Serializing objects containing django querysets

Django provides tools to serialize querysets (django.core.serializers), but what about serializing querysets living inside other objects (like dictionaries)?I want to serialize the following dictionar…

How do I list my scheduled queries via the Python google client API?

I have set up my service account and I can run queries on bigQuery using client.query(). I could just write all my scheduled queries into this new client.query() format but I already have many schedule…

What does conda env do under the hood?

After searching and not finding, I must ask here:How does conda env work under the hood, meaning, how does anaconda handle environments?To clarify, I would like an answer or a reference to questions l…