swap 2 letters in python

42 views
Skip to first unread message

Rudi Hammad

unread,
Jan 3, 2016, 7:12:23 PM1/3/16
to Python Programming for Autodesk Maya
hello,
first of all, happy new year!!!
this is my first 2016 question. I have a list like myList=[ 'l_pollexFeatherWingA_loc', 'l_primCovFeathers1AEnd_loc', 'l_primCovFeathers1A_loc', 'l_primCovFeathers2AEnd_loc']
actually, the list is a lot longer but that will work. I want to swap "1A" and to "2A" , to "A1" and "A2". The first element "l_pollexFeatherWingA_loc" is fine because it doesn´t have a number before.
I tried using split, but I can´t figure it out, because the elements of the list are too different. Just swapping "number+Letter" to "letter+number" would be perfect, but I guess it is not possible.
I search on google, but the solutions I found didn´t work for me.
cheers!

Phil Sloggett

unread,
Jan 3, 2016, 10:19:07 PM1/3/16
to Python Programming for Autodesk Maya
regex will be your friend here. Dont bother trying to learn all of regex, but knowing some basic patterns are very handy for exactly this situation (and figure out more complicated ones if/when you need to). For instance, to match a number followed by a single capital letter you could use a pattern like:

"([0-9][A-Z])"

The [0-9] is literally "match any digit", and the [A-Z] means match any capital letter right after it. If you want to include lower case letters then you could use [A-Za-z]. Wrapping the whole thing in brackets "()" means you get a "group" containing the match, which you can use to find/replace later.

some working code to play with:
import re
item = 'l_primCovFeathers1AEnd_loc'
pattern = "([0-9][A-Z])"
match = re.search(pattern, item)
if match:
letter_number = match.group(0)
number_letter = letter_number[::-1]
new_item = item.replace(letter_number, number_letter)
print new_item
else:
print "NO MATCH: %s" % item
Hope that's useful! In terms of regex worth knowing offhand, I'd recommend learning what the basic characters are and how to use them i.e. [] {} () ^ $ . + *    .... They get me by in 99% of situations.

Phil

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2d95fadb-d4a4-4565-8510-60092ded3c6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
Jan 3, 2016, 10:40:25 PM1/3/16
to Python Programming for Autodesk Maya

Rudi Hammad

unread,
Jan 4, 2016, 4:31:57 AM1/4/16
to Python Programming for Autodesk Maya
How do you know all that stuff??!! XD
Wow, thanks. There is no way I would have found that on my own

Rudi Hammad

unread,
Jan 4, 2016, 4:31:59 AM1/4/16
to Python Programming for Autodesk Maya

Marcus Ottosson

unread,
Jan 4, 2016, 5:46:28 AM1/4/16
to python_in...@googlegroups.com

How do you know all that stuff??!! XD

Whenever you think “string manipulation” beyond the simple replace() or upper(), think “Regular Expressions” (aka “regex” or “re”) and you’ve got it. :)

Justin Israel

unread,
Jan 4, 2016, 3:55:09 PM1/4/16
to python_in...@googlegroups.com
On Mon, Jan 4, 2016 at 10:32 PM Rudi Hammad <rudih...@gmail.com> wrote:
How do you know all that stuff??!! XD
Wow, thanks. There is no way I would have found that on my own

At one point, any of us would have asked the same question. I found the answers through searching or asking. Over time you just build up a ton of answers in your brain and become more and more productive. 

Basically... time.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rudi Hammad

unread,
Jan 4, 2016, 8:41:23 PM1/4/16
to Python Programming for Autodesk Maya
Sometimes it is overwhelming to see how much stuff people knows. But it is cool. It keeps me wanting to know more ( until you trough me out from this forum because I keep asking XXXD)


El lunes, 4 de enero de 2016, 21:55:09 (UTC+1), Justin Israel escribió:
On Mon, Jan 4, 2016 at 10:32 PM Rudi Hammad <rudih...@gmail.com> wrote:
How do you know all that stuff??!! XD
Wow, thanks. There is no way I would have found that on my own

At one point, any of us would have asked the same question. I found the answers through searching or asking. Over time you just build up a ton of answers in your brain and become more and more productive. 

Basically... time.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages