swapping every two chars in the string with Python [duplicate] - python

I want to swap each pair of characters in a string. '2143' becomes '1234', 'badcfe' becomes 'abcdef'.
How can I do this in Python?

oneliner:
>>> s = 'badcfe'
>>> ''.join([ s[x:x+2][::-1] for x in range(0, len(s), 2) ])
'abcdef'
s[x:x+2] returns string slice from x to x+2; it is safe for odd len(s).
[::-1] reverses the string in Python
range(0, len(s), 2) returns 0, 2, 4, 6 ... while x < len(s)

The usual way to swap two items in Python is:
a, b = b, a
So it would seem to me that you would just do the same with an extended slice. However, it is slightly complicated because strings aren't mutable; so you have to convert to a list and then back to a string.
Therefore, I would do the following:
>>> s = 'badcfe'
>>> t = list(s)
>>> t[::2], t[1::2] = t[1::2], t[::2]
>>> ''.join(t)
'abcdef'

Here's one way...
>>> s = '2134'
>>> def swap(c, i, j):
... c = list(c)
... c[i], c[j] = c[j], c[i]
... return ''.join(c)
...
>>> swap(s, 0, 1)
'1234'
>>>

''.join(s[i+1]+s[i] for i in range(0, len(s), 2)) # 10.6 usec per loop
or
''.join(x+y for x, y in zip(s[1::2], s[::2])) # 10.3 usec per loop
or if the string can have an odd length:
''.join(x+y for x, y in itertools.izip_longest(s[1::2], s[::2], fillvalue=''))
Note that this won't work with old versions of Python (if I'm not mistaking older than 2.5).
The benchmark was run on python-2.7-8.fc14.1.x86_64 and a Core 2 Duo 6400 CPU with s='0123456789'*4.

If performance or elegance is not an issue, and you just want clarity and have the job done then simply use this:
def swap(text, ch1, ch2):
text = text.replace(ch2, '!',)
text = text.replace(ch1, ch2)
text = text.replace('!', ch1)
return text
This allows you to swap or simply replace chars or substring.
For example, to swap 'ab' <-> 'de' in a text:
_str = "abcdefabcdefabcdef"
print swap(_str, 'ab','de') #decabfdecabfdecabf

Loop over length of string by twos and swap:
def oddswap(st):
s = list(st)
for c in range(0,len(s),2):
t=s[c]
s[c]=s[c+1]
s[c+1]=t
return "".join(s)
giving:
>>> s
'foobar'
>>> oddswap(s)
'ofbora'
and fails on odd-length strings with an IndexError exception.

There is no need to make a list. The following works for even-length strings:
r = ''
for in in range(0, len(s), 2) :
r += s[i + 1] + s[i]
s = r

A more general answer... you can do any single pairwise swap with tuples or strings using this approach:
# item can be a string or tuple and swap can be a list or tuple of two
# indices to swap
def swap_items_by_copy(item, swap):
s0 = min(swap)
s1 = max(swap)
if isinstance(item,str):
return item[:s0]+item[s1]+item[s0+1:s1]+item[s0]+item[s1+1:]
elif isinstance(item,tuple):
return item[:s0]+(item[s1],)+item[s0+1:s1]+(item[s0],)+item[s1+1:]
else:
raise ValueError("Type not supported")
Then you can invoke it like this:
>>> swap_items_by_copy((1,2,3,4,5,6),(1,2))
(1, 3, 2, 4, 5, 6)
>>> swap_items_by_copy("hello",(1,2))
'hlelo'
>>>
Thankfully python gives empty strings or tuples for the cases where the indices refer to non existent slices.

To swap characters in a string a of position l and r
def swap(a, l, r):
a = a[0:l] + a[r] + a[l+1:r] + a[l] + a[r+1:]
return a
Example:
swap("aaabcccdeee", 3, 7) returns "aaadcccbeee"

Do you want the digits sorted? Or are you swapping odd/even indexed digits? Your example is totally unclear.
Sort:
s = '2143'
p=list(s)
p.sort()
s = "".join(p)
s is now '1234'. The trick is here that list(string) breaks it into characters.

Like so:
>>> s = "2143658709"
>>> ''.join([s[i+1] + s[i] for i in range(0, len(s), 2)])
'1234567890'
>>> s = "badcfe"
>>> ''.join([s[i+1] + s[i] for i in range(0, len(s), 2)])
'abcdef'

re.sub(r'(.)(.)',r"\2\1",'abcdef1234')
However re is a bit slow.
def swap(s):
i=iter(s)
while True:
a,b=next(i),next(i)
yield b
yield a
''.join(swap("abcdef1234"))

