How do i use a loop nested within a loop to create this output:
100
101 102
103 104 105
106 107 108 109
You can do this using a while loop with certain other variables:
>>> st, end, length = 100, 110, 1
>>> while st < end:
... print (" ".join(map(lambda x: "%3d" % x, range(st,st+length))))
... st += length
... length += 1
...
100
101 102
103 104 105
106 107 108 109
Note that I have used a lambda instead of str in the map, this is so that different width numbers don't break up the indentation.
You can similarly base this on the width of the final line as below (in which case there is no need to keep track of the end variable I was doing above):
>>> st, width = 1, 1
>>> while width <= 15:
... print (" ".join(map(lambda x: "%3d" % x, range(st, st + width))))
... st += width
... width += 1
...
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 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
Something like this???
start = 100
lines = 5
for i in range(lines):
stop = start + i + 1
for j in range(a, stop):
print j,
start = stop
print
Just use two-level for loop:
x = 100
for i in range(1, 5):
for j in range(i):
print(x, end=" ")
x += 1
print("")
Related
def display_code_ascii():
for i in range(32, 128):
print(chr(i))
print(display_code_ascii())
This is my code. the output is:
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
#
A
B
C
D
E
But I want to print in a console like this:
32 is 33 is ! 34 is " 35 is # 36 is $ 37 is % 38 is & 39 is ' 40 is ( 41 is )
42 is * 43 is + 44 is , 45 is - 46 is . 47 is / 48 is 0 49 is 1 50 is 2 51 is 3
52 is 4 53 is 5 54 is 6 55 is 7 56 is 8 57 is 9 58 is : 59 is ; 60 is < 61 is =
62 is > 63 is ? 64 is # 65 is A 66 is B 67 is C 68 is D 69 is E 70 is F 71 is G
72 is H 73 is I 74 is J 75 is K 76 is L 77 is M 78 is N 79 is O 80 is P 81 is Q
82 is R 83 is S 84 is T 85 is U 86 is V 87 is W 88 is X 89 is Y 90 is Z 91 is [
92 is \ 93 is ] 94 is ^ 95 is _ 96 is ` 97 is a 98 is b 99 is c 100 is d 101 is e
102 is f 103 is g 104 is h 105 is i 106 is j 107 is k 108 is l 109 is m 110 is n 111 is o
112 is p 113 is q 114 is r 115 is s 116 is t 117 is u 118 is v 119 is w 120 is x 121 is y
122 is z 123 is { 124 is | 125 is } 126 is ~ 127 is None
# set chunk_size which is number of initial elements to handle per each outputted line
chunk_size = 10
def format_element(x):
x = chr(x)
return x if x != '\x7f' else "None"
# prepare initial list elements with output strings
ll = [f"{x} is {format_element(x)}" for x in range(32, 128)]
# split list into chunks using chunk_size
ll = [ll[i:i+chunk_size] for i in range(len(ll))[::chunk_size]]
# join inner lists into output lines strings
ll = [" ".join(x) for x in ll]
# print each line separately
for i in ll:
print(i)
Output:
32 is 33 is ! 34 is " 35 is # 36 is $ 37 is % 38 is & 39 is ' 40 is ( 41 is )
42 is * 43 is + 44 is , 45 is - 46 is . 47 is / 48 is 0 49 is 1 50 is 2 51 is 3
52 is 4 53 is 5 54 is 6 55 is 7 56 is 8 57 is 9 58 is : 59 is ; 60 is < 61 is =
62 is > 63 is ? 64 is # 65 is A 66 is B 67 is C 68 is D 69 is E 70 is F 71 is G
72 is H 73 is I 74 is J 75 is K 76 is L 77 is M 78 is N 79 is O 80 is P 81 is Q
82 is R 83 is S 84 is T 85 is U 86 is V 87 is W 88 is X 89 is Y 90 is Z 91 is [
92 is \ 93 is ] 94 is ^ 95 is _ 96 is ` 97 is a 98 is b 99 is c 100 is d 101 is e
102 is f 103 is g 104 is h 105 is i 106 is j 107 is k 108 is l 109 is m 110 is n 111 is o
112 is p 113 is q 114 is r 115 is s 116 is t 117 is u 118 is v 119 is w 120 is x 121 is y
122 is z 123 is { 124 is | 125 is } 126 is ~ 127 is None
i am trying to convert code from matlab to python.
Can you please help me to convert this code from matlab to python?
in matlab code
z is list and z length is 121
z= 7.0502 5.8030 4.4657 3.0404 1.5416 0 -1.5416 -3.0404 -4.4657
-5.8030 -7.0502 7.5944 6.3059 4.8990 3.3662 1.7189 0 -1.7189 -3.3662 -4.8990 -6.3059 -7.5944 8.2427 6.9282 5.4611 3.8122 1.9735 0 -1.9735 -3.8122 -5.4611 -6.9282 -8.2427 9.0135 7.7027 6.2075 4.4590 2.3803 0 -2.3803 -4.4590 -6.2075 -7.7027 -9.0135 9.9185 8.6576 7.2038 5.4466 3.1530 0 -3.1530 -5.4466 -7.2038 -8.6576 -9.9185 10.9545 9.7980 8.4853 6.9282 4.8990 0 -4.8990 -6.9282 -8.4853 -9.7980 -10.9545 12.0986 11.0885 9.9947 8.8128 7.6119 -6.9282 -7.6119 -8.8128 -9.9947 -11.0885 -12.0986 13.3133 12.4632 11.5988 10.7649 10.0829 -9.7980 -10.0829 -10.7649 -11.5988 -12.4632 -13.3133 14.5583 13.8564 13.1842 12.5910 12.1612 -12.0000 -12.1612 -12.5910 -13.1842 -13.8564 -14.5583 15.8011 15.2238 14.6969 14.2594 13.9626 -13.8564 -13.9626 -14.2594 -14.6969 -15.2238 -15.8011 17.0207 16.5431 16.1227 15.7875 15.5684 -15.4919 -15.5684 -15.7875 -16.1227 -16.5431 -17.0207
Matlab code : [z,index]=sort(abs(z));
after the code
z = 0 0 0 0 0 0 1.5416 1.5416 1.7189 1.7189 1.9735 1.9735 2.3803 2.3803 3.0404 3.0404 3.1530 3.1530 3.3662 3.3662 3.8122 3.8122 4.4590 4.4590 4.4657 4.4657 4.8990 4.8990 4.8990 4.8990 5.4466 5.4466 5.4611 5.4611 5.8030 5.8030 6.2075 6.2075 6.3059 6.3059 6.9282 6.9282 6.9282 6.9282 6.9282 7.0502 7.0502 7.2038 7.2038 7.5944 7.5944 7.6119 7.6119 7.7027 7.7027 8.2427 8.2427 8.4853 8.4853 8.6576 8.6576 8.8128 8.8128 9.0135 9.0135 9.7980 9.7980 9.7980 9.9185 9.9185 9.9947 9.9947 10.0829 10.0829 10.7649 10.7649 10.9545 10.9545 11.0885 11.0885 11.5988 11.5988 12.0000 12.0986 12.0986 12.1612 12.1612 12.4632 12.4632 12.5910
12.5910 13.1842 13.1842 13.3133 13.3133 13.8564 13.8564 13.8564 13.9626 13.9626 14.2594 14.2594 14.5583 14.5583 14.6969 14.6969 15.2238 15.2238 15.4919 15.5684 15.5684 15.7875 15.7875 15.8011 15.8011 16.1227 16.1227 16.5431 16.5431 17.0207 17.0207
and index is
index = 6 17 28 39 50 61 5 7 16 18 27 29 38 40 4 8 49 51 15 19 26 30 37 41 3 9 14 20 60 62 48 52 25 31 2 10 36 42 13 21 24 32 59 63 72 1 11 47 53 12 22 71 73 35 43 23 33 58 64 46 54 70 74 34 44 57 65 83 45 55 69 75 82 84 81 85 56 66 68 76 80 86 94 67 77 93 95 79 87 92 96 91 97 78 88 90 98 105 104 106 103 107 89 99 102 108 101 109 116 115 117 114 118 100 110 113 119 112 120 111 121
so what is the [z,index] in python ?
Do you need to return the index? If you don't, you could use:
z = abs(z)
new_list = sorted(map(abs, z))
index = sorted(range(len(z)), key=lambda k: z[k])
where x is the output and z is the list.
EDIT:
Try that now
Good morning,
i've this txt file space separated:
drive-------------------------------------------------------------------------k='6' seq nelsa s length: 4044778
# 51 1
# 64 1
# 65 1
# 67 2
# 70 1
# 72 1
# 73 1
# 77 1
# 79 1
# 86 1
# 88 2
# 89 1
# 92 1
# 94 1
# 95 1
# 96 2
# 100 1
# 103 1
# 105 1
# 108 1
# 112 1
# 119 1
# 123 1
# 126 1
# 127 1
# 129 1
# 130 1
# 133 3
# 134 1
# 135 1
# 138 2
# 139 1
# 140 1
# 141 1
# 142 1
# 143 1
# 144 2
# 145 1
# 148 2
# 150 3
# 151 1
# 152 1
# 153 1
# 154 1
# 155 1
# 156 1
# 157 1
# 159 3
# 160 1
# 161 1
# 162 1
# 163 1
# 164 1
# 165 2
# 167 2
# 168 1
# 169 1
# 170 1
# 172 2
# 173 1
# 174 1
# 175 1
-------------------------------------------------------------------------k='7' seq nelsa s length: 4044778
# 4 1
# 5 1
# 8 1
# 9 1
# 10 3
# 11 3
# 12 4
# 13 7
# 14 6
# 15 5
# 16 11
# 17 7
# 18 14
# 19 8
# 20 15
# 21 13
# 22 10
# 23 6
# 24 22
# 25 14
# 26 19
# 27 17
# 28 20
# 29 25
# 30 15
# 31 22
# 32 18
# 33 23
# 34 30
# 35 24
# 36 35
# 37 39
# 38 27
# 39 33
# 40 36
# 41 34
# 42 40
# 43 43
# 44 44
# 45 44
# 46 43
# 47 50
# 48 51
# 49 54
# 50 55
# 51 44
# 52 49
# 53 56
# 54 35
# 55 52
# 56 47
# 57 48
# 58 65
# 59 56
# 60 53
# 61 54
# 62 66
# 63 47
# 64 61
# 65 50
# 66 46
# 67 69
# 68 65
# 69 66
# 70 59
# 71 59
# 72 55
# 73 73
# 74 91
# 75 73
# 76 56
# 77 66
# 78 63
# 79 67
# 80 78
# 81 51
# 82 69
# 83 60
# 84 64
# 85 73
# 86 58
# 87 60
# 88 64
# 89 73
# 90 63
# 91 65
# 92 59
# 93 69
# 94 67
# 95 73
# 96 50
# 97 53
# 98 68
# 99 65
# 100 63
# 101 55
# 102 73
# 103 76
# 104 66
# 105 70
# 106 75
# 107 66
# 108 56
# 109 49
# 110 68
# 111 52
# 112 66
# 113 67
# 114 66
# 115 52
# 116 61
# 117 59
# 118 65
# 119 67
# 120 56
# 121 60
# 122 64
# 123 53
# 124 59
# 125 66
# 126 58
# 127 77
# 128 51
# 129 67
# 130 53
# 131 56
# 132 62
# 133 64
# 134 56
# 135 42
# 136 71
# 137 57
# 138 53
# 139 52
# 140 65
# 141 59
# 142 61
# 143 60
# 144 64
I have to calculate with python max weighted_mean and min from the first # to the last # before the -----k='' separator for each K.
How can i say to python this, to do calculus from # to # for each K?
This is the code i've wrote so far:
#! /usr/bin/python
import numpy
import csv
file = open('Acetobacterium_woodii_DSM_1030.mdistr', 'rb')
data = csv.reader(file, delimiter=' ')
table = [row for row in data]
a = table[10]
print a[2]
It's just an example.
Thank you in advance.
An easy solution is the following:
#! /usr/bin/python
import numpy as np
with open('Acetobacterium_woodii_DSM_1030.mdistr', 'r') as f:
# read file
lines = f.read().splitlines()
values = []
weights = []
for line in lines:
if line.startswith("-"):
# print statistics and reset variables
print ("Min: %d, Max: %d, Avg: %d" %(min(values),max(values),
np.average(values,
weights=weights)))
values = []
weights = []
elif line.startswith("#"):
row = line.split()
values.append(int(row[1]))
weights.append(int(row[2]))
# print statistics before quitting
print ("Min: %d, Max: %d, Avg: %d" %(min(values),max(values),
np.average(values,
weights=weights)))
I'm sure there is a more elegant answer out there, but this works:
#parse data
with open('data.txt') as f:
lines = f.readlines()
alldata = []
kdata = []
for i,line in enumerate(lines):
if len(line) > 1:
if line[0] =='#':
x = int(line.split(' ')[1])
y = int(line.split(' ')[2][:-1])
kdata.append([x,y])
else:
if i !=0:
alldata.append([kval,kdata])
ptr = line.find('k=')+3
kval = int(line[ptr:ptr+1])
kdata=[]
alldata.append([kval,kdata])
#analyse data
for kblock in alldata:
kval = kblock[0]
sumx = sum([x for z in kblock[1:] for (x,y) in z])
sumy = sum([y for z in kblock[1:] for (x,y) in z])
sumxy = sum([x*y for z in kblock[1:] for (x,y) in z])
mean = sumxy/sumy
print('The mean of k-{0} is :{1}'.format(kval,mean))
numpywould have been my first choice but if your data is of variable size then this becomes more difficult. Hope this helps.
I have a long 121 element array where the data is stored in ascending order and I want to reshape to an 11x11 matrix and so I use the NumPy reshape command
Z = data.attributevalue[2,time,axial,:]
Z = np.reshape(Z, (int(math.sqrt(datacount)), int(math.sqrt(datacount))))
The data should be oriented in a Cartesian plane and I create the mesh grid with the following
x = np.arange(1.75, 12.5, 1)
y = np.arange(1.75, 12.5, 1)
X,Y = np.meshgrid(x, y)
The issue is that rows of Z are in the wrong order so the data in the last row of the matrix should be in the first and vice-versa. I want to rearrange so the rows are filled in the proper manner. The starting array Z is assembled in the following arrangement [datapoint #1, datapoint #2 ...., datapoint #N]. Datapoint #1 should be in the top left and the last point in the bottom right. Is there a simple way of accomplishing this or do I have to make a function to changed the order of the rows?
my plot statement is the following
surf = self.ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
linewidth=1, antialiased=True)
***UPDATE****
I tried populating the initial array backwards and still no luck. I changed the orientation of the axis to the following
y = np.arrange(12.5,1,-1)
This flipped the data but my axis label is wrong so it is not a real solution to my issue. Any ideas?
It is possible that your original array does not look like a 1x121 array. The following code block shows how you reshape an array from 1x121 to 11x11.
import numpy as np
A = np.arange(1,122)
print A
print A.reshape((11,11))
Gives:
[ 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121]
[[ 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 101 102 103 104 105 106 107 108 109 110]
[111 112 113 114 115 116 117 118 119 120 121]]
I need to write a program that gives an output of the following table:
chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: # A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
Any help would be appreciated, though I would like to ask to not be given the entire answer, but rather hints, so there is some challenge involved for me. Thanks.
The ord and chr functions will help you out:
ord('a') # 97
chr(97) # 'a'
Add to a range, and you got a stew going on!
for i in range(32,128):
print (i, chr(i))
or to be even closer to what you want:
#!/usr/bin/python3
def f(x,y):
for i in range(x,y):
print ('%3d '%i,end=''),
print()
for i in range(x,y):
print ('%3s '%chr(i),end='')
print()
for x in range(32,128,16):
f(x,x+16)
print '''chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: # A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127'''
Hint: use loops in the loops with given range thus distinguishing lines as cases. The converted characters may be added as strings together and you only print the line after you have all strings added.
The full code would be:
for i in range (2,14):
#range(1,13 would have been correct as well, but I want to use the parity)
if i%2==0: #use even numbers for "chr..." line
a="chr: "
for j in range(int((i/2-1)*16+32),int((i/2-1)*16+48)):
#range is a bit complicated due to the i-range
b=str(chr(j))
#the following ifs are used to regulate space depending on character length
if len(b)==1:
s=" "
if len(b)==2:
s=" "
if len(b)==3:
s=" "
a=a+b+s #add new characters with space to the previous ones
print(a)
if i%2==1: #use odd numbers for asc:... line
a="asc: "
for j in range(int(((i-1)/2-1)*16+32),int(((i-1)/2-1)*16+48)):
b=str(j) #in this line you need only the numbers
#the following ifs are used to regulate space depending on character length
if len(b)==1:
s=" "
if len(b)==2:
s=" "
if len(b)==3:
s=" "
a=a+b+s
print(a)
A good friend gave me this tip, and it works! The remaining thing to do is to adjust placeholders:
for i in range (0, 12):
if i%2 ==0:
content ="chr:"
for j in range (32, 48):
content=content+" "+str(chr(j+(i//2)*16))
print (content)
if i%2 ==1:
content = "asc:"
for j in range (32, 48):
content=content+" "+str(j+(i//2)*16)
print (content)
Forgive me as I am just beginning but the following worked for me:
i=32
while i <= 112:
print ('chr:\t'+chr(i)+'\t'+chr(i+1)+'\t'+chr(i+2)+'\t'+chr(i+3)+'\t'+chr(i+4)+'\t'+chr(i+5)+'\t'+chr(i+6)+'\t'+chr(i+7)+'\t'+chr(i+8)+'\t'+chr(i+9)+'\t'+chr(i+10)+'\t'+chr(i+11)+'\t'+chr(i+12)+'\t'+chr(i+13)+'\t'+chr(i+14)+'\t'+chr(i+15))
print ('asc:\t'+str(i)+'\t'+str(i+1)+'\t'+str(i+2)+'\t'+str(i+3)+'\t'+str(i+4)+'\t'+str(i+5)+'\t'+str(i+6)+'\t'+str(i+7)+'\t'+str(i+8)+'\t'+str(i+9)+'\t'+str(i+10)+'\t'+str(i+11)+'\t'+str(i+12)+'\t'+str(i+13)+'\t'+str(i+14)+'\t'+str(i+15))
i=i+16
Now I am just trying to create a second loop to take care of the progression/iteration