0

I have 2 lists. One of the is includes string and the other one includes integers. I would like to add two of them to a set, print them in order. But when I try to do so, the set prints randomly in the console. How can I solve this problem? Here are my arrays,

['#', '*']    
[2, 3, 5, 6, 8]
['o', '€']

Expected result :

 {'#', '*', 2, 3, 5, 6, 8, 'o', '€'}

But in reality, elements of the set always changes. It never ordered like set updating order. Also, I am using set_x.update(arr_x) method to do that.

NOTE: I want to solve this problem without external libs and functions.

phzdjz
  • 79
  • 7
  • 2
    Does this answer your question? [Does Python have an ordered set?](https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) – Marat Nov 09 '21 at 16:30
  • No it doesn't seem like my answer – phzdjz Nov 09 '21 at 16:32
  • What have you tried? Post your attempt here. – njari Nov 09 '21 at 16:36
  • @omeren why does an `OrderedSet` not seem like the answer? How did you try using an `OrderedSet` and why did that not work? – Random Davis Nov 09 '21 at 16:37
  • I have two sets ` s1 = {2,3,5,'*',2,6,'#',3} s2 = {2,'o',3,5,6,8,'€'}` and i would like to convert them `{'#', '*', 2, 3, 5, 6}, {'#', '*', 2, 3, 5, 6, 8, 'o', '€'}, {2, 3, 5, 6}`. I tried that converting them array and i divide the data type 2, integers and strings. I get the integers and strings array. – phzdjz Nov 09 '21 at 16:39
  • @RandomDavis it is not permitted to use external functions or libs. It has to work with pure code. – phzdjz Nov 09 '21 at 16:44
  • @phzdjz this is the first time you've mentioned that; if you have important info like that, it must go into your question. You shouldn't have to follow up in comments with any additional details or restrictions like that. – Random Davis Nov 09 '21 at 16:53
  • @RandomDavis okay thank you, i noted that. – phzdjz Nov 09 '21 at 16:55

1 Answers1

0

When I do this:

def add_set(a, b, c):
    return set(a + b + c)
add_set(a, b, c)

I do have them in the order you requested

Reine Baudache
  • 413
  • 4
  • 16
  • 2
    Are you sure that gives the expected result? It certainly doesn't in 3.9.7 –  Nov 09 '21 at 16:39
  • I've tried that like `set1=set(arr_1_str+arr_1_int) print(set1)` . Here are the results `{2, 3, 5, 6, '*', '#'}` `{2, 3, '*', 5, 6, '#'}`. So order is always changing. – phzdjz Nov 09 '21 at 16:41
  • It's weird, I do have them in the proper order in 3.9.5 – Reine Baudache Nov 10 '21 at 08:09