how to print unicode number series in python? - python

I am just trying to print the Unicode number ranging from 1 to 100 in python. I have searched a lot in StackOverflow but no question answers my queries.
So basically I want to print Bengali numbers from ১ to ১০০. The corresponding English number is 1 to 100.
What I have tried is to get the Unicode number of ১ which is '\u09E7'. Then I have tried to increase this number by 1 as depicted in the following code:
x = '\u09E7'
print(x+1)
But the above code says to me the following output.
TypeError: can only concatenate str (not "int") to str
So what I want is to get a number series as following:
১, ২, ৩, ৪, ৫, ৬, ৭, ৮, ৯, ১০, ১১, ১২, ১৩, ............, ১০০
TypeError: can only concatenate str (not "int") to str1
I wish if there is any solution to this. Thank you.

Make a translation table. The function str.maketrans() takes a string of characters and a string of replacements and builds a translation dictionary of Unicode ordinals to Unicode ordinals. Then, convert a counter variable to a string and use the translate() function on the result to convert the string:
#coding:utf8
xlat = str.maketrans('0123456789','০১২৩৪৫৬৭৮৯')
for i in range(1,101):
print(f'{i:3d} {str(i).translate(xlat)}',end=' ')
Output:
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 ৩৪ 35 ৩৫ 36 ৩৬ 37 ৩৭ 38 ৩৮ 39 ৩৯ 40 ৪০ 41 ৪১ 42 ৪২ 43 ৪৩ 44 ৪৪ 45 ৪৫ 46 ৪৬ 47 ৪৭ 48 ৪৮ 49 ৪৯ 50 ৫০ 51 ৫১ 52 ৫২ 53 ৫৩ 54 ৫৪ 55 ৫৫ 56 ৫৬ 57 ৫৭ 58 ৫৮ 59 ৫৯ 60 ৬০ 61 ৬১ 62 ৬২ 63 ৬৩ 64 ৬৪ 65 ৬৫ 66 ৬৬ 67 ৬৭ 68 ৬৮ 69 ৬৯ 70 ৭০ 71 ৭১ 72 ৭২ 73 ৭৩ 74 ৭৪ 75 ৭৫ 76 ৭৬ 77 ৭৭ 78 ৭৮ 79 ৭৯ 80 ৮০ 81 ৮১ 82 ৮২ 83 ৮৩ 84 ৮৪ 85 ৮৫ 86 ৮৬ 87 ৮৭ 88 ৮৮ 89 ৮৯ 90 ৯০ 91 ৯১ 92 ৯২ 93 ৯৩ 94 ৯৪ 95 ৯৫ 96 ৯৬ 97 ৯৭ 98 ৯৮ 99 ৯৯ 100 ১০০

You can try this. Convert the character to an integer. Do the addition and the convert it to character again. If the number is bigger than 10 you have to convert both digits to characters that's why we are using modulo %.
if num < 10:
x = ord('\u09E6')
print(chr(x+num))
elif num < 100:
mod = num % 10
num = int((num -mod) / 10)
x = ord('\u09E6')
print(''.join([chr(x+num), chr(x+mod)]))
else:
x = ord('\u09E6')
print(''.join([chr(x+1), '\u09E6', '\u09E6']))
You can try running it here
https://repl.it/repls/GloomyBewitchedMultitasking
EDIT:
Providing also javascript code as asked in comments.
function getAsciiNum(num){
zero = "০".charCodeAt(0)
if (num < 10){
return(String.fromCharCode(zero+num))
}
else if (num < 100) {
mod = num % 10
num = Math.floor((num -mod) / 10)
return(String.fromCharCode(zero+num) + String.fromCharCode(zero+mod))
}
else {
return(String.fromCharCode(zero+1) + "০০")
}
}
console.log(getAsciiNum(88))

Related

How to prune out certain results from pandas dataframes using and/or operators

