简体   繁体   中英

Python- Lists of lists

I have to create a code of lists of lists where it returns "True" exactly when there exists(at least 1) and favorite band in all of the lists(may be > 3). I'm very new to programming and I am very lost on what to do. I have tried something, but it's not completely filled in. I need some help!

favoriteBandLists = [
    ["Metallica","Linkin Park","Alice In Chains","Nirvana", "Soundgarden"],
    ["Pink Floyd","Alice In Chains","Soundgarden","Metallica","Linkin Park"],
    ["Audioslave","Offspring","Soundgarden","Linkin Park","The Beatles"]]

My Code:

def commonFavoriteBand(list):
    foundCounterExampleyet = False
    for i in range(()):
        if(()):
            foundCounterExampleYet = True
    return not(foundCounterExampleYet)

print (commonFavoriteBand())

def commonFavoriteBandA():
    foundExampleYet = False
    for value in values:
        if():
            foundExampleYet = True

return foundExampleYet

I'd say the simplest but most comprehensible way is this:

favorite_band_lists = [
    ["Metallica", "Linkin Park", "Alice In Chains", "Nirvana", "Soundgarden"],
    ["Pink Floyd", "Alice In Chains", "Soundgarden", "Metallica", "Linkin Park"],
    ["Audioslave", "Offspring", "Soundgarden", "Linkin Park", "The Beatles"],
]

def common_favorite_band(band_lists):
    # If there are no bands at all, you can't really say there are any in
    # common
    if not band_lists:
        return False

    # Convert the first band list to a "set" -- a group of unique items
    common_bands = set(band_lists[0])

    # Then, for every other band list...
    for bands in band_lists[1:]:
        # ...intersect it with the running set.  This means `common_bands`
        # should throw away anything that isn't also in `bands`.
        common_bands.intersection_update(bands)

    # Now common_bands contains only the bands that are in every list.
    # But you wanted True or False, so cast it to a bool -- an empty set
    # will become False, a set with at least one item will become True.
    return bool(common_bands)

print(common_favorite_band(favorite_band_lists))  # True!

(btw, function and variable names are traditionally written in snake_case in Python, not camelCase)

You can use this:

s=set(favorite_band_lists[0])
for ii in favorite_band_lists[1:]:
    s=s.intersection(s,ii)
    if len(s) == 0:
        break

This will stop as soon as an intersection is empty, which may be desirable. It also returns the bands that are common across lists, if any. To produce True or False , check if the length of s -- the intersection list -- is > 0.

print len(s) > 0

Here's a different solution to the others:

def common_favorite_band(band_lists):
    for band in band_lists[0]:
        count = 0
        for band_list in band_lists[1:]:
            if band  not in band_list:
                break
            else:
                count += 1
        if count == len(band_lists) - 1:
            return True
    return False

It looks at all the bands in the first list and checks if that band is in each other list. We add 1 to count every time it is. For any band, if count reaches the length of band_lists - 1, then we may return True and finish. If at any time a band does not appear in another list, we break from that iteration of the loop and try another band. If we have checked every band and we have not got a count equal to the length of band_list - 1, then we return False.

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