How to turn a set into a Python 3.x list

Asked

Viewed 187 times

-1

I’m doing an exercise that asks to, given two sets, print in ascending order (line by line) the symmetric difference between them, my code:

n = set(input().split())
m = set(input().split())

m_diff = m.difference(n)
n_diff = n.difference(m)
result = [m_diff, n_diff]

if __name__ == '__main__':
    for i in range(0, len(result)):
        print(result[i])

input:

2 4 5 9
2 4 11 12

my output:

{'11', '12'}
{'9', '5'}

expected output:

5
9
11
12

How do I get the elements out of the sets and make them all become elements of just one list?

  • It didn’t happen because you put it like this result = [m_diff, n_diff]? Because there was not even the question of turning into a set, it was simply that took the "diffs" and placed one at index 0 and another at index 1, all knowing that the .difference returns all results that differ in a new set, well, if you want to put it all together the right way .union (I guess I’m not sure) or the .update or the "pipe" (set1|set2)

  • the problem that Difference() returns a set, I put on a list to give . Sort() and print item by item in ascending order, equal appears in expected output

  • I said at the beginning, the problem doesn’t look like the rest of the code or Difference, it looks like result = [m_diff, n_diff], I’m gonna use a [...,...] when maybe you should have used Union or update, depending on what you want

  • You were right, with the update returns the full set {'4', '12', '9', '11'}, now how do I print each element of the set?

  • Take the set that was updated, after all this is different from . update() pro . Union(), right? I don’t remember exactly. If you used the update one of your sets has been updated, then one of them has all values

  • yes, update() has turned everything into a single set, with all the elements that interested me, but you cannot iterate and print as a list, nor use Sort(). I still have to know how to return the expected output

  • 1

    Is not iterating the set? I guess you just use for i in m_diff or for i in n_diff (depending on which received the update)

  • 1

    Iterating using the index is not the most idiomatic way to iterate on Python. The way proposed by @Guilhermenascimento, however, with for var in iterable is the most idiomatic and allows iterating in several ways

  • Thanks for the help, your method didn’t need to be made into a list, I didn’t know you could iterate there. Anyway, I will answer the question by converting the set into a list first.

Show 4 more comments

2 answers

2


You can use the class method set of diferença simétrica:

result = m.symmetric_difference(n)

Or the equivalent operator ^:

result = m ^ n 

For example:

a = '2 4 5 9'
b = '2 4 11 12'

n = set(int(i) for i in a.split())
m = set(int(i) for i in b.split())

result = m ^ n

for i in sorted(result):
    print(i)

Exit:

5
9
11
12

See working on Repl.it

  • the output has to be in ascending order: for i in sorted(list(result), key=int):
print(i)

  • @Pirategull: Made!

0

n2 = set(input().split())
m2 = set(input().split())

m_diff = m2.difference(n2)
n_diff = n2.difference(m2)

# poderia resolver o exercício iterando diretamente no set e usando print()
m_diff.update(n_diff)


# esse método converte set em list:
def convert(set):
    return list(set)

#set convertido para lista
m_lis = convert(m_diff) 


if __name__ == '__main__':
    for elements in m_lis:
        print(elements)

Browser other questions tagged

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