One more way:
>>> s='123456'
>>> ''.join([''.join(el) for el in zip(s[1::2], s[0::2])])
'214365'

>>> import ctypes
>>> s = 'abcdef'
>>> mutable = ctypes.create_string_buffer(s)
>>> for i in range(0,len(s),2):
>>> mutable[i], mutable[i+1] = mutable[i+1], mutable[i]
>>> s = mutable.value
>>> print s
badcfe

def revstr(a):
b=''
if len(a)%2==0:
for i in range(0,len(a),2):
b += a[i + 1] + a[i]
a=b
else:
c=a[-1]
for i in range(0,len(a)-1,2):
b += a[i + 1] + a[i]
b=b+a[-1]
a=b
return b
a=raw_input('enter a string')
n=revstr(a)
print n

A bit late to the party, but there is actually a pretty simple way to do this:
The index sequence you are looking for can be expressed as the sum of two sequences:
0 1 2 3 ...
+1 -1 +1 -1 ...
Both are easy to express. The first one is just range(N). A sequence that toggles for each i in that range is i % 2. You can adjust the toggle by scaling and offsetting it:
i % 2 -> 0 1 0 1 ...
1 - i % 2 -> 1 0 1 0 ...
2 * (1 - i % 2) -> 2 0 2 0 ...
2 * (1 - i % 2) - 1 -> +1 -1 +1 -1 ...
The entire expression simplifies to i + 1 - 2 * (i % 2), which you can use to join the string almost directly:
result = ''.join(string[i + 1 - 2 * (i % 2)] for i in range(len(string)))
This will work only for an even-length string, so you can check for overruns using min:
N = len(string)
result = ''.join(string[min(i + 1 - 2 * (i % 2), N - 1)] for i in range(N))
Basically a one-liner, doesn't require any iterators beyond a range over the indices, and some very simple integer math.

While the above solutions do work, there is a very simple solution shall we say in "layman's" terms. Someone still learning python and string's can use the other answers but they don't really understand how they work or what each part of the code is doing without a full explanation by the poster as opposed to "this works". The following executes the swapping of every second character in a string and is easy for beginners to understand how it works.
It is simply iterating through the string (any length) by two's (starting from 0 and finding every second character) and then creating a new string (swapped_pair) by adding the current index + 1 (second character) and then the actual index (first character), e.g., index 1 is put at index 0 and then index 0 is put at index 1 and this repeats through iteration of string.
Also added code to ensure string is of even length as it only works for even length.
DrSanjay Bhakkad post above is also a good one that works for even or odd strings and is basically doing the same function as below.
string = "abcdefghijklmnopqrstuvwxyz123"
# use this prior to below iteration if string needs to be even but is possibly odd
if len(string) % 2 != 0:
string = string[:-1]
# iteration to swap every second character in string
swapped_pair = ""
for i in range(0, len(string), 2):
swapped_pair += (string[i + 1] + string[i])
# use this after above iteration for any even or odd length of strings
if len(swapped_pair) % 2 != 0:
swapped_adj += swapped_pair[-1]
print(swapped_pair)
badcfehgjilknmporqtsvuxwzy21 # output if the "needs to be even" code used
badcfehgjilknmporqtsvuxwzy213 # output if the "even or odd" code used

One of the easiest way to swap first two characters from a String is
inputString = '2134'
extractChar = inputString[0:2]
swapExtractedChar = extractChar[::-1] """Reverse the order of string"""
swapFirstTwoChar = swapExtractedChar + inputString[2:]
# swapFirstTwoChar = inputString[0:2][::-1] + inputString[2:] """For one line code"""
print(swapFirstTwoChar)