I have the following dataframe named state:
SSSLifestress SSSHealthstress SSSFinancialstress SSSSocialstress
0 61 80 78 46
1 62 85 19 75
2 63 57 62 21
3 64 11 90 26
4 65 31 77 48
and I want to prune out a high scale and low scale where lifestress >= 63 AND either one of the three is true where (healthStress >= 63 OR ssFinance >= 63 OR socialstress >= 63)
So lifestress must be >= 63 and one of the three others must be >= 63 as well as <= 33 for the low scale, same as above.
I have the following code here
high_scale1 = ( state[state['SSSLifestress']>=63].reset_index(drop=True) & (state[state['SSSHealthstress']>=63] | state[state['SSSFinancialstress']>=63] | state[state['SSSSocialstress']>=63])).reset_index(drop=True)
low_scale1 = (state[state['SSSLifestress']<=33].reset_index(drop=True) & (state[state['SSSHealthstress']<=33] | state[state['SSSFinancialstress']<=33] | state[state['SSSSocialstress']<=33])).reset_index(drop=True)
however I get the error of:
TypeError: unsupported operand type(s) for |: 'float' and 'bool'
I'm looking for the following output for the high scale:
SSSLifestress SSSHealthstress SSSFinancialstress SSSSocialstress
0 64 11 90 26
1 65 31 77 48
You don't need to create multiple dataframes and reset their indexes. Just put certain conditions in .loc function. For high scale it would be:
high_scale1 = state.loc[(state['SSSLifestress']>=63) &
((state['SSSHealthstress']>=63) |
(state['SSSFinancialstress']>=63) |
(state['SSSSocialstress']>=63)),
:].reset_index(drop=True)
Output:
SSSLifestress SSSHealthstress SSSFinancialstress SSSSocialstress
0 64 11 90 26
1 65 31 77 48

How to use use numpy random choice to get progressively longer sequences with the same numbers?

What I tried was this:
import numpy as np
def test_random(nr_selections, n, prob):
selected = np.random.choice(n, size=nr_selections, replace= False, p = prob)
print(str(nr_selections) + ': ' + str(selected))
n = 100
prob = np.random.choice(100, n)
prob = prob / np.sum(prob) #only for demonstration purpose
for i in np.arange(10, 100, 10):
np.random.seed(123)
test_random(i, n, prob)
The result was:
10: [68 32 25 54 72 45 96 67 49 40]
20: [68 32 25 54 72 45 96 67 49 40 36 74 46 7 21 20 53 65 89 77]
30: [68 32 25 54 72 45 96 67 49 40 36 74 46 7 21 20 53 62 86 60 35 37 8 48
52 47 31 92 95 56]
40: ...
Contrary to my expectation and hope, the 30 numbers selected do not contain all of the 20 numbers. I also tried using numpy.random.default_rng, but only strayed further away from my desired output. I also simplified the original problem somewhat in the above example. Any help would be greatly appreciated. Thank you!
Edit for clarification: I do not want to generate all the sequences in one loop (like in the example above) but rather use the related sequences in different runs of the same program. (Ideally, without storing them somewhere)

Rot18, encrypt and decrypt (python, Sublime Text)

