This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I want to make a1=0, a2=0,... aN=0.
I thought using "for"
For example N=10
for i in range(0, 10):
print('a%d'%i)
but it isn't not zeros(just print).
So, I did 'a%d'%i=0. but It didn't work
How can I make that?
For printing use .format() (or f-strings on python 3.6+ :
for i in range(0, 10):
print('a{} = {}'.format(i,i)) # the 1st i is put into the 1. {}, the 2nd i is put ...
If you want to calculate with those as names, store them into a dictionary and use the values to calculate with them:
d = {}
for i in range(0, 10):
d["a{}".format(i)] = i # the nth i is put instead nth {}
print("sum a4 to a7: {} + {} + {} + {} = {}".format( # use the values stored in dict to
d["a4"], ["a5"], ["a6"], ["a7"], # calculate and print the single
d["a4"]+d["a5"]+d["a6"]+d["a7"])) # values where needed
Output:
# for loop
a0 = 0
a1 = 1
a2 = 2
a3 = 3
a4 = 4
a5 = 5
a6 = 6
a7 = 7
a8 = 8
a9 = 9
# calculation
sum a4 to a7: 4 + ['a5'] + ['a6'] + ['a7'] = 22
You can use a dictionary for that.
var_name = 'a'
for i in range(0, 10):
key = var_name + str(i) # an
new_values[key] = 0 # assign 0 to the new name
For accessing them individually,
new_values['a1']
>>> 0
or you can access them all together like this,
for k,v in new_values.items():
print(k,'=',v)
outputs:
a0 = 0
a1 = 0
a2 = 0
a3 = 0
a4 = 0
a5 = 0
a6 = 0
a7 = 0
a8 = 0
a9 = 0
Simple solution, using const value x=0, and counter i:
x = 0
for i in range(0,10):
print(f"a{i} = {x}")
output:
a0 = 0
a1 = 0
a2 = 0
a3 = 0
a4 = 0
a5 = 0
a6 = 0
a7 = 0
a8 = 0
a9 = 0
Related
Supposing I have a data frame that looks like:
col1 col2
0 10
1 23
2 21
3 15
I want to subtract each value in col2 with the previous row sequentially, so that we are subtracting the previously subtracted value to get:
col1 col2
0 10 # left unchanged as index == 0
1 13 # 23 - 10
2 8 # 21 - 13
3 7 # 15 - 8
Other solutions that I have found all subtract the previous values as is, and not the new subtracted value. I would like to avoid using for loops as I have a very large dataset.
Try below to understand the 'previously subtracted'
b2 = a2 - a1
b3 = a3 - b2 = a3 - a2 + a1
b4 = a4 - b3 = a4 - a3 + a2 - a1
b5 = a5 - b4 = a5 - a4 + a3 - a2 + a1
So we just do
s = np.arange(len(df))%2
s = s + s - 1
df['new'] = np.tril(np.multiply.outer(s,s)).dot(df.col2)
Out[47]: array([10, 13, 8, 7])
Below a simple pure Pandas (doesn't need to import numpy) approach which is a more straightforward concept and easy to understand from code without additional explanations:
Let's first define a function which will do the required work:
def ssf(val):
global last_val
last_val = val - last_val
return last_val
Using the function above the code for creating the new column will be:
last_val = 0
df['new'] = df.col2.apply(ssf)
Let's compare number of functions/methods used by the pure Pandas approach compared to the numpy one in the other answer.
The Pandas approach uses 2 functions/methods: ssf() and .apply() and 1 operation: simple subtraction.
The numpy approach uses 5 functions/methods: .arange(), len(), .tril(), .multiply.outer() and .dot() and 3 operations: array addition, array subtraction and modulo division.
For function a(0) = 3, a(n) = 2*a(n-1) -1, generator should be like:
def p():
b = 3
while True:
yield b
b = 2 * b -1
So for function c(1) = 9, c(n) = 9*c(n-1) + 10**(n-1)- c(n-1),
how to write the generator for this function?
For the sequence, you have value for c(1) as 9, calculate the value for c(0) using c(1) which turns out to be 1, then write a generator which first yeilds c(0), and c(1), then for each next values, apply the formula, and get the next value and yield it, finally replace the previous value b0 by this next value b1 in order to continue the sequence.
def generate_seq():
b0 = 1
b1 = 9
n=2
yield b0
yield b1
while True:
b1 = 9*b1 + 10**(n-1) - b1
yield b1
b0 = b1
n += 1
seq = generate_seq()
for i in range(10):
print(next(seq))
OUTPUT:
1
9
82
756
7048
66384
631072
6048576
58388608
567108864
Similar to your original, just the extra power of 10:
def p():
c = 1
pow10 = 1
while True:
yield c
c = 8*c + pow10
pow10 *= 10
Try it online!
[]
The Image is the perfect display of my problem.
I have a .txt file. containing the alphabet: a b c d e f g h i ...
I want to insert a number with each letter, so the updated file look like this:
a1 b2 c3 d4 e5 f6 and so on.
How can I do this in Python?
Note: Also, how to do the same when the letters are in a column, not a row?
this is another solution based on your modified question.
you can write the output on the file
with open('s.txt') as s:
alphabet = s.readlines()
i = 1
for l in alphabet:
res = l.split()
print(res[0], " ", (res[1] + str(i)))
i = i+1
Output on the console
A a1
B b2
C c3
D d4
E e5
F f6
G g7
H h8
I i9
J j10
Try this:
with open('s.txt') as s:
alphabet = s.read()
lst = alphabet.split()
for x in range( len(lst)):
print(lst[x]+str(x+1)," ", end='')
output:
a1 b2 c3 d4 e5 f6
I created a pandas dataframe out of some StackOverFlow posts. Used lxml.eTree to separate the code_blocks and the text_blocks. Below code shows the basic outline :
import lxml.etree
a1 = tokensentRDD.map(lambda (a,b): (a,''.join(map(str,b))))
a2 = a1.map(lambda (a,b): (a, b.replace("<", "<")))
a3 = a2.map(lambda (a,b): (a, b.replace(">", ">")))
def parsefunc (x):
html = lxml.etree.HTML(x)
code_block = html.xpath('//code/text()')
text_block = html.xpath('// /text()')
a4 = code_block
a5 = len(code_block)
a6 = text_block
a7 = len(text_block)
a8 = ''.join(map(str,text_block)).split(' ')
a9 = len(a8)
a10 = nltk.word_tokenize(''.join(map(str,text_block)))
numOfI = 0
numOfQue = 0
numOfExclam = 0
for x in a10:
if x == 'I':
numOfI +=1
elif x == '?':
numOfQue +=1
elif x == '!':
numOfExclam
return (a4,a5,a6,a7,a9,numOfI,numOfQue, numOfExclam)
a11 = a3.take(6)
a12 = map(lambda (a,b): (a, parsefunc(b)), a11)
columns = ['code_block', 'len_code', 'text_block', 'len_text', 'words#text_block', 'numOfI', 'numOfQ', 'numOfExclam']
index = map(lambda x:x[0], a12)
data = map(lambda x:x[1], a12)
df = pd.DataFrame(data = data, columns = columns, index = index)
df.index.name = 'Id'
df
code_block len_code text_block len_text words#text_block numOfI numOfQ numOfExclam
Id
4 [decimal 3 [I want to use a track-bar to change a form's ... 18 72 5 1 0
6 [div, ] 5 [I have an absolutely positioned , div, conta... 22 96 4 4 0
9 [DateTime] 1 [Given a , DateTime, representing a person's ... 4 21 2 2 0
11 [DateTime] 1 [Given a specific , DateTime, value, how do I... 12 24 2 1 0
I need to create a Spark DataFrame on order to apply machine learning algorithms on the output. I tried:
sqlContext.createDataFrame(df).show()
The error I receive is:
TypeError: not supported type: <class 'lxml.etree._ElementStringResult'>
Can someone tell me a proper way to convert a Pandas DataFrame into A Spark DataFrame?
Your problem is not related to Pandas. Both code_block (a4) and text_block (a6) contain lxml specific objects which cannot be encoded using SparkSQL types. Converting these to strings should be just enough.
a4 = [str(x) for x in code_block]
a6 = [str(x) for x in text_block]
EDITED
My code:
#!/usr/bin/python3
import os.path
import pickle
def read_hts(hts):
if(os.path.isfile("hts.pickle")):
p = open("hts.pickle", "rb")
hts = pickle.load(p)
p.close()
else:
f = open("hts.dat", "r")
for line in f:
spl = line.split()
val = [spl[2], spl[3], spl[4]]
hts[spl[0]+"."+spl[1]] = val
f.close()
p = open("hts.pickle", "wb")
pickle.dump(hts, p)
p.close()
def main():
hts = {}
read_hts(hts)
print(list(hts))
main()
The hts.dat file:
1 A1 1 1 6523.00
1 A2 1 2 10823.08
1 A3 1 3 8661.76
1 A4 1 4 9851.96
1 A5 1 5 6701.12
1 A6 1 6 12934.13
1 A7 1 7 11882.38
1 A8 1 8 9787.36
1 A9 1 9 10292.06
1 A10 1 10 9040.32
1 A11 1 11 12742.89
1 A12 1 12 11607.01
1 A13 1 13 13638.06
1 A14 1 14 11038.11
1 A15 1 15 11839.42
1 A16 1 16 13206.73
If there is no hts.pickle the output is:
['1.A3', '1.A7', '1.A14', '1.A11', '1.A8', '1.A16', '1.A15', '1.A12', '1.A9', '1.A4', '1.A1', '1.A6', '1.A10', '1.A2', '1.A5', '1.A13']
But if there is a hts.pickle the output is only:
[]
I don't understand why it don't restores the dictonary. EDIT: I think pickle is not the problem. It have to be a problem with the variables.
The issue is not with the pickle , when your pickle file already exists , it is creating a completely new dictionary object and returning and your are assigning this back to hts variable , this will not change the hts variable in your main() function .
In the else part you are changing the hts variable inplace hence it will reflect in the main() function.
Instead of relying on this , you should always return the variable hts from your function , and assign it back to hts in your main() function.