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
  Converting string the pandas dataframe chrismc 0 7 21 minutes ago
Last Post: chrismc
  Tuple Unpacking HarshaliPatel 2 51 Yesterday, 02:40 PM
Last Post: buran
  Using xpath to get value of a nested element name using tag named xq1xq1xq1 3 74 Jan-18-2019, 07:13 PM
Last Post: Larz60+
  converting the items of a list in new lists clarablanes 2 78 Jan-13-2019, 12:54 PM
Last Post: clarablanes
  Python Selenium getting table element trengan 2 129 Dec-31-2018, 03:02 PM
Last Post: trengan
  Click Element if displayed using Selenium and Python giaco__mar 1 109 Dec-27-2018, 06:19 PM
Last Post: metulburr
  Need help with weird tuple syntax mwskz8 2 93 Dec-13-2018, 09:58 AM
Last Post: buran
  Converting Angle to X and Y Values: 90/180/270 deg qrani 1 153 Nov-21-2018, 06:41 PM
Last Post: woooee
  Replace element in a nested list nagymusic 4 224 Nov-19-2018, 08:03 PM
Last Post: nilamo
  AttributeError: 'tuple' object has no attribute 'interface' Kim6071 0 164 Nov-18-2018, 03:06 PM
Last Post: Kim6071

Forum Jump:


Users browsing this thread: 1 Guest(s)