简体   繁体   中英

comparing a list to another nested list(unorded) and output a list

Here is my lists:

x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']

For example, take y[0] and compare it to x[0][0]~x[2][2] and then print the list(nested list) in x that has the element in y.

This function should compare all the elements in y to every element in x

l have thought about this for 2 days, and l cant figure it out. Please help!

As I understand it, you'd like to make a list of the books in x whose publication date is in y . This ought to do it:

>>> [b for b in x if b[1] in y]

[['What if?', '2014', 'Randall Munroe'],
 ['Thing Explainer', '2015', 'Randall Munroe'],
 ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]

y probably ought to be a set here. The performance gains will be negligible, since y is so small, but it being a set conveys how you intend to use it:

years = {'2014', '2015', '2014'}

Lastly, you might want to use a namedtuple from collections to represent your books. Something like:

from collections import namedtuple
Book = namedtuple('Book', 'name year author')
books = [Book(b) for b in x]

Then the above list comprehension becomes:

[b for b in books if b.year in years]

which is nice and readable.

You can filter what you want by using built-in filter method:

>>> filter(lambda s: s[1] in y, x)
[['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]

What it does:

It iterates through every list from your x list and check if the second element of each sub-list is found in y[1] , by using the lambda function

Edit:

The above code will work if you are certain that the dates in each sub list of x maintain the same index, that is s[1] ,

But in case, you can not guarantee that, then I prefer the next code (I've added other element to x , with different date indexes:

>>> z = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge'],['2015','Thing Explainer',  'Randall Munroe'], ['Alan Turing: The Enigma', 'Andrew Hodge','2014']]
>>> 
>>> 
>>> filter(lambda s: set(s).intersection(y), z)
[['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge'], ['2015', 'Thing Explainer', 'Randall Munroe'], ['Alan Turing: The Enigma', 'Andrew Hodge', '2014']]

Using list comprehension:

list(i for i in x if y[0] in i)

>>> [['What if?', '2014', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]

see the clear loop method

output = []
for yy in y:
    for xlist in x:
        for xx in xlist:
            if yy == xx:
                output.append( xlist )
                break
print output

Easy nested loop problem. Simply iterate through all nested lists in x and if the second value is in y print it.

for nested in x:
    if nested[1] in y:
        print(nested)

Or append to a results list and print after the comparisons, depending on what you want.

Run through y, checking against x using 'in'

x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']
for a in y:
    print("""{}:""".format(a))
    for b in x:
        if a in b:
            print(b)

produces:

2014:
['What if?', '2014', 'Randall Munroe']
['Alan Turing: The Enigma', '2014', 'Andrew Hodge']
2015:
['Thing Explainer', '2015', 'Randall Munroe']
2014:
['What if?', '2014', 'Randall Munroe']
['Alan Turing: The Enigma', '2014', 'Andrew Hodge']

Check this code: you can run it as a module. it returns a list of coincidences with each item being a list of the index in y and the indexes in x

def mifunc(x, y):
    coincidence = []
    for alpha in range(len(x)):
        for beta in range(len(x[alpha])):
            for gamma in range(len(y)):
                if y[gamma] == x[alpha][beta]:
                    coincidence.append([gamma, [alpha, beta]])
    return coincidence


x = [['Godel Escher Bach', '1979', 'Douglas Hofstadter'], ['What if?', '2014', 'Randall Munroe'], ['Thing Explainer', '2015', 'Randall Munroe'], ['Alan Turing: The Enigma', '2014', 'Andrew Hodge']]
y = ['2014', '2015', '2014']

if __name__ == '__main__':
    coincidence = mifunc (x, y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM