Filter elements from a tuple based on a value in a given index

Asked

Viewed 679 times

-1

I want to write a Python function, called separates, that takes a Tuplo whose elements are pairs (double 2 elements), where the 1st element is a name (a string) and the 2nd element is an age (an integer), as described in the previous exercise.

The function shall return 1 tuple with two elements:

  • 1 Double with the elements of the original Double in which the age is less than 18.
  • 1 Double section with the elements of the original Double section where the age is 18 or over.
  • Your function should check that the received argument is correct (use exercise 1 function to do this check). If the argument is not correct an error must be generated, with the raise statement Valueerror('separates: incorrect argument. ').

Example:

>>> tup = (('Maria', 38), ('Miguel', 17), ('Tiago', 18), ('Sara', 19))
>>> menores, maiores = separa(tup)
>>> menores
(('Miguel', 17),)
>>> maiores
(('Maria', 38), ('Tiago', 18), ('Sara', 19))

>>> separa(())
((), ())

>>> separa((('Maria', 38), ('Miguel', 17), ('Tiago', 18), ('Sara', 19.7)))
              ….
builtins.ValueError: separa: argumento incorrecto.
  • 1

    What did you try? Apart from copying the question statement, you had some difficulty?

  • And now, with you, Celso Overflow from "Pass Glue and Answer Soon".

  • I’m not realizing how I can do it.

  • 1

    Sousa, please, Sopt works best when the questions are more general and apply broadly to the site’s audience. In other words, "how to solve an exercise" is not a good question. Neither is copying the statement. There needs to be more engagement on your part in explaining, formatting, and demonstrating the problem. tried do. Summarize your doubts, do questions. Read that and this to try to fit your question.

  • This is what I did: def verifies(variable): if isinstance(variable, tuple): if isinstance(variable[0], str) and isinstance(variable[1], int): Return True Else: Return False Else: Return False Turn

  • @Please click on [Edit] and add the code to the question. Thus it is more organized (other users do not need to "hunt" information in the comments, all relevant information is in the question), and in addition it is more readable, because it is possible to format the code (see here tips on how to format). This is even more important in Python, since indentation is part of the syntax, and in the comments it is not clear how indentation is

Show 2 more comments

1 answer

4

If you have a tuple in the shape

valores = (('Maria', 38), ('Miguel', 17), ('Tiago', 18), ('Sara', 19))

You can list items that have the second value less than 18 as follows:

menores = tuple(it for it in valores if it[1] < 18)

And equal to or greater than 18 in a similar manner:

maiores = tuple(it for it in valores if it[1] >= 18)

Thus, considering the function verifica, which was defined in the previous exercise - that is, let’s consider that you have already done the previous exercise and have this function working (its implementation is not part of this question), you can write a function in the form:

def separa(tuplo):
    if not verifica(tuplo):
        raise ValueError(’separa: argumento incorrecto.’)

    menores = tuple(it for it in tuplo if it[1] < 18)
    maiores = tuple(it for it in tuplo if it[1] >= 18)

    return (menores, maiores)
  • 1

    Thank you so much Anderson, you helped so much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.