How to Do This Horizontally Instead of Vertically in Python

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')],
]

Sample Image

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



Leave a reply



Submit