How can I do this horizontally instead of vertically in Python?
import random
N = 5 # number of dice throws
values = [0] * N
for i in range(N):
values[i] = random.randint(1,6)
# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))
Sample Output:
You rerolled some dice and the new values are: 1, 1, 6, 1, 5
If you want a function that returns the array values (all 3 types), use the following:
import random
def calcVals(values, N):
for i in range(N):
values[i] = random.randint(1,6)
# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
return values, str_values, new_values
N = 5 # number of dice throws
values = [0] * N
values, str_values, new_values = calcVals(values, N)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))
How do I add elements horizontally instead of vertically in PySimpleGUI?
The layout in following form left
, separator
and right
vertically or in rows.
layout = [
[element1],
[sg.HSeparator(pad=(500,0))],
[element2],
]
Should be like this
layout = [
[element1, sg.HSeparator(pad=(500,0)), element2],
]
Since the element1 and element2 are for another complex layout, use Frame or Column element.
For horizontal layout, Instead of HSeparator
, VSeparator
will be used here.
For elements in Column vertical aligned top, so option vertical_alignment='top'
added.
So the layout
in your code maybe like this,
layout = [
[sg.Column(left_part, vertical_alignment='top'), sg.VSeparator(), sg.Column(right_part, vertical_alignment='top')],
]
How to Make my Merge output Horizontally instead of Vertically in python
Instead just use list comprehention:
l=[[4, 5, 6], [10], [1, 2, 3], [10], [1, 2, 3], [10], [4, 5, 6], [1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9], [1, 2, 3], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [4, 5, 6], [10], [7, 8, 9], [7, 8, 9]]
new_l=[j for i in l for j in i]
print(new_l)
Output :
C:\Users\Desktop>py x.py
[4, 5, 6, 10, 1, 2, 3, 10, 1, 2, 3, 10, 4, 5, 6, 1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9, 1, 2, 3, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 10, 7, 8, 9, 7, 8, 9]
How to change a vertical string to a horizontal string in python?
input=(a\nb\nc\nd\nA\nB\nC\n) #<---Vertical text
result=input.replace("\n", "") #Convert the vertical text to horizontal text
print(result) #Print the horizontal text
Output:
abcABC
100% working for my program
How do I different values of stars vertically instead of horizontally?
Try the following:
import random
def q6():
d = {k: 0 for k in range(2, 13)}
for _ in range(100):
d[random.randint(1, 6) + random.randint(1, 6)] += 1 # random draw
output = ' '.join(f'{k:2}' for k in d) # header
while any(d.values()): # while there are any remaining stars to be drawn
line = '' # begin with an empty string
line = ' '.join(' *' if v else ' ' for v in d.values())
for k in d: # loop over d to reduce the number of remaining stars
if d[k] > 0:
d[k] -= 1
output += '\n' + line # append the line to the output
return output
print(q6())
As you can note, the idea is to reduce the number of stars by 1 at each step, until the numbers are depleted.
Note that the number of stars, i.e., the dict d
, will not be kept intact. In case you want to keep that, use something like d_copy = d.copy()
before the while
loop.
Also this approach uses the fact that dict preserves the order of the items (based on insertion) since python 3.7.
Output:
2 3 4 5 6 7 8 9 10 11 12
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* *
* *
*
*
*
*
*
*
*
*
*
*
*
As per OP's request,
line = ' '.join(' *' if v else ' ' for v in d.values())
uses conditional expression. For example, output = 'a' if x else 'b'
sets output
as 'a'
if x
is True
, and 'b'
if x
is False
.
In this case, python sees v
. If v
is non-zero, it is "truthy", so ' *' if v else ' ' for v
equals to ' *'
. If v
is zero, then v
is "falsy", so it equals to ' '
. In other words, the line is equivalent to
temp = []
for v in d.values():
if v != 0:
temp.append(' *')
else:
temp.append(' ')
line = ' '.join(temp)
Related Topics
Robot Framework Using Python, Key Press Without Selecting Any Button or Element in the Page
Replacing Pandas or Numpy Nan With a None to Use With Mysqldb
How to Write a Lambda Function That Is Conditional on Two Variables (Columns) in Python
How to Install Tesseract for Python on Anaconda
How to Split a CSV File Row to Columns in Python
How to Skip Blank Line While Reading CSV File Using Python
Converting Json into Newline Delimited Json in Python
Python3: Remove a Substring Between Two Delimiting Char
Replace Empty Strings With None/Null Values in Dataframe
Python - How to Sort a List of Alpha and Numeric Values
Json.Loads() Decodes Only With Raw String Literal
How to Assign Class Instance to a Variable and Use That in Other Class
Sharing a Complex Object Between Processes
When to Use Cla(), Clf() or Close() for Clearing a Plot in Matplotlib
How to Stop Execution of All Cells in Jupyter Notebook
Search and Replace a Line in a File in Python