Ashwin Sarith
Ashwin Sarith

Reputation: 79

Is there a way to prevent duplicates from coming about, when adding two lists together from a tuple set?

so I have a question regarding list checking. I am setting up a hiring system, in which I have individuals, their rate, and the skills they have. I am trying to get a function created which allows me to key in two individuals, and basically, the output should give the values, without duplicates. Take note the individuals themselves are tuples. With skills and cost.

I have tried individually hard coding skills, but apprently that would defeat the purpose if some new skill is implemented.

I have also tried looping through but i am still unable to get the values to read from the second individual.

jess = (["php", "java"], 200)

clark = (["php", "c++", "go"], 1000)

john = (["lua"], 500)

cindy = (["php", "go", "word"], 240)

candidates = [jess, clark, john, cindy]

project = ["php", "java", "c++", "lua", "go"]

def skills(candidates):                   ## This command is supposed to find the possible skills of all the individuals.
    skillset = []                         ## empty list to contain the skills that are going to be appended
    for x in (candidates):                ## so basically the x represents the tuple set
        skillset.append(x[0])             ## the tuple selection represents the skills of the given candidates
        if x[0] not in skillset:          ## if the selected component of the tuple is not in the skill list, then the component should be removed
            skillset.append(x[0])
        else:
            return skillset


    return skillset



(print(skills([cindy, clark])))

Upvotes: 0

Views: 89

Answers (4)

Lante Dellarovere
Lante Dellarovere

Reputation: 1858

Your code has some obscure point.
For example, you append x[0] to skillset and then you check if x[0] is in skillset, which is always True.


All other answers solve your problem, here a one-liner variant:

def skills(candidates): return list({x for c in candidates for x in c[0]})

Upvotes: 0

Bjarnemus
Bjarnemus

Reputation: 16

I understand that your goal is to combine two candidates skills.

So when Clark can do PHP, C+ and Go, and Cindy can do PHP, Go and Word.

The combination should then be PHP, C+, Go and Word.

This skills function should do that:

def skills(candidates):
    skillset = []  
    for x in (candidates):       
        for skill in x[0]:
            if skill not in skillset:
                skillset.append(skill)
    return skillset

Instead of just checking if a set of skills is not already in the list, it goes into each skillset and adds them to a new list, if the individual skill is not already there.

Upvotes: 0

gregk
gregk

Reputation: 199

You could use sets, they prevent duplicates.

def skills(candidates):
    skillset = set()
    for x in candidates:
        skillset.update(x[0])
    return list(skillset)

Upvotes: 1

quamrana
quamrana

Reputation: 39404

It sounds like you are trying to see if some candidates bring enough skills to run a project.

This code will help you on your way:

# candidates and project the same as posted

def skills(candidates):                   ## This command is supposed to find the possible skills of all the individuals.
    skillset = set()                      ## empty set to contain the skills that are going to be appended
    for x in candidates:                  ## iterate over candidates
        for skill in x[0]:                ## iterate of each candidates skills
            skillset.add(skill)

    return skillset

availableSkills = skills([jess, clark, john])
requirement = set(project)
print(availableSkills)
print(requirement)
print(availableSkills >= requirement)

Output:

{'go', 'c++', 'lua', 'java', 'php'}
{'go', 'c++', 'lua', 'java', 'php'}
True

You have found that jess, clark and john have the skills for the project.

Upvotes: 0

Related Questions