[Python] How do I separate the int and str and store them in their respective tuple?

Discussão em 'Python' iniciado por Stack, Julho 6, 2025.

  1. Stack

    Stack Membro Participativo

    How do I separate and store the items of a tuple within a tuple to their respective (new) tuple?

    The code below is what I'm working.

    The output I want is:

    unique_num = (45, 21, 10, 24, 2) unique_ words = ('foot', 'basket', 'hand', 'foot', 'hand')**

    def unique_data_items(data):
    unique_num = ()
    unique_words = ()
    for items in data:
    for values in items:
    if values == int:
    unique_num= unique_num + values
    elif values == str :
    unique_words= unique_words + values

    return ((unique_words), (unique_num))


    data = ((45, 'foot'), (21, 'basket'), (10, 'hand'), (24, 'foot'), (21, 'hand'))
    print(unique_data_items(data))


    I've tried to approach this in a different way, and it worked, but I want to know why this way isn't working.

    Continue reading...

Compartilhe esta Página