-1

I have a syntax as follows:

a = []

data = ['2007-01-03', '2007-01-04', '2007-01-05','2007-01-03', '2007-01-04', '2007-01-05']
data1 = ['2007-01-04', '2007-01-05']

data = np.array(data)
data1 = np.array(data1)

for idx, day in enumerate(data):
    for idx1, day1 in enumerate(data1):
          print(np.where(day == day1)[0])

Outcome as follows:

[]
[0]
[]
[]
[0]
[]

How should i fix the syntax in order to get the following outcome?

[1, 2, 4, 5]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Does this answer your question? [How to find list intersection?](https://stackoverflow.com/questions/3697432/how-to-find-list-intersection) – JonSG Sep 05 '21 at 17:25
  • 1
    Does this answer your question? [How can I compare two lists in python and return matches](https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches) – Trenton McKinney Sep 05 '21 at 19:57
  • 2
    @NivDudovitch in this case the question is a duplicate and should be closed. – Trenton McKinney Sep 05 '21 at 19:58
  • 1
    Does this answer your question? [Finding indices of matches of one array in another array](https://stackoverflow.com/q/33678543/7758804) – Trenton McKinney Sep 05 '21 at 21:28
  • The accepted answer here is a duplicate of the accepted answer for [compare two lists in python and return indices of matched values](https://stackoverflow.com/q/10367020/7758804) and should be closed – Trenton McKinney Sep 06 '21 at 15:00
  • i agreed after ran thru the few links. thanks for help, and i am not sure how to close the post. kindly help. thanks! – DreamyDeerz Sep 06 '21 at 15:18

1 Answers1

0
res = [key for key, val in enumerate(data) if val in data1]
print(res)

Output:

[1, 2, 4, 5]
Niv Dudovitch
  • 1,614
  • 7
  • 15
  • 2
    This answer is exactly the same as the accepted answer to [compare two lists in python and return indices of matched values](https://stackoverflow.com/q/10367020/7758804). `set(data1)` is not required here, because `data1` values are unique. – Trenton McKinney Sep 05 '21 at 21:30
  • how to understand this syntax as i am not familiar with this format. what is the purpose of first "key"? who should i rewrite in in the following format: for key, val in enumerate(data): if val in data1 – DreamyDeerz Sep 06 '21 at 01:25
  • @DreamyDeerz see https://stackoverflow.com/q/34835951/7758804 and accept the close as a duplicate – Trenton McKinney Sep 06 '21 at 03:20
  • https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions & https://docs.python.org/3/tutorial/datastructures.html#nested-list-comprehensions – Trenton McKinney Sep 06 '21 at 03:28