#๐Ÿ”’ how to find the common objects, in a varying amount of lists

122 messages ยท Page 1 of 1 (latest)

nocturne pollen
#

will provide screenshots to help

peak hullBOT
#

@nocturne pollen

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

nocturne pollen
#

basically given something like this

#

'''([(0, ['FoC', 'Calc 1', 'Logic']), (108, ['FoA', 'Calc 2', 'Logic']), (148, ['FoC', 'Calc 1', 'Logic']), (248, ['FoC', 'Calc 1', 'Logic']), (0, ['Calc 2', 'History', 'Politics']), (108, ['FoC', 'Calc 1', 'Logic'])], (0, 1, 3))'''

#

i dont know how to make it apear in python font

burnt shoal
#

!code

peak hullBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

nocturne pollen
#
'([(0, ['FoC', 'Calc 1', 'Logic']), (108, ['FoA', 'Calc 2', 'Logic']), (148, ['FoC', 'Calc 1', 'Logic']), (248, ['FoC', 'Calc 1', 'Logic']), (0, ['Calc 2', 'History', 'Politics']), (108, ['FoC', 'Calc 1', 'Logic'])], (0, 1, 3))'
#

given something like that and i want a list of the classes in common

burnt shoal
#

have you used set before?

nocturne pollen
#

yeah hold up thats not the best explenation

#

basically i will choose to access different indexes depending on something else in the code

open onyx
#

what's the (0, 1, 3) at the end? seems dif from others

burnt shoal
#

yeah, if the len of the objects in the list vary, this is going to be tricky

#

where did this data come from?

nocturne pollen
#

but the group changes

open onyx
nocturne pollen
#

ill access all subjects but it might be different indexes

nocturne pollen
#
from hidden import zbini_attrs

def valid_study_group(zbinis, group):
    if len(group) not in (3, 4):
        status = False
        
    attributes = []
    
    for i in group:
        attributes.append(zbini_attrs(zbinis[i][0]))
            
    for j in range(len(attributes)):
        set_tester = set()
        for k in range(4):
            set_tester.add(attributes[j][k])
            
        if len(set_tester) not in (1, 4):
            status = False
      
open onyx
#

well ok
each tuple looks like (num, [classes])
so extract the lists of classes, make em into sets, take the intersection

burnt shoal
#

So it sounds like the issue is more "accessing the correct lists" rather than finding the intersection?

nocturne pollen
#

i have this which deals with other stuff but i need a way to check the number of subjects that overlap betweeb the zbinis specified

nocturne pollen
#

because it changes depending on the code

burnt shoal
#

then you just iterate through your list of lists

open onyx
nocturne pollen
#

how do i go from a list of sets to a iterable containing all the in common items in the set

#

like i get the .intersection method but dont actually know how to apply it in this

burnt shoal
#
classes = [
    ['FoC', 'Calc 1', 'Logic'],
    ['FoA', 'Calc 2', 'Logic'],
    ['FoC', 'Calc 1', 'Logic'],
    ['FoC', 'Calc 1', 'Logic'],
    ['Calc 2', 'History', 'Politics'],
    ['FoC', 'Calc 1', 'Logic']
    ]
#

lets say you have this

nocturne pollen
#

yeah

burnt shoal
#

it actually looks like there's no value in every single list

#

so you're going to end up with an empty set

open onyx
burnt shoal
#

||reduce||

open onyx
nocturne pollen
#

if i show the task itll prob make this easier

burnt shoal
nocturne pollen
#

Task 2: Checking Group Validity (3 marks)
While Zoomerbinis are usually quite amicable with one another, they have a rather specific set of preferences when it comes to forming study groups! Specifically, a study group is valid if for each of the four attributes:

All members have the same attribute value, or;
All members have unique attribute values, i.e. no two members share the same attribute value.
Additionally,

A valid group can only have either 3 or 4 members, and;
Each member must study at least one subject in common with every other member of the group.
Write a function valid_study_group(zbinis, group) that computes the validity of a given study group of Zoomerbinis, as per the above definition.

The parameter zbinis will be a list of Zoomerbinis, each represented as a (type_id, subjects) tuple, where type_id is a Zoomerbini type ID (0 to 255 inclusive) and subjects is a list of non-empty strings denoting the subjects the respective Zoomerbini is studying.

The parameter group will be a tuple of indices into the zbinis list and reflects the group of Zoomerbinis being checked for validity (zbinis may be of a different length to group). Note that while Zoomerbinis in a group may share the same type IDs, they must be distinct individuals in zbinis. In other words, if an index appears more than once in group, the group is invalid.

Your function should return a tuple containing two elements:

A boolean value denoting the validity of the respective group (True if the group is valid or False if not).
A positive integer representing the number of subjects all members of the group share in common with one another, or otherwise None if the group is not valid.
A working version of the zbini_attrs(type_id) function from Task 1 has been provided to help you with this task.

Example Calls:

open onyx
nocturne pollen
#

i think i have everything fine i just need to make an iterable with all the common items in it

burnt shoal
#

for set intersections and looping, you can make use of &=

#

make a set from the first item
loop through remaining items
&= each item to the original set

#
overlap = set(classes[0])
for c in classes[1:]:
    overlap &= set(c)
open onyx
nocturne pollen
#

why start at 1:

burnt shoal
nocturne pollen
#

oh yeah

#

sorry

#

still new to this

burnt shoal
#

all good, no need to apologize

nocturne pollen
#

alrigthit ill try that

#

i think it will work

burnt shoal
#

I mentioned reduce above, which is a function that does basically this

nocturne pollen
#

is there an easy way to add all the elements in a list to a list without iterating over it

#

if i use append i think ill just have a list of lists

burnt shoal
#

extend

nocturne pollen
#

thanks

#

giving an unboundLocalError

#

havent recieved that before

open onyx
nocturne pollen
#
from hidden import zbini_attrs

def valid_study_group(zbinis, group):
    if len(group) not in (3, 4):
        status = False
        
    attributes = []
    classes = []
    for i in group:
        attributes.append(zbini_attrs(zbinis[i][0]))
        classes.append(zbinis[i][1])
            
    for j in range(len(attributes)):
        set_tester = set()
        for k in range(4):
            set_tester.add(attributes[j][k])
            
        if len(set_tester) not in (1, 4):
            status = False
    overlap = set(classes[0])
    for c in classes[1:]:
        overlap &= set(c)
        
    return status, overlap
#

oh wait

#

nvm

#

thats for status

#

code was not complete

#

this is solved

#

thank you very much

cosmic laurel
reef charm
#
a = set(1, 2, 3, 4)
b = set(3, 4, 5, 6)
print(a & b)
nocturne pollen
#
from hidden import zbini_attrs

def valid_study_group(zbinis, group):
    status = True
    if len(group) not in (3, 4):
        status = False
        
    attributes = []
    classes = []
    for i in group:
        attributes.append(zbini_attrs(zbinis[i][0]))
        classes.append(zbinis[i][1])
            
    for j in range(len(attributes)):
        set_tester = set()
        for k in range(4):
            set_tester.add(attributes[j][k])
            
        if len(set_tester) not in (1, 4):
            status = False
            
    overlap = set(classes[0])
    for c in classes[1:]:
        overlap &= set(c)
        
    if len(overlap) == 0:
        return False, None
    
    return status, len(overlap)
#

its returning True, 1 when group is 0, 1, 5

cosmic laurel
#

what's the rationale behind setting the overlap length to None instead of 0? sounds to me like making the return type more complicated than it needs to be

burnt shoal
#

you should also be returning immediately once you realize the groups is not len 3 or 4

reef charm
#

wait

#

i think i got it

burnt shoal
#

why go through all the calculations if the groups aren't a valid size?

reef charm
#

i should use a reduce function

#

with the set built in intersect function

burnt shoal
nocturne pollen
burnt shoal
reef charm
nocturne pollen
#

obv

burnt shoal
nocturne pollen
#

i like this, dont think the uni will appreciate me using something we havent covered

nocturne pollen
#

oh wait

reef charm
#
def intersect(*args: list) -> list:
    res = [i for i in args]
    from functools import reduce
    res = list(map(lambda a: set(a), res))
    res = reduce(lambda a, b: a & b, res)
    return list(res)

I GOT IT

nocturne pollen
#

i misunderstood

reef charm
#

I FUCKING GOT IT

#

YEAAAAAAAAAAAAAAAAAA

burnt shoal
reef charm
#

ok

nocturne pollen
#

lmao

#

it wants None whenever status is going to return as false

burnt shoal
reef charm
#

well i did it yay

nocturne pollen
#

so pretty much return false, none after every check

reef charm
#

:(((((

burnt shoal
#

yeah exactly

nocturne pollen
#

okay i have done that

#

problem is somewhere else in the code

#

im missing a check

burnt shoal
#

does it want a tuple returned?

#

or simply None if not valid?

nocturne pollen
#

still wants a tuple

#

(False, None)

#

i know where the problem is but not what it is

#

lmao i just didnt read the task sheet

#

now i just have to find a nice way to put that in

#

thanks everyone

#

!close

peak hullBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.