3

I want to check if any element in noun matches any one of the elements in tables.

First element in nous in "devices". Clearly it matches with "projname:dataset.devices". If a match is found, loop should break, else it should check whether second element in noun, which is "today" matches any element in the tables.

    tables = ["projname:dataset.devices","projname:dataset.attempts"]

    noun = [devices,today]

I tried it with "noun in tables", i got empty result. Is there any other method that I can try with?

Thanks in advance !!

9
  • Have you attempted anything? Commented Jan 13, 2017 at 20:02
  • Please add some context and the expected output. Commented Jan 13, 2017 at 20:03
  • Because tables is a list. A list is a container, you need to check the contents of the container, which is a single string in your example. noun is also a list, and you nee to check the contents of that list against the contents of another list. Commented Jan 13, 2017 at 20:03
  • does the noun list contains lists? Commented Jan 13, 2017 at 20:04
  • @LironLavi: Noun is a list which has two elements. I want to check whether any element in noun list matches partially with any element in devices list. Commented Jan 13, 2017 at 20:06

5 Answers 5

3

A simple use of any(n in s for n in nouns for s in tables) would suffice for a check.

If you actually want the matching item, you could write this quick function:

>>> def first_match(nouns, tables):
...     for n in nouns:
...         for t in tables:
...             if n in t:
...                 return t
...
>>> first_match(nouns,tables)
'projname:dataset.devices'
Sign up to request clarification or add additional context in comments.

1 Comment

If any function returns true, I have to get the corresponding element in tables.
1

Task:

  1. 'Check if any element in noun matches any one of the elements in tables'
  2. 'If any function returns true, I have to get the corresponding element in tables'

Data:

tables = ["projname:dataset.devices","projname:dataset.attempts"]
noun = ['devices','today']

Generator expression:

This only gives the first match, as per OP request.

try:
    print(next(t for n in noun for t in tables if n in t))
except StopIteration:
    pass

Output:

   'projname:dataset.devices'

7 Comments

This is great if you want all matches, if you want to break as soon as you find something for efficiency reasons, though, this won't work.
this look like a more unnecessarily complicate solution of what is really needed.
@ade1e that is better, although it will trow a exception if nothing is found. Also, l think the order is reversed, you first should iterate over noun...
@ade1e by the way the OP describe it, it looks like he want to check the noun in order... Also, just for the record, there is another way to avoid the exception: next(iterable,default) which return the provided default value in case of a empty iterable.
@ade1e the modification is simple, just change the order of iteration: next(t for n in noun for t in tables if n in t) (and is the same as the any function above)
|
0

By your comment I assume the elements in the list aren't strings, I don't know any way of getting the var name on python, so this is my solution

    tables = ["projname:dataset.devices","projname:dataset.attempts"]

noun = {"devices": devices, "today": today}

for n in noun:
    for table in tables:
        if n in table:
            break

Comments

0

How about this simple solution:

>>> tables = ["projname:dataset.devices","projname:dataset.attempts"]
>>> noun = ['devices','today']
>>> for x in noun:
        if any(x in s for s in tables):
            print('Found !')
            break


Found !

5 Comments

This does not print Found for any of the cases.
@user3447653, is noun a list of string or something else?
I just checked the type of elements in noun and tables. The result is <type 'str'>
So, how does this not work?, I tried it myself, and the output is from IDLE !
@IronFist looks like the OP want the element from tables that match a element from noun
0

Note that in your code, when you say noun in tables it means is a list (noun) INSIDE of another list (tables). Thats not what we want to find out, rather you want to find each noun that exists inside of one of the elements of tables. Try the following:

tables = ["projname:dataset.devices","projname:dataset.attempts"]

nouns = ["devices","today"]

for noun in nouns:
    for table in tables:
        if noun in table:
            print "Found", noun
            break

7 Comments

He wants a break after it finds something.
Thanks. Here "devices" in nouns matches "projname:dataset.devices" in tables. So the output should be "projname:dataset.devices". But "in" keyword does not do it. I tried with in already, which I have mentioned in my question.
The in keyword does do it, you are just using it incorrectly.
I tried with the above code, it returns empty result.
Consider to following issue: noun = [devices,today] doesnt that create a list of two elements, first being the devices variable and the second being the today variable. What value do these variables have? Are you mistakenly trying to write noun = ['devices', 'today']
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.