1

I try to update tuple values using another tuple.

grade = (('a', 10), ('b', 20), ('c', 30), ('d', 40))

factors = (('a', 1), ('b', 2))

Expected result:

result  = (('a', 11), ('b', 22), ('c', 30), ('d', 40))

What would be the right way to do this? , I tried the following code but it did not work. I would be happy to help

print(list(filter(lambda list_a: list(map(lambda x, y: x+ y, list_a[0], grade[1])) not in list(map(lambda x: x[0], factors)), grade)))
5
  • Why b is 12 and why not 22 ? Commented Dec 9, 2020 at 18:47
  • Time to learn list comprehensions. Commented Dec 9, 2020 at 18:48
  • And dictionaries. Commented Dec 9, 2020 at 18:49
  • @toRex You're right my mistake, I updated it Commented Dec 9, 2020 at 18:49
  • 1
    To improve your question please describe shortly what the update is expected to do. We may guess it by the result but it would help to have a clear specification. Commented Dec 9, 2020 at 18:57

5 Answers 5

1

I'd use dictionaries, seem more suited for your task:

factors = dict(factors)
grade = dict(grade)
{k: grade[k] + factors[k] if k in factors.keys() else grade[k] for k  in grade.keys()}

Output:

{'a': 11, 'b': 22, 'c': 30, 'd': 40}
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any solution to do that without using dict?
1

You should use a dict, and you can take advantage of its get method to get 0 for non existing keys:

grade = (('a', 10), ('b', 20), ('c', 30), ('d', 40))

factors = (('a', 1), ('b', 2))
factors = dict(factors)

new_grades = [(g[0], g[1] + factors.get(g[0], 0)) for g in grade]
print(new_grades)
# [('a', 11), ('b', 22), ('c', 30), ('d', 40)]

3 Comments

Is there any solution to do that without using dict?
This could be possible, but you have to get the value corresponding to a letter in factors. If you don't build a dict, you'll need a function that iterates on your tuples each time until it finds the one that contains the right letter. This is very inefficient. On the other side, once you have built the dict once, accessing the value associated with a letter is done directly without any iteration. That's much more efficient, and doesn't require you to write a function that will not be as good. So, do the right thing and enjoy!
And, as a side note, list comprehensions are usually faster than using map when you just want to build the list.
0
fact = dict(factors)
result = list(
    map(
        lambda x: (x[0], x[1] + fact.get(x[0], 0)), grade
    )
)

1 Comment

Is there any solution to do that without using dict?
0

Make factors a dict:

factors = dict(factors)

then use a dict comprehension to build a new dict using the existing association list grades:

result = {g: score + factors.get(g, 0) for g, score in grade}

If you really need a tuple of tuples, use

result = tuple((g, score + factors.get(g,0)) for g, score in grade)

1 Comment

Tried your code but it brings: {'a': 1, 'b': 2, 'c': 0, 'd': 0}
0

How about using Counters instead?

from collections import Counter

grade = Counter(dict(grade))
factors = Counter(dict(factors))

The the update is simply

grade += factors

or

grade.update(factors)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.