python


How to reverse “reversed” duplicated tuples from a list in Python


Suppose I have a list of tuples:
a = [(a,b), (a,b), (b,c), (b,a), (a,b), (c,b)]
How can I find all the 'reversed' duplicated tuples like (a,b) and (b,a), (b,c) and (c,b); and change elements inside tuples in a consistent order, so it can become:
a = [(a,b), (a,b), (b,c), (a,b), (a,b), (b,c)]
Short answer
Convert tuples to list, sort the list, return it to a tuple type.
def sort_tuples(alistoftuples):
return [tuple(sorted(list(k))) for k in alistoftuples]

Related Links

Running multiple Python scripts
programatically change matplotlib fill data
Python: include module into one *.py file
TypeError: can't multiply sequence by non-int of type 'tuple'
Assert that two dictionaries are almost equal
How to retry urllib2.urlopen n times
Python: Create a multidimensional array from a loop
MySQL SELECT: Find all languages by Unicode
how to write IS NOT for str.endswith in python [closed]
Python — Use of Numpy.mgrid and Numpy.reshape
How can I add more to a file when printing stdout to a file, instead of overwriting the file?
How do I create a custom window title bar using PyQt4?
Trouble printing all items from a list in python
Python datetime vs time
Error handling (dividing by zero) [duplicate]
Regex won't capture past \n

Categories

HOME
converter
ajax
admin-on-rest
dynamics-crm
ncurses
raspbian
concourse
h2
azureservicebus
goutte
netflix-feign
octopus-deploy
unity2d
hashicorp-vault
react-leaflet
dendrogram
anychart
statusbar
deb
pdfsharp
scalajs-react
hash-collision
forum
referenceerror
shapes
pljson
data-extraction
pycparser
superpowered
fltk
common.logging
service-fabric-stateful
powermta
matlab-cvst
fastq
email-parsing
ideone
appstore-approval
sgmlreader
rollback
udev
volume
info
in-memory-database
jszip
cjson
libreadline
alter
slam-algorithm
nofollow
metalsmith
spring-repositories
windows-vista
application-loader
rx-groovy
pl-i
hana-xs
odftoolkit
node-imagemagick
humanizer
yahoo-boss-api
mfmailcomposeviewcontroll
korma
phpdocx
pic24
indexing-service
mbox
edit-in-place
winrt-httpclient
mimosa
exiv2
angulartics
convex-polygon
driver-signing
onselect
sttwitter
html-form-post
client-side-scripting
iconv
digital-design
database-permissions
pascals-triangle
template-haskell
mvccontrib
userid
ajax.beginform
adsl
ruby-debug
yajl
asp.net-mvc-controller
callgrind
project-lifecycle
database-diagramming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App