I need help with creating something like this. (I´m just a beginner and I need some inspiration on how to create this board with these numbers, this board should parameter n, with this parameter i can choose how big will be this board [9x9 or 13x13]) Thx for every idea!
0 1 2 3 4 5 6 7 8
0 * * *
1 * D *
2 * D *
3 * * * * D * * * *
4 * D D D X D D D *
5 * * * * D * * * *
6 * D *
7 * D *
8 * * *
My Board list looks like this:
board = [[' ',' ',' ','*','*','*',' ',' ',' '],
[' ',' ',' ','*','D','*',' ',' ',' '],
[' ',' ',' ','*','D','*',' ',' ',' '],
['*','*','*','*','D','*','*','*','*'],
['*','D','D','D','X','D','D','D','*'],
['*','*','*','*','D','*','*','*','*'],
[' ',' ',' ','*','D','*',' ',' ',' '],
[' ',' ',' ','*','D','*',' ',' ',' '],
[' ',' ',' ','*','*','*',' ',' ',' ']]
These two lines would work:
print(' '+' '.join(map(str,range(9))))
print('\n'.join([' '.join([str(i)]+v) for i,v in enumerate(board)]))
Output:
0 1 2 3 4 5 6 7 8
0 * * *
1 * D *
2 * D *
3 * * * * D * * * *
4 * D D D X D D D *
5 * * * * D * * * *
6 * D *
7 * D *
8 * * *
Bunch of str.joins can do it!!!
Related
Examples:
m n: 2 3
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
m n: 4 2
1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
I have written this code but i said "list assignment index out of range", how can I fix it? thanks
m, n = input('m n: ').split()
x = []
for i in range(0, int(m)):
for j in range(0, int(n)):
x[j] = int(m[i]) * int(n[j])
print(str(i) + ' * ' + str(j) + ' = ',x[j])
m, n = input('m n: ').split()
for i in range(1, int(m)+1):
for j in range(1, int(n)+1):
print(str(i) + ' * ' + str(j) + ' = ', i * j)
I want to print this pattern in python:(10 rows rhombus increment-decrement numbers)
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
12321
121
1
Note : 1234321 is missing in the last but fourth line, so I think it is not a perfect rhombus. Plus instead of 10, 1 is printed in the tenth line.How to achieve this shape? Please let me know.
I had no idea on how to do this using numbers so,tried the below basic program using * but I don't know how to print the same using numbers as shown above.
n=11
for i in range(n):
print(''*(n-i-1)+'* '*(i+1))
for j in range(n-1,0,-1):
print(''*(n-j)+'* '*(j))
Also this prints half the shape only
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
How to print the pattern I mentioned(using increment-decrement numbers)?
Plus is the question wrong because it misses 1234321 line or it can be achieved?
Please help me.I am new to coding so how to achieve this using the simplest way i.e using looping constructs like for loop? Thanks in advance for your help and time.
Here is a possibility for how to generate the geometry:
n = 11
for i in range(n // 2 + 1):
print(' ' * (n // 2 - i) + '* ' * (i + 1))
for i in reversed(range(n // 2)):
print(' ' * (n // 2 - i) + '* ' * (i + 1))
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
EDIT: Here is an actual implementation with numbers, that fails at n = 10, unfortunately.
from itertools import chain
n = 9
ranges = chain(range(n), reversed(range(n - 1)))
for i in ranges:
print(' ' * (n - i - 1), *[x + 1 for x in range(i + 1)],
*[x + 1 for x in reversed(range(i))])
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
I'd do a function for one line, and run it twice, skipping the 4 at the end:
def oneline(i):
for j in range(i,10):
print(' ', end='')
for j in range(1, i+1):
print(j if j<10 else 1, end='')
for j in range(i-1, 0, -1):
print(j if j<10 else 1, end='')
print()
for i in range(1,11):
oneline(i)
for i in range(9,0,-1):
if i == 4:
continue
oneline(i)
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
12321
121
1
I believe this would be more pythonic:
size = 10
pattern = (size-1)*" " + size*"123456789" # indented pattern
rhombus = [ pattern[i:i+size] for i in range(size) ] # top-left
rhombus = [ d+d[-2::-1] for d in rhombus ] # horizontal mirror
rhombus = rhombus+rhombus[-2::-1] # vertical mirror
The approach leverages the horizontal and vertical symmetry of the output by only generating the top-left corner and then mirroring horizontally and vertically
output:
for line in rhombus: print(line)
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Alternatively, if you want to print as you go, you can work with the left side of the pattern for all the lines and print the horizontally mirrored substring of the pattern for each line:
size = 10
pattern = size*" " + size*"123456789" # indented pattern (1 based)
for i in range(1,size*2): # for each line (1 based)
line = pattern[size-abs(size-i):][:size] # left side of line
print(line + line[-2::-1]) # print mirrored line
Here's a simple version using for loops. Should work for large numbers.
def r(upper=10):
for i in range(-1 * (upper-1), upper):
j = upper - abs(i)
print(' ' * (abs(i)), end ='')
for k in range(-1 * (j-1), j):
print(str(j - abs(k))[0], end='')
print()
r(10)
..
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
Hope it helps :)
Basically I need to create a vertical histogram that cascades downwards.
My code so far:
a = 1
b = 8
c = 6
d = 7
x = [a, b, c, d]
z = max(x)
print(z)
i = 0
while i < z:
i += 1
a -= 1
b -= 1
c -= 1
d -= 1
if a >= 0:
print("*".ljust(5), end="")
if b >= 0:
print("*".ljust(5), end="")
if c >= 0:
print("*".ljust(5), end="")
if d >= 0:
print("*".ljust(5))
output obtained:
* * * *
* * *
* * *
* * *
* * *
* * *
* *
*
Required output:
* * * *
* * *
* * *
* * *
* * *
* * *
* *
*
ps: I'm new to all this so please excuse my ignorance 😁
Your code is almost working as is, but the *s are shifting over between columns.
If I change the *s to be the variable they are for, your current output looks like this:
a b c d
b c d
b c d
b c d
b c d
b c d
b d
b
You just need to print some whitespace when your if conditions come up False. So each one becomes
if a >= 0:
print("*".ljust(5), end="")
else:
print(" ".ljust(5), end="")
I have a pivot table result as below :
len
MERCHANT_NAME
MCC_CODE
0.0 58635982
742.0 7378
763.0 750
780.0 281
1520.0 974
frame_mcc_merchant_pvt=pd.pivot_table(frame_mcc_merchant,index=['MCC_CODE'],
values=['MERCHANT_NAME'],aggfunc=[len],fill_value=0)
I need to sort the pivot table by the values of the len of merchant name ?
Pls help
Remove [] for avoid MultiIndex and then sorting by sort_values:
frame_mcc_merchant_pvt=pd.pivot_table(frame_mcc_merchant,
index='MCC_CODE',
values='MERCHANT_NAME',
aggfunc=len,
fill_value=0).sort_values('MERCHANT_NAME')
my code here is copying over the pivot table results, and then checking for the average (arithmetic mean value) and the standard deviation of a batch.
At the section where i copied over the numbers, i am sure you could sort them for size, by getting max / min and then loop through.
Private Sub EvaluateButton_Click()
Dim EvaRange As Range 'Evaluation range
Dim i As Integer 'Counter for free columns
Dim n As Integer 'Counter for number of measurements
Dim a As Integer 'Counter for Abfragenummer
Dim AbfrageZahl As Variant 'Frage nach der Abfrage
Dim z As Integer 'counter für Abfrage
Worksheets("testsheet").Activate
'MsgBox "The name of the active sheet is " & ActiveSheet.Name
z = 0
AbfrageZahl = InputBox("Bitte geben Sie eine Zahl >1 ein für die Abfrage. Q2 2022: 2, Q4 2022: 3, Q2 2023: 3 usw.")
z = AbfrageZahl - 2
Set EvaRange = Range("C10:C999")
EvaRange.Copy Cells(15, 7 + 2 * z)
Range(Cells(15, 7 + 2 * z), (Cells(Rows.Count, 7 + 2 * z))).Select
i = 15
For Each self In Selection.SpecialCells(xlCellTypeConstants)
self.Copy Cells(i, 8 + 2 * z)
i = i + 1
Next
Cells(i - 1, 8 + 2 * z).Clear 'und jetzt die Auswertung
n = i - 16 'definiere mein n
Cells(1, 8 + 2 * z).Value = Date 'schreibe Abfragedatum
Cells(2, 8 + 2 * z).Value = n 'schreibe mein n
Cells(3, 8 + 2 * z).Value = Application.WorksheetFunction.Average(Range(Cells(15, 8 + 2 * z), Cells(Rows.Count, 8 + 2 * z)))
Cells(4, 8 + 2 * z).Value = Application.WorksheetFunction.StDev_S(Range(Cells(15, 8 + 2 * z), Cells(Rows.Count, 8 + 2 * z)))
Cells(5, 8 + 2 * z).Value = Range("C1").Value
Cells(6, 8 + 2 * z).Value = Range("C2").Value
Cells(7, 8 + 2 * z).Value = Range("C3").Value
Cells(8, 8 + 2 * z).Value = Range("C4").Value
Cells(9, 8 + 2 * z).Value = Range("C5").Value
Cells(10, 8 + 2 * z).Value = Range("C6").Value
Cells(11, 8 + 2 * z).Value = Tabelle2.Name
Cells(12, 8 + 2 * z).Value = AbfrageZahl
Cells(1, 7 + 2 * z).Value = "Date of Review"
Cells(2, 7 + 2 * z).Value = "n, Number of Measurements"
Cells(3, 7 + 2 * z).Value = "µ, arithmetic mean"
Cells(4, 7 + 2 * z).Value = "Sigma, stand. Dev. of a batch"
Cells(5, 7 + 2 * z).Value = Range("B1").Value
Cells(6, 7 + 2 * z).Value = Range("B2").Value
Cells(7, 7 + 2 * z).Value = Range("B3").Value
Cells(8, 7 + 2 * z).Value = Range("B4").Value
Cells(9, 7 + 2 * z).Value = Range("B5").Value
Cells(10, 7 + 2 * z).Value = Range("B6").Value
Cells(11, 7 + 2 * z).Value = "Test Name"
Cells(12, 7 + 2 * z).Value = "Review Cycle Nr."
End Sub
I have a file in the following format ;
string1 string2 ........ stringN
value1,1 value1,2 ........ value1,N
. . ........ .
. . ........ .
. . ........ .
valueM,1 valueM,2 ........ valueM,N
M is on the scale of 10000
N is on the scale of 100
Which I need to;
remove empty lines
remove first two columns
keep 7th,14th,21th ... columns and delete the rest
from this file respectively.
it gets very tricky with numpy since there are strings (titles of each column) in this data as well. I would appreciate any guidance.
You have a custom ASCII-table-like format with fixed-with columns:
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
* Row * Instance * test_string * test_string * test_string * test_string * test_string * test_string * test_string * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str *
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
* 0 * 0 * 0 * 50331648 * test_string * 2 * 1 * 13 * 5.76460e+18 * 0 * 50331648 * string__722 * 2 * 1 * 606 * 5.83666e+18 * 0 * 50331648 * string__720 * 2 * 1 * 575 * 5.83666e+18 * 0 * 50331648 * HCAL_SlowDa * 2 * 1 * 36 * 5.76460e+18 * 0 * 50331648 * string__718 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__719 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__723 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__721 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * 212135 * 15080 * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 1 * 0 * 50331648 * * 2 * 1 * 13 * 0 * 0 * 50331648 * * 2 * 1 * 606 * 53440 * 0 * 50331648 * * 2 * 1 * 575 * 53440 * 0 * 50331648 * * 2 * 1 * 36 * 0 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 2 * 0 * 50331648 * * 2 * 1 * 13 * 4294970636 * 0 * 50331648 * * 2 * 1 * 606 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 575 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 36 * 2.70217e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 3 * 0 * 50331648 * * 2 * 1 * 13 * 352321545 * 0 * 50331648 * * 2 * 1 * 606 * 2.30610e+18 * 0 * 50331648 * * 2 * 1 * 575 * 2.30610e+18 * 0 * 50331648 * * 2 * 1 * 36 * 7.30102e+18 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 4 * 0 * 50331648 * * 2 * 1 * 13 * 0 * 0 * 50331648 * * 2 * 1 * 606 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 575 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 36 * 2.82590e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
If we assume that none of the actual data fields contain asterisks themselves, the easiest way to read each row is to use a regular expression to split out the lines.
To output, I'd still use the csv module, because that would make future processing that much easier:
import csv
import re
from itertools import islice
row_split = re.compile('\s*\*\s*')
with open(someinputfile, 'rb') as infile, open(outputfile, 'wb') as outfile:
writer = csv.writer(outfile, delimiter='\t')
next(islice(infile, 3, 3), None) # skip the first 3 lines in the input file
for line in infile:
row = row_split.split(line)[1:-1]
if not row: continue
writer.writerow(row[8::7])
This skips empty rows, and writes only every 7th column (counting from number nine) and skips the rest.
The first row thus is:
['5.76460e+18', '5.83666e+18', '5.83666e+18', '5.76460e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '3340']
This is removing empty lines:
filtered = filter(lambda x: not re.match(r'^\s*$', x), original)
To remove a specific column (I assume your data is stored in a text file):
f = open("textfile.txt","r")
lines = f.readlines()
f.close()
f = open("newfile.txt","w")
Write your lines back, except the lines you want to delete:
list = [0, 1, 6, 13, 20] # remove first,second as well as 7th, 14th and 21th line
for i,line in enumerate(lines):
if i not in list:
f.write(line)
At the end, close the file again.
f.close()