#๐ how to find the common objects, in a varying amount of lists
122 messages ยท Page 1 of 1 (latest)
@nocturne pollen
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.
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
!code
'([(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
have you used set before?
yeah hold up thats not the best explenation
basically i will choose to access different indexes depending on something else in the code
what's the (0, 1, 3) at the end? seems dif from others
yeah, if the len of the objects in the list vary, this is going to be tricky
where did this data come from?
the group, it specifies which ones ill access in this example
but the group changes
I assume that's by index?
ill access all subjects but it might be different indexes
yeah
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
well ok
each tuple looks like (num, [classes])
so extract the lists of classes, make em into sets, take the intersection
So it sounds like the issue is more "accessing the correct lists" rather than finding the intersection?
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
but i dont know how many sets to make
because it changes depending on the code
that's why you need a clean way to access each list
then you just iterate through your list of lists
you don't have to
make a list (of sets) you append to
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
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
yeah
it actually looks like there's no value in every single list
so you're going to end up with an empty set
How do you calculate the product of a list of numbers?without the function
||reduce||
Loops work too
if i show the task itll prob make this easier
haha yeah for sure
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:
I'm just on phone and do not want to code
i think i have everything fine i just need to make an iterable with all the common items in it
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)
Same idea can be applied to your problem
classes is just a list of all the classes?
why start at 1:
.
because we've made a set from [0] already, so there's no point in checking it twice
all good, no need to apologize
I mentioned reduce above, which is a function that does basically this
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
extend
I think that might mean you're trying to use a variable before it's defined in a function
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
yes, it just means a local variable wasn't defined yet
a = set(1, 2, 3, 4)
b = set(3, 4, 5, 6)
print(a & b)
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
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
you should also be returning immediately once you realize the groups is not len 3 or 4
why go through all the calculations if the groups aren't a valid size?
that's already what the loop with &= is doing
how do i return the len of overlap then?
it entirely depends on your requirements
i can do it with less code
obv
sure, but that's not solving any current issues
i like this, dont think the uni will appreciate me using something we havent covered
its just the task requirement, it wants a tuple containing False, None
oh wait
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
i misunderstood
Please don't derail the help channel
ok
That also looks like way more code than what's currently there
well i did it yay
so pretty much return false, none after every check
:(((((
yeah exactly
okay i have done that
problem is somewhere else in the code
im missing a check
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
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.