#Works on even/odd size strings
str = '2143657'
newStr = ''
for i in range(len(str)//2):
newStr += str[i*2+1] + str[i*2]
if len(str)%2 != 0:
newStr += str[-1]
print(newStr)

#Think about how index works with string in Python,
>>> a = "123456"
>>> a[::-1]
'654321'

Related

Extracting number from alphanumeric string and adding them

Given string str containing alphanumeric characters. The task is to calculate the sum of all the numbers present in the string.
Example 1:
Input:
str = 1abc23
Output: 24
Explanation: 1 and 23 are numbers in the
a string which is added to get the sum as
24.
Example 2:
Input:
str = geeks4geeks
Output: 4
Explanation: 4 is the only number, so the
the sum is 4.
I broke down the problem into smaller parts, for first I just want to extract the numbers.
s = "a12bc3d"
number = ""
for i in range(0, len(s)):
if s[i].isdigit():
n=0
number = number + s[i]
while s[i].isdigit():
n = n+1
if s[i + n].isdigit():
number = number + s[i+n] + " "
else:
break
i = i + n + 1
else:
continue
print(number)
my output from the above code is 12 23 but it should be 12 3, as the for loop is starting from the initial point making 2 coming twice, I have tried to move the for loop forward by updating i = i + n + 1 but it's not working out like that.
It will be great if someone gives me a direction, any help is really appreciated.
A slightly simpler approach with regex:
import re
numbers_sum = sum(int(match) for match in re.findall(r'(\d+)', s))
Use itertools.groupby to break the string into groups of digits and not-digits; then convert the digit groups to int and sum them:
>>> from itertools import groupby
>>> def sum_numbers(s: str) -> int:
... return sum(int(''.join(g)) for d, g in groupby(s, str.isdigit) if d)
...
>>> sum_numbers("1abc23")
24
>>> sum_numbers("geeks4geeks")
4
you can use regex.
import re
s='a12bc3d'
sections = re.split('(\d+)',s)
numeric_sections = [int(x) for x in sections if x.isdigit()]
sum_ = sum(numeric_sections)
print(sum_)
I appreciate the solutions with regex and group-by. And I got the solution using logic as well.
`s = "4a7312cfh86"
slist = [i for i in s]
nlist = []
for i in range(len(slist)):
if slist[i].isdigit() and (i != (len(slist) - 1)):
if not slist[i + 1].isdigit():
nlist.append(slist[i])
else:
slist[i + 1] = slist[i] + slist[i + 1]
elif slist[i].isdigit() and (i == (len(slist) - 1)):
nlist.append(slist[i])
def addingElement(arr):
if len(arr) == 0:
return 0
return addingElement(arr[1:]) + int(arr[0])
print(addingElement(nlist))
Output - 7402

Getting every nth character in the string in Python

I have the following function but it doesn't give me the intended result:
def GetNthLetters(text,n):
builtstring=""
for letter in text:
if text.index(letter)%n==0:
builtstring=builtstring+letter
print letter
return builtstring
str.index() finds the first match for your letter. If you have a letter that appears more than once, that'll give you the wrong index. For any given character, you only test if their first occurrence in the string is at a n'th position.
To demonstrate, take a look at the string 'hello world' with the character indices (I used . to mark the space):
0 1 2 3 4 5 6 7 8 9 10
h e l l o . w o r l d
For the letter l, text.index('l') will return 2, so it'll only be included in the output if n is 1 or 2. It doesn't matter that l also appears at index 3 or 9, because you only ever test 2 % n == 0. The same applies for 'o' (positions 4 and 7), only 4 % n == 0 is tested for either.
You could use the enumerate() function to give you a running index:
def GetNthLetters(text, n):
builtstring = ""
for index, letter in enumerate(text):
if index % n == 0:
builtstring = builtstring + letter
return builtstring
Now index is correct for every letter, repeated or not.
However, it'll be much easier to use slicing:
def GetNthLetters(text, n):
return text[::n]
This takes every n'th letter too:
>>> 'foo bar baz'[::2]
'fobrbz'
>>> 'foo bar baz'[::3]
'f ra'
>>> 'foo bar baz'[::4]
'fbb'
If somebody asked me to give every nth character in a string, I wouldn't include the first character. I would rather do something like below:
def GetNthLetters(text, n):
builtstring = ""
for i in range(0, len(text)):
if (i + 1) % n == 0:
# print(text[i])
builtstring = builtstring + text[i]
return builtstring
text = '1234567890123456789012345678901234567890'
nthLetters = GetNthLetters(text, 1)
print(nthLetters)
nthLetters = GetNthLetters(text, 2)
print(nthLetters)
nthLetters = GetNthLetters(text, 3)
print(nthLetters)
nthLetters = GetNthLetters(text, 10)
print(nthLetters)
nthLetters = GetNthLetters(text, 40)
print(nthLetters)
This would yield these results:
1234567890123456789012345678901234567890
24680246802468024680
3692581470369
0000
0
Using Python's slicing syntax:
Python's slicing syntax is much like the range() function. It accepts a start, stop and step value:
string[start : stop : step]
where you can leave any of the parameters blank and they will default to 0, the length of the string and 1 respectively.
This means you can do:
string[::n]
to get a string's every nth characterter.
So you can write the function as:
def getNthLetters(text, n):
return text[::n]
Hope this does what you want!
string = "12345678"
n = 2
splitted_string = string[::n]
print(splitted_string)
# Output : 1357
Hope this will help.!
The problem with the code is everytime the index of same repeated letter will give same result.
For example 'Hello World!'.index('o') will be always 4 so it will not give intended result.
The best way is to enumerate the for loop. so you will get appropriate index.
You should also remember array starts with 0 not 1.

How do I do this "If n is 3, return `1+..1+2+..1+2+3+..`"

Define a function named nested_increasing_additions(n) which receives one positive integer (n) and returns a string as illustrated in the following examples:
If n is 3, the function should return the string:
1+..1+2+..1+2+3+..
If n is 5, the function should return the string:
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5..+
What I think is, I can make n to a list [1,2,3] and use while loop or for loop to repeat n times. For the first loop it returns 1+.., for the second loop it returns 1+2.. somehow (which i don't know) it stops at 2 which is the same as the repeating time.
I don't know if I'm thinking it right. Need some help and explanations! Thank you!
Consecutive evaluations of these strings results in a sequence of tetrahedral numbers. For example, for input 5, the output evaluates to 35. This is the number of spheres you would need to build a tetrahedron of side length 5.
To see how it relates to the sum in the question, note that the discrete "volume" of the tetrahedron would be equal to the sum of the triangle "slices" from top to bottom.
35 = 1 + 3 + 6 + 10 + 15
= 1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5)
By a similar argument, the triangular numbers are made up of slices of consecutive integers.
Please excuse the maths, it was difficult (but not impossible) to adapt a closed-form solution into the desired output format.
def tetrahedral(n):
return n*(n+1)*(n+2)//6
def string_generator(n):
x = tetrahedral(n)
n = N = 1
while x > 0:
while n <= N:
yield str(n) + '+'
n += 1
x -= N*(N+1)//2
n = 1
N += 1
yield '..'
def nested_increasing_additions(n):
return ''.join(string_generator(n))
You can build the complete string step by step, and remember at each step what you have added last:
def nested_increasing_additions(n):
complete_string = ""
add_string = ""
for i in range(1,n+1):
add_string += str(i) + "+"
complete_string += add_string + ".."
return complete_string
print(nested_increasing_additions(1))
print(nested_increasing_additions(3))
print(nested_increasing_additions(5))
The output with python3 is:
1+..
1+..1+2+..1+2+3+..
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+..
def nested_increasing_additions(n):
l=['']
for i in range(1, n+1):
l.append(l[-1]+str(i)+'+')
return '..'.join(l[1:])
This returns a string without the .. at the end. If you want that, just do return '..'.join(l[1:]) + '..'
you can use something like this.
def nested_increasing_additions(n):
string = ""
for i in range(1,n+1): #1
for j in range(1,i+1): #2
string += str(j)+"+" #4
string += ".." #5
print(string)
here is a printout of nested_increasing_additions(4)
1+..1+2+..1+2+3+..1+2+3+4+..
i think it's self explanatory, nothing complicated.
How's this:
def nested_increasing_additions(n):
string = ""
new_string = ""
dot = ".."
for i in range(1, n+1):
new_string+=('{}+'.format(i))
string = string+new_string+dot
print(string)
return (string)
Output:
nested_increasing_additions(3)
'1+..1+2+..1+2+3+..'
Assuming you did want the ".." at the end of each returned string and that recursion is OK, here's a solution:
def nested_increasing_additions(n):
if n == 1:
return "1+.."
else:
return nested_increasing_additions(n-1) + '%s+..' % '+'.join(str(i) for i in range(1, n+1))
print(nested_increasing_additions(3))
print(nested_increasing_additions(5))
type(nested_increasing_additions(1))
Prints:
1+..1+2+..1+2+3+..
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+..
<type 'str'>
Explanation:
The first return and if (true) block ends the recursive call when the passed in argument value reaches 1 via subtraction from the other half.
The second half (else block) calls the next iteration with n subtracted by 1 and the string build of the current iteration (if n!=1).
The complicated looking code '%s+..' % '+'.join(str(i) for i in range(1, n+1)) is just concatenation of a list of numbers generated by the range function, turned into strings and "+.."
range(1, n+1) returns a list of integers starting with 1 until n+1 so range(1,3) yields [1,2]. This is passed to join which places a + between each number.

Joining elements in a list without the join command

I need to join the elements in a list without using the join command, so if for example I have the list:
[12,4,15,11]
The output should be:
1241511
Here is my code so far:
def lists(list1):
answer = 0
h = len(list1)
while list1 != []:
answer = answer + list1[0] * 10 ** h
h = h - 1
list1.pop(0)
print(answer)
But, in the end, the answer ends up being 125610 which is clearly wrong.
I think the logic is OK, but I can't find the problem?
If you just want to print the number rather than return an actual int:
>>> a = [12,4,15,11]
>>> print(*a, sep='')
1241511
You could just convert each element to a string, add them, and then convert back to an int:
def lists(list1):
answer=''
for number in list1:
answer+=str(number)
print(int(answer))
lists([12,4,15,11])
>>>
1241511
s = ""
for x in map(str, x):
s += x
print(s)
1241511
There can be few more options like
Option1
>>> lst=[12,4,15,11]
>>> str(lst).translate(None, '[,] ')
'1241511'
Option 2
>>> join = lambda e: str(e[0]) + join(e[1:]) if e else ""
>>> join(lst)
'1241511'
Option 3
>>> ("{}"*len(lst)).format(*lst)
'1241511'
Option 4
>>> reduce(lambda a,b:a+b,map(str,lst))
'1241511'
a numeric solution, using your code
import math
def numdig(n):
#only positive numbers
if n > 0:
return int(math.log10(n))+1
else:
return 1
def lists(list1):
answer = 0
h = 0
while list1 != []:
answer = answer * 10 ** h + list1[0]
list1.pop(0)
if list1 != []:
h = numdig(list1[0])
print(answer)
lists([12,4,15,11])
You may try map and reduce with lambda like this:
def without_join(alist):
try:
return int(reduce(lambda a,b: a + b, map(str, alist)))
except ValueError, error:
print error
return None
print without_join([12,4,15,11])
Here's an entirely numerical solution, playing off of your notion of messing with powers of 10. You were on the right track, but your implementation assumed all values were 1 digit long.
import math
def lists(list1):
b = 0
foo = 0
for item in reversed(list1):
b += item*(10**foo)
foo += int(math.floor(math.log10(item))) + 1
return b
a = [12, 4, 15, 11]
print lists(a)
This returns 1241511, as requested.
All I'm doing here is looping through the list in reverse order and keeping track of how many digits to the left I need to shift each value. This allows integers with an arbitrary number of digits.
list_name_of_program = [a,b,c,d,e,f]
program = ""
for pro in list_name_of_program:
program += str(pro)
program += "," # you can use seprator a space " " or different
print(program[:-1])
Output:
'a,b,c,d,e,f'

How to replace all instances of a sub-sequence in a list in Python?

I currently use this code:
""" Replace all occurrences of subsequence a with b in list l """
def replace_subsequence(l,a,b):
for i in range(len(l)):
if(l[i:i+len(a)] == a):
l[i:i+len(a)] = b
Example:
>>> l = [1,2,3]
>>> replace_subsequence(l,[2,3],[4])
>>> l
[1, 4]
Is there a more efficient and/or elegant way to do this ?
To improve efficiency, you can use the Boyer–Moore string search algorithm when searching for a sublist in a list
Code (credits)
def match(pattern, list):
matches = []
m = len(list)
n = len(pattern)
rightMostIndexes = preprocessForBadCharacterShift(pattern)
alignedAt = 0
while alignedAt + (n - 1) < m:
for indexInPattern in xrange(n-1, -1, -1):
indexInlist = alignedAt + indexInPattern
x = list[indexInlist]
y = pattern[indexInPattern]
if indexInlist >= m:
break
if x != y:
r = rightMostIndexes.get(x)
if x not in rightMostIndexes:
alignedAt = indexInlist + 1
else:
shift = indexInlist - (alignedAt + r)
alignedAt += (shift > 0 and shift or alignedAt + 1)
break
elif indexInPattern == 0:
matches.append(alignedAt)
alignedAt += 1
return matches
def preprocessForBadCharacterShift(pattern):
map = { }
for i in xrange(len(pattern)-1, -1, -1):
c = pattern[i]
if c not in map:
map[c] = i
return map
if __name__ == "__main__":
matches = match("ana", "bananas")
for integer in matches:
print "Match at:", integer
print (matches == [1, 3] and "OK" or "Failed")
matches = match([1, 2, 3], [0, 1, 2,3 , 4, 5, 6])
for integer in matches:
print "list Match at:", integer
print (matches)
It definitely isn't elegant, but I'm wondering if converting to strings and using string.replace would perform better if your data is as simple as in the example...
def strx(l):
return str(l).strip('[]')
def replace_substring(l, a, b):
return strx(l).replace( strx(a), strx(b) ).split(', ')
Using xrange is a simple improvement that will speed up your code. xrange returns a generator, so performance improvements will be particualy noticeable for long lists. But even with your really short test code I get a decent increase.
Using timeit:
replace_subsequence 0.337936162949, 100000 runs
replace_subsequence_xrange 0.275990962982, 100000 runs
Additionally you should assign a variable to len(a) outside of the loop, this way you won't keep calling the len() function. This will also yield a significant speedup.

Categories