I am looking for a plugin that can encrypt/decrypt text using rot18 in Sublime Text v3.2.2.
I tried this tutorial (only rot13) but it doesn’t work for me: https://www.sublimetext.com/docs/plugin-examples
I tried a lot of plugins and the only one that works fine is:
(unfortunately it is rot47)
import sublime
import sublime_plugin
class Rot47Command(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
s = self.view.substr(region)
s = ''.join(chr(33 + ((ord(ch) + 14) % 94)) for ch in s)
self.view.replace(edit, region, s)
Does anyone have any functional plugin on rot18, please?
You can adapt your code. Here is how rot_N works:
This is the ASCII-Range up to 127:
a = 32
for k in range(0,16):
print(a+k, chr(a+k), " ", a+16+k, chr(a+16+k), " ", a+32+k, chr(a+32+k), " ",
a+48+k, chr(a+48+k), " ", a+64+k, chr(a+64+k), " ", a+80+k, chr(a+80+k))
# 32 48 0 64 # 80 P 96 ` 112 p
# 33 ! 49 1 65 A 81 Q 97 a 113 q
# 34 " 50 2 66 B 82 R 98 b 114 r
# 35 # 51 3 67 C 83 S 99 c 115 s
# 36 $ 52 4 68 D 84 T 100 d 116 t
# 37 % 53 5 69 E 85 U 101 e 117 u
# 38 & 54 6 70 F 86 V 102 f 118 v
# 39 ' 55 7 71 G 87 W 103 g 119 w
# 40 ( 56 8 72 H 88 X 104 h 120 x
# 41 ) 57 9 73 I 89 Y 105 i 121 y
# 42 * 58 : 74 J 90 Z 106 j 122 z
# 43 + 59 ; 75 K 91 [ 107 k 123 {
# 44 , 60 < 76 L 92 \ 108 l 124 |
# 45 - 61 = 77 M 93 ] 109 m 125 }
# 46 . 62 > 78 N 94 ^ 110 n 126 ~
# 47 / 63 ? 79 O 95 _ 111 o 127
ROT n means you take the chr(ord(l)+n)'s letter instead. You need to be carefull when wrapping around.
For calculation of rot_N the basic formular is:
def rot_N(n,letter):
return chr( (ord(letter)-32+n) % (128-32) + 32) # 128-32 = 96
You can test it with:
k="Hello Zzzzz"
print( ''.join(rot_N(18, l) for l in k)) # schould give you a tranlation
print( ''.join(rot_N(0, l) for l in k)) # should give the exact text
and test the inverse with:
k_inverse ="Zw~~!2l,,,,"
print( ''.join(rot_N(-18, l) for l in k_inverse)) # use -18 here
print( ''.join(rot_N(0, l) for l in k_inverse))
If you replace
s = ''.join(chr(33 + ((ord(ch) + 14) % 94)) for ch in s)
with
s = ''.join(rot_N(18, ch) for ch in s))
you should be fine.
You do not specify, but I assume you are using ROT-18 on the character set 0..9, A..Z which is 36 characters. 36/2 = 18, hence ROT-18.
ROT-13 works on the 26 alphabetic characters: 26/2 = 13. You want to adapt that to ROT-18.
The major difference is that the alphabetic characters are continuous in the ASCII character set, and that assumption is built into the code you are copying from. The same is true for ROT-47; the ASCII characters used are continuous. With ROT-18, the digits 0..9 and the alphabetic characters, A..Z are not continuous in ASCII. There is a gap between them from : (#58) to # (#64). ASCII codes in that region are neither digits nor letters.
One solution is to set up your own array, not in the ASCII order, where the two are continuous: [0, 1, ... 9, A, B, ... Z]. Write your program to work on that array.
Alternatively you can work with the ASCII codes, treating codes from #58 to #64 specially to make the shift come out right.
The first option is probably easier, and the code will be more similar to the ROT-13 example. The main difference will be replacing the ord() function, which returns the ASCII code, with an equivalent function giving the position in your array.

Multiplying an entire df or matrix by 1000?

I am new to R and Python, so forgive me if this is an elementary question. I have a large data set of genes (columns) by patients (rows), with each value being an RNA expression value (most values falling between 0 and 1). I want to multiply the entire data set by 1000 so that all non-zero values will be >1.
Currently:
Pt GeneA GeneB GeneC
1 0.001 2 0
2 0 0.5 0.002
Would like:
Pt GeneA GeneB GeneC
1 1 2000 0
2 0 500 2
I have tried to do this in both R and Python and am running into issues with both. I have also tried converting my data between data frame and matrix, and it won't work with either. I have searched extensively on this website and find information about how to multiply an entire df/matrix by a vector, or individual columns by a scalar, but not the entire thing. Could someone kindly point me in the right direction? I feel like it can't possibly be this hard :)
Using R:
df <- read.csv("/Users/m/Desktop/data.csv")
df * 100
In Ops.factor(left, right) : ‘*’ not meaningful for factors
mtx <- as.matrix(df)
mtx * 100
Error in mtx * 100 : non-numeric argument to binary operator
Using Python 3.7.6:
df = df * 1000
^ This runs without an error message but the values in the cells are exactly the same, so it didn't actually multiply anything...
df = df.div(.001)
TypeError: unsupported operand type(s) for /: 'str' and 'float'
Any creative ideas or resources to point me in the right direction? Thank you!
What does str(df) give you? At least some of your columns have been converted to factors because they are character strings. Open the csv file in a text editor and make sure the numbers are not surrounded by "" or that missing values have been labeled with a character. Once you have the data read properly it will be simple:
set.seed(42)
dat <- data.frame(matrix(sample.int(100, 100, replace=TRUE), 10, 10))
str(dat)
# 'data.frame': 10 obs. of 10 variables:
# $ X1 : int 49 65 25 74 100 18 49 47 24 71
# $ X2 : int 100 89 37 20 26 3 41 89 27 36
# $ X3 : int 95 5 84 34 92 3 58 97 42 24
# $ X4 : int 30 43 15 22 58 8 36 68 86 18
# $ X5 : int 92 69 4 98 50 99 88 87 49 26
# $ X6 : int 6 6 2 3 21 2 58 10 40 5
# $ X7 : int 33 49 100 73 29 76 84 9 35 93
# $ X8 : int 16 92 69 92 2 82 24 18 69 55
# $ X9 : int 40 21 100 57 100 42 18 91 13 53
# $ X10: int 54 83 32 80 60 29 81 73 85 43
dat1000 <- dat * 1000
Try this option:
df[,c(2:ncol(df)] <- 1000*df[,c(2:ncol(df)]
If you instead wanted a perhaps more generic solution targeting only columns whose name starts with Gene, then use:
df[grep("^Gene", names(df))] <- 1000*df[grep("^Gene", names(df))]
Looking at your target result, you need to multiply all columns except pt. In python:
target_cols = [i for i in df.columns if i!='Pt']
for i in target_cols:
df[i] = df[i].astype(float)
df[i] = df[i]*1000

Python: Predicting series of numbers without INPUT to a NN

I have a random list of series (integers) along with dates in a csv like:
1/1/2019,34 44 57 62 70
12/28/2018,09 10 25 37 38
12/25/2018,02 08 42 43 50
12/21/2018,10 13 61 62 70
12/18/2018,13 22 32 60 69
12/14/2018,05 22 26 43 49
12/11/2018,04 38 39 54 59
12/7/2018,04 10 20 33 57
12/4/2018,28 31 41 42 50
The list goes all the way back to year 1997. What I am trying is to predict the next series (or as closest as possible) based on these data:
The size of the list (2336)
What have I tried?
The approach that I've used so far is (e.g. for 1/1/2019,34 44 57 62 70):
1) Get the occurrence of each number in the list, i.e. the number 34 has occurred 170 times out the total list (2336).
2) Find the percentage of each number that has occurred. i.e.
Perc/Chances(34) = Occurrence/TotalNo.
Chances(34) = 170/2336
Chances(34) = 0.072 ~ 07
One way to get the list would be to just find the 5 numbers from the list with the least Percentages. but that won't be much effective.
On the other hand, Now I have a data which has each number, its percentage and its occurrence. Is there any way I can somehow train a neural network that predicts the next series? or closest.
Hierarchy:
Where comp_data.csv contains data like:
1/1/2019,34 44 57 62 70
12/28/2018,09 10 25 37 38
12/25/2018,02 08 42 43 50
12/21/2018,10 13 61 62 70
12/18/2018,13 22 32 60 69
12/14/2018,05 22 26 43 49
12/11/2018,04 38 39 54 59
12/7/2018,04 10 20 33 57
12/4/2018,28 31 41 42 50
and occurrence.csv contains:
34,170
44,197
57,36
62,38
70,37
09,186
10,210
25,197
37,185
38,206
02,217
08,185
and report.csv contains the number, occurrence and its percentage:
34,3,11
44,1,03
57,5,19
62,5,19
70,5,19
09,1,03
10,5,19
25,2,07
37,3,11
38,2,07
02,1,03
08,2,07
So I have the list of series, its occurrences over a period of time, and the percentages. Is there anyway I can create a NN that expects some INPUTS trains over a data and predicts the OUT (a series in this case)
The Problem:
Which ones would be the Input? As it is a pure random problem. PS. I cannot provide any Input since I need a series without INPUT. Perhaps, a LSTM Network for Regression?

Categories

Resources