-2

I'm trying to sort a dictionary that contains binary strings but it doesn't seem to sort properly. The code I used is below:

dict1 = {"5": "101", "1": "001", "17": "10001", "3" : "11"}
print("Unsorted dict:",dict1)
sorted_tuples = sorted(dict1.items(), key=lambda item: item[1])
print("Sorted tuples:",sorted_tuples)
sorted_dict = {k: v for k, v in sorted_tuples}
print("Sorted dict:",sorted_dict)

I expected the output to be:

Unsorted dict: {'5': '101', '1': '001', '17': '10001', '3': '11'}
Sorted tuples: [('3', '11'), ('1', '001'), ('5', '101'), ('17', '10001')]
Sorted dict: {'3': '11', '1': '001', '5': '101', '17': '10001'}

The reason I thought 11 would come before 001 is that 11 is two charcters and 001 is three. This, I assumed, would continue and a five character string would be sorted after a two character string.

However, the output I do get is:

Unsorted dict: {'5': '101', '1': '001', '17': '10001', '3': '11'}
Sorted tuples: [('1', '001'), ('17', '10001'), ('5', '101'), ('3', '11')]
Sorted dict: {'1': '001', '17': '10001', '5': '101', '3': '11'}

The keys for each value are just symbolic, so it's easier to show. The important parts are the values - they're what I want to sort.

Any suggestions please?

3
  • 2
    Strings are sorted by default in alphabetical order. If you want to sort by length, use lambda item: len(item[1])... Commented Jun 26, 2022 at 15:09
  • By the way, sorted_dict can be as simple as dict(sorted_tuples). But anyway dicts are not ordered structures by nature so there is generally not much reason to sort them Commented Jun 26, 2022 at 15:14
  • Well, dicts are sorted these days and there are real world use-cases that made this useful. Commented Jun 26, 2022 at 15:16

1 Answer 1

0

TRy this.

dict1 = {"5": "101", "1": "001", "17": "10001", "3" : "11"}
print("Unsorted dict:",dict1)
sorted_tuples = sorted(dict1.items(), key=lambda item: (len(item[1]),int(item[1]))) 
# you can also change int(item[1]) to int(item[0]) or item[1](for binary string as @Tomerikoo say.)
print("Sorted tuples:",sorted_tuples)
sorted_dict = {k: v for k, v in sorted_tuples}
print("Sorted dict:",sorted_dict)

OUTPUT

Unsorted dict: {'5': '101', '1': '001', '17': '10001', '3': '11'}
Sorted tuples: [('3', '11'), ('1', '001'), ('5', '101'), ('17', '10001')]
Sorted dict: {'3': '11', '1': '001', '5': '101', '17': '10001'}

Explaination.

  1. As you say 11 would come before 001 is that 11 is two charcters and 001 is three. Here you need to sort elements by length using len(item[1]).
Sign up to request clarification or add additional context in comments.

8 Comments

With binary strings, the int conversion is not really necessary (alphabetical order will represent the values' order)
@Tomerikoo Thanks for the information, But What if it is a simple string containing integers?
@Tomerikoo BUt anyway I will comment this in my code.
Then indeed '9' will be treated as greater than '117', but the question is specifically about binary strings... This is why I opened my comment with "with binary strings"
Simply dumping a solution without explanation generally isn't all that useful, especially if it has already been repeated often. Please consider to at least edit in an outline on the why and how.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.