Example of the code that works great
s="abc123"
swap_seq="103254"
swapped=''.join([s[int(i)] for i in swap_seq])
if s[0].isupper(): swapped.capitalize()
print (swapped)
but I have a large amount of characters that I need to be swapped 18 exactly, just wondering how to do the double digits
The example i'm trying to get it working with is the following.
s="0123456789abcdefgh"
swap_seq="1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16"
swapped=''.join([s[int(i)] for i in swap_seq])
if s[0].isupper(): swapped.capitalize()
print (swapped)
Tried ["0", "1", "2"]
but then the input would have to be exactly the same length or I get an error.
What i'm trying to do is have a user input anything up to 18 characters and the code will swap the letters/numbers around.
I think this is what you want, didn't try it. The way I read your code, you are indexing s, using the sequence outlined in swap_seq, so the split will give you a list of the positions, and i, instead of being each possible character in swap_seq, which includes spaces, will now be a list of the strings with the spaces taken out.
swapped=''.join([s[int(i)] for i in swap_seq.split()])
swap_seq shouldn't be a string. It's a sequence of index values so use a more appropriate data type, list a list of integers:
swap_seq=[1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 17, 16]
swapped=''.join([s[i] for i in swap_seq])
Related
I would like to ask a question please regarding printing the number of different numbers in python.
for example:
Let us say that I have the following list:
X = [5, 5, 5]
Since here we have only one number, I want to build a code that can recognize that we have only one number here so the output must be:
1
The number is: 5
Let us say that I have the following list:
X = [5,4,5]
Since here we have two numbers (5 and 4), I want to the code to recognize that we have only two numbers here so the output must be:
2
The numbers are: 4, 5
Let us say that I have the following list:
X = [24,24,24,24,24,24,24,24,26,26,26,26,26,26,26,26]
Since here we have two numbers (24 and 26), I want to the code to recognize that we have only two numbers here so the output must be:
2
The numbers are: 24, 26
You could keep track of unique numbers with a set object:
X = [1,2,3,3,3]
S = set(X)
n = len(S)
print(n, S) # 3 {1,2,3}
Bear in mind sets are unordered, so you would need to convert back to a list and sort them if needed.
you can change this list into set, it will remove duplicate, then you can change it again into list.
list(set(X))
You can try numpy.unique, and use len() on the result
May I ask you please if we can use set() to read the data in a specific column in pandas?
For example, I have the following the DataFrame:
df1= [ 0 -10 2 5
1 24 5 10
2 30 3 6
3 30 2 1
4 30 4 5 ]
where the first column is the index..
I tried first to isolate the second column
[-10
24
30
30
30]
using the following: x = pd.DataFrame(df1, coulmn=[0])
Then, I transposed the column using the following XX = x.T
Then, I used set() function.
However, instead of obtaining
[-10 24 30]
I got the following [0 1 2 3 4]
So set() read the index instead of reading the first column
I have a pandas dataframe, containing four columns; a reference sequence, a read from that reference sequence, and start/end positions of that read. I am trying to iterate over this dataframe and check rows pairwise to see if the reads overlap based on their start and end positions, and merge them if they do. Next, I want to check this newly merged read to the next read in the dataframe to see if they overlap, and merge these as well if they do. So far I have put my data in a pandas DataFrame, but maybe I'm starting to believe that this maybe not be the optimal solution, and maybe e.g. a dictionary would be more suited for this kind of operation.
I have tried multiple things, without any luck, so I am hoping that one of you wonderful people may be able to come up with a solution from the data:
data = [
["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDE", 1, 5],
["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "DEFGHIJK", 4, 11],
["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "IJKLMNOPQRST", 9, 20],
["TESTINGONETWOTHREE", "TEST", 1, 4],
["TESTINGONETWOTHREE", "NGONE", 6, 10],
["TESTINGONETWOTHREE", "NETWOTHR", 9, 16],
]
df = pd.DataFrame(
data, columns=["reference", "read", "start", "end"]
)
print(df)
reference read start end
0 ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDE 1 5
1 ABCDEFGHIJKLMNOPQRSTUVWXYZ DEFGHIJK 4 11
2 ABCDEFGHIJKLMNOPQRSTUVWXYZ IJKLMNOPQRST 9 20
3 TESTINGONETWOTHREE TEST 1 4
4 TESTINGONETWOTHREE NGONE 6 10
5 TESTINGONETWOTHREE NETWOTHR 9 16
In this case, I would like to end up with a new dataframe (or dictionary) that has the merged reads, the reference sequence that they are from and their start and stop positions, like so:
reference read start end
0 ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRST 1 20
1 TESTINGONETWOTHREE TEST 1 4
2 TESTINGONETWOTHREE NGONETWOTHR 6 16
I would very much appreciate any help on this :)
Cheers!
You could use a custom group to identify the non overlapping stretches, then use it to aggregate with join/min/max:
group = df['start'].gt(df.groupby('reference')['end'].shift()-1).cumsum()
# [0, 0, 0, 0, 1, 1]
(df.groupby(['reference', group])
.agg({'read': ''.join, 'start': 'min', 'end': 'max'})
)
output:
read start end
reference
ABCDEFGHIJKLMNOPQRSTUVWXYZ 0 ABCDEDEFGHIJKIJKLMNOPQRST 1 20
TESTINGONETWOTHREE 0 TEST 1 4
1 NGONENETWOTHR 6 16
I hope all of you are having a great day. In my python class, we are learning how to use Numpy, so we got an assignment about that. My question is this: What is a rank array and how can I construct that with using python? My instructor tried to explain that with these lines but I did not understand anything actually :(
These are the instructions:
rank_calculator(A) - 5 pts
Given a numpy ndarray A, return its rank array.
Input: [[ 9 4 15 0 18]
[16 19 8 10 1]]
Return value: [[4 2 6 0 8]
[7 9 3 5 1]]
The return value should be an ndarray of the same size and shape as the original array A.
So, can someone explain that? I am not so good at Python, unfortunately :(
You can use numpy.argsort multiple times to handle a matrix, as suggested in this answer on SO.
import numpy as np
inp = np.array([[9,4,15,0,18],
[16,19,8,10,1]])
inp.ravel().argsort().argsort().reshape(inp.shape)
array([[4, 2, 6, 0, 8],
[7, 9, 3, 5, 1]])
What is a rank matrix?
In summary, if I were to take all the integers in the matrix, and sort them smallest to largest, then assign each one a rank from 0 to 9, that would result in the rank matrix. Notice that the smallest is 0 which gets a rank of 0, while largest is 19, which gets the last rank of 9.
How the double argsort works
#printing them so they align nicely
print('Array ->', end='')
for i in inp.ravel().astype('str'):
print(i.center(4), end='')
print('\n')
print('Sort1 ->', end='')
for i in inp.ravel().argsort().astype('str'):
print(i.center(4), end='')
print('\n')
print('Sort2 ->', end='')
for i in inp.ravel().argsort().argsort().astype('str'):
print(i.center(4), end='')
Array -> 9 4 15 0 18 16 19 8 10 1
Sort1 -> 3 9 1 7 0 8 2 5 4 6
Sort2 -> 4 2 6 0 8 7 9 3 5 1
Let's first summarize what argsort does. It takes the position of each element and puts them where they belong after sorting. Knowing this, we can write a backward logic which is sort of triangular in nature. Lets start from sort2, then sort1 and then array.
0th (in sort2) is 4th (in sort1), 4th (in sort1) is 0th (in array). So 0th (in array) is 0th (in sort2)
9th (in sort2) is 1st (in sort1), 1st (in sort1) is 9th (in array). So, 9th (in array) is 9th (in sort2)
6th (in sort2) is 9th (in sort1), 9th (in sort1) is 6th (in array). So, 6th (in array) is 6th (in sort2)
Its a bit confusing to wrap your head around it, but once you can understand how argsort() works, you shouldn't have a problem.
Q) What is a rank array?
Ans: It's basically the elements in their sorted order.
Basically what your teacher is asking you is to return each elements positions if they were sorted in ascending order.
CODE:
import numpy as np
A = np.array([[9, 4, 15, 0, 18],
[16, 19, 8, 10, 1]])
flatA = A.flatten()
sorted_flatA = sorted(flatA) # will become -> [0, 1, 4, 8, 9, 10, 15, 16, 18, 19]
# Using a 'MAP' to map the values of sorted_faltA to the index of sorted_faltA.
MAP = {}
for i in range(len(sorted_flatA)):
MAP[sorted_flatA[i]] = i
# Then simply going through the 2D array snd replacing the with their ranks.
res = np.zeros(A.shape)
for i in range(A.shape[0]):
for j in range(A.shape[1]):
res[i][j] = MAP[A[i][j]]
print(res)
I know there are related threads, but I've searched and none of them helped me with my problem.
I have a input text file with square matrices that looks as follows:
1 2 3
4 5 6
7 8 9
*
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
Now there could be more matrices following, all with a * in between them.
I want to separate the matrices (without using numpy!) and then using list within lists to work with them (list comprehension might be useful I guess), all the entries would be integers. The first matrix would then look as follows [[1,2,3],[4,5,6],[7,8,9]] and the second one would be [[10,11,12,13,14],[15,16,17,18,19],[20,21,22,23,24],[25,26,27,28,29],[31,31,32,33,34]] and so on.
My plan was to separate the matrices in their own list entries and then (using the fact that the matrices are square, and therefore it can easily be determined how many integers one list entry should contain) using some list comprehension to change the strings into integers. But I'm stuck at the very beginning.
matrix = open('input.txt','r').read()
matrix = matrix.replace('\n',' ')
list = matrix.split('* ')
Now if I print list I get ['1 2 3 4 5 6 7 8 9', '10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34']
The problem is that I'm now stuck with two strings instead of a list of integers.
Any suggestions?
mat_list = [[[int(num_str) for num_str in line.split()] for line in inner_mat.split('\n')] for inner_mat in open('input_mat.txt','r').read().split('\n*\n')]
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34]]]
Now that you have strings, split each string on space and convert each little string to an integer. After that, you can convert each list of strings to a square "matrix" (list of lists).
An additional point: do not use list as a variable name. That conflicts with the built-in list type. In this code I used the name mylist.
newlist = [[int(nstr) for nstr in v.split()] for v in mylist]
# Now convert each list of strings to a square "matrix" (list of lists)
However, note that the approach given by #Selcuk in his comment is a better way to solve your overall problem. Though it would be a little easier to read the entire file into memory and split on the stars, then split each line into its integers. This would result in easier code but larger memory usage.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just started learning python and though the best way would be to solve few easy algorithm questions. I came across the question:
A strange grid has been recovered from an old book. It has 5 columns and infinite number of rows. The bottom row is considered as the first row. First few rows of the grid are like this:
..............
..............
20 22 24 26 28
11 13 15 17 19
10 12 14 16 18
1 3 5 7 9
0 2 4 6 8
The grid grows upwards forever!
Rows are indexed from bottom to top and columns are indexed from left to right.
The task is to find the integer in cth column in rth row of the grid.
Example:
Input: 6 3
Output: 25
The number in the 6th row and 3rd column is 25.
The solution for this problem was:
import sys
for line in sys.stdin:
r = int(line.split(' ')[0])
c = int(line.split(' ')[1])
if r%2 == 1:
print ((r-1)/2)*10 + (c-1)*2
else:
print ((r-1)/2)*10 + (c-1)*2 + 1
I dint understand why are we taking r%2 == 1 and why are we using ((c-1)*2)+1)+(((r-1)/2)
Look at just the odd-numbered rows. In column one the values are 0, 10, 20. In column two the values are 2, 12, 22. In column three, 4, 14, 24.
Now look at just the even-numbered rows. In column one the values are 1, 11, 21. Column two: 3, 13, 23. Column three: 5, 15, 25.
Do you see how as you move up the rows, the value increases by ten? Not on every row, but rather on every other row? This is why we have ((r-1)/2)*10 - first we round r down to the nearest multiple of 2, then we multiply by 10. This gives us the value in the tens place.
Look again at the odd-numbered rows. In row one, the values are 0, 2, 4, 6, 8. In row three: 10, 12, 14, 16, 18. Row five: 20, 22, 24, 26, 28.
Now back to the even-numbered rows. In row two we have 1, 3, 5, 7, 9. Row four: 11, 13, 15, 17, 19.
Do you see how in the rows, the values of the ones digits are increasing by two? In the case of the odd-numbered rows, they are the even numbers. In the even-numbered rows, they are the odds. This is why we have if r%2 == 1: to check if we are dealing with an odd or even row in order to handle this branching behavior.
If the r is odd, we calculate the c-1th multiple of 2 - this is (c-1)*2. On the other hand, if r is even, we calculate the c-1th multiple of 2, plus 1 (thus making the value odd). (c-1)*2 + 1.
Since the value generated by knowing the row number describes the tens digit, and the value generated by knowing the column number describes the ones digit, we can just add these two values together. That is ((r-1)/2)*10 + (c-1)*2 in the case where r is odd and ((r-1)/2)*10 + (c-1)*2 + 1 in the case where r is even.
Thanks John for the Edit suggestion!