Python Forum
set and sorted, not working how expected!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set and sorted, not working how expected!
#1
Hello all,

I implemented the following. I don't understand why the output is unsorted list. I'm using set and sort to filter my list.
freq_list = set(sorted(i.f_center for i in ret))

However breaking down the steps
  1. i.f_center for i in ret # extracts a list of frequency values. See below
a = [1225000000.0, 5800000000.0, 434000000.0, 2450000000.0, 2425000000.0, 5750000000.0, 455000000.0, 441000000.0, 441000000.0, 5850000000.0, 455000000.0, 1225000000.0, 5740000000.0, 5800000000.0, 1200000000.0, 5820000000.0, 441000000.0, 2475000000.0, 5850000000.0, 915000000.0, 2585000000.0, 5740000000.0, 2425000000.0, 5750000000.0, 1165000000.0, 1225000000.0, 915000000.0, 434000000.0, 872000000.0, 5800000000.0, 2415000000.0, 2415000000.0, 2425000000.0, 1105000000.0, 872000000.0, 5750000000.0, 434000000.0, 2450000000.0, 2475000000.0, 455000000.0, 1255000000.0, 1105000000.0, 1255000000.0, 2585000000.0, 5870000000.0, 915000000.0, 2585000000.0, 1165000000.0, 2450000000.0, 5820000000.0, 5870000000.0, 5740000000.0, 5870000000.0, 5820000000.0, 5780000000.0, 2475000000.0, 2415000000.0, 1105000000.0, 5850000000.0, 1200000000.0, 5780000000.0, 1200000000.0, 1165000000.0, 5780000000.0, 872000000.0, 1255000000.0]
[list=2]
[*]now apply the sorted and set functions.
[/list]
a1 = sorted(a)
print(a1)
a2 = set(a1)
print(a2)
You'll notice that 441000000.0, is not where I expect it.
Reply
#2
sets are unordered
https://docs.python.org/3/tutorial/datas....html#sets Wrote:Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
...
Reply
#3
Do it in the other order. Create a set from them if you want to remove duplicates, then sort it (into a list) if you want a particular order.

a2 = sorted(set(a))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple conditional not working as expected return2sender 8 1,013 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Custom method to handle exceptions not working as expected gradlon93 3 1,038 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  Failing to print sorted files tester_V 4 1,277 Nov-12-2022, 06:49 PM
Last Post: tester_V
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,088 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  How to make elements return sorted? notsoexperienced 4 3,242 Sep-24-2020, 09:00 AM
Last Post: perfringo
  Why is my original list also sorted? Pedroski55 1 1,622 Jul-15-2020, 09:25 PM
Last Post: Yoriz
  Outputting Sorted Text files Help charlieroberrts 1 1,742 Jul-05-2020, 08:37 PM
Last Post: menator01
  append not working as expected teachinggeek 2 2,278 Apr-09-2020, 04:32 AM
Last Post: buran
  sorted function example mystery sabaidii2 4 2,578 Feb-10-2020, 09:37 AM
Last Post: DeaD_EyE
  Byte array is sorted when sending via USB daviddlc68 1 2,841 Aug-16-2019, 10:11 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020