0

Summary: I have a list

list = [
        {
            'id': '1',
            'elements': 'A',
            'table': 'maps/partials/a.html',
            'chart': 'maps/partials/charts/a.html',
        },
        {
            'id': '2',
            'elements': 'B',
            'table': 'maps/partials/census/b.html',
            'chart': 'maps/partials/charts/b.html',
        },
        {
            'id': '3',
            'elements': ('C','D','E','F'), //i believe it is wrong 
            'table': 'maps/partials/census/common.html',
            'chart': 'maps/partials/charts/common.html',
        },]
some_arr = ['E','2011','English','Total']

I want to compare elements with items in some_arr. and i am doing this to get list and compare it with some_arr.

for data in list:
            for i in range(len(some_arr)):
                if data['elements'] == some_arr[i]:
                    print(data['id'])

As you can see in 'id':3 section_title has 4 values. How do i compare elements here with some_arr.

3
  • What is some_arr example?? Commented Jul 27, 2015 at 18:53
  • 1
    I am getting those values from form action(jquery). And i am collecting those values in some_arr(views.py). Commented Jul 27, 2015 at 18:56
  • do you want to see if any values are in some_arr? Commented Jul 27, 2015 at 18:57

2 Answers 2

4

If you want to find any matches:

lst = [
        {
            'id': '1',
            'elements': 'A',
            'table': 'maps/partials/a.html',
            'chart': 'maps/partials/charts/a.html',
        },
        {
            'id': '2',
            'elements': 'B',
            'table': 'maps/partials/census/b.html',
            'chart': 'maps/partials/charts/b.html',
        },
        {
            'id': '3',
            'elements': ('C','D','E','F'),
            'table': 'maps/partials/census/common.html',
            'chart': 'maps/partials/charts/common.html',
        }]
some_arr = ['E','2011','English','Total']
st = set(some_arr)

from collections import Iterable
for d in lst:
    val = d["elements"]
    if isinstance(val, Iterable) and not isinstance(val, str):
        if any(ele in st for ele in val):
            print(d["id"])
    else:
        if val in st:
            print(d["id"])

Making a set of all the elements in some_arr will give you O(1) lookups, using isinstance(val, Iterable) and not isinstance(val, str) will catch any iterable value like a list, tuple etc.. and avoid iterating over a string which could give you false positives as "F" is in "Foo". any will short circuit on the first match so if you actually want to print id for every match then use a for loop. Lastly if you are using python2 use basestring instead of str.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. It works like a charm with little changes.
No worries, do you actually want to see if there is any match or do you want to check all elements regardless?
No thank you I just want to see any match regardless. btw i am using python3.
2

You can use set.intersection to get the intersection between them but before you need to check that if it's a tuple then use intersection.

Also in the first case you can use in to check the membership to check if data['elememnt'] in in some_arr then print data['id']:

for data in list:
        d=data['elements']
        if isinstance(d,tuple):
           if set(d).intersection(some_arr):
                print(data['id'])
        if d in some_arr:
              print(data['id'])

Comments

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.