Bottom Page

Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting List of 3 Element Tuple to Dictionary
#11
(Jan-11-2019, 04:27 PM)DeaD_EyE Wrote: result = []
for element in data:
    sorted_tuple = sorted(element)
    result.append(sorted_tuple)
print(result)
It worked Many thanks!!!
Quote
#12
Based on DeaD_EyE code we can build one-liner list comprehension that sorts both elements and list:

>>> data = [(1, 5, 1), (1, 3, 2), (1, 3, 1), (1, 1, 4)]
>>> sorted([tuple(sorted(element)) for element in data])
[(1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 2, 3)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Question about doc strings and tuple separator ? mcgrim 1 58 Mar-20-2019, 12:57 PM
Last Post: Yoriz
  Dictionary adds an unexpected list erina 1 110 Mar-14-2019, 01:13 PM
Last Post: ichabod801
  TypeError: 'tuple' object is not callable marlinayakno 1 81 Mar-13-2019, 11:20 AM
Last Post: stullis
  Converting list elements and sublists from int to str iMuny 5 148 Mar-10-2019, 09:05 PM
Last Post: iMuny
  Converting R code to python mcva 2 108 Mar-09-2019, 04:01 PM
Last Post: mcva
  Adding a line number to an lxml Element vindy 0 69 Mar-08-2019, 08:34 PM
Last Post: vindy
  Value error when converting hex value to bytearray shubhamjainj 3 79 Mar-01-2019, 08:29 AM
Last Post: DeaD_EyE
  tuple veysel 4 155 Feb-21-2019, 10:42 AM
Last Post: veysel
  Unable to locate element no such element gahhon 6 198 Feb-18-2019, 02:09 PM
Last Post: gahhon
  tuple and formating problem darktitan 7 161 Feb-17-2019, 07:37 PM
Last Post: marienbad

Forum Jump:


Users browsing this thread: 1 Guest(s)