0

The input of the array of sets:

[{"Bob","87"}, {"Mike", "35"},{"Bob", "52"}, {"Jason","35"}, {"Mike", "55"}, {"Jessica", "99"}]

However, if I immediately print it, the terminal shows me

[{'Bob', '87'}, {'35', 'Mike'}, {'Bob', '52'}, {'35', 'Jason'}, {'55', 'Mike'}, {'99', 'Jessica'}]

We can see some names are swapped with their corresponding numbers. Here are two problems:

Q1. How can we keep the list the same as the input?

Q2. If I run a for-loop for each element of the list, how can I quickly extract the numeric elements of each set?

Charles Yan
  • 83
  • 1
  • 1
  • 9
  • 1
    "How can we keep the list the same as the input?" This is nonsensical; `{"Mike", "35"}` and `{'35', 'Mike'}` **mean the same thing**. See the linked duplicate; `set`s inherently **don't have an order**. "Q2." - stop right there; please read [ask] and note well that we require **one** question per post. – Karl Knechtel Jan 06 '23 at 05:34
  • 2
    Q1: sets in Python are unordered, so you are out of luck on this one. Q2: you can iterate over the contents of each set using `isnumeric()` on the strings to spot the numeric elements. Of course, if you have control over the input you could use a different data structure like a tuple instead of a set. – rhurwitz Jan 06 '23 at 05:36

0 Answers0