Python - editing my range to print certain strings [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to edit my code to produce 10 lines. the first line starts at 0 and last ends at 9. Each line contains a string of 10 integers with 0 being the first and in successive order. I have produced the following and I cannot for the life of me figure out what to do next.
for i in range(10):
for i in range(10):
print(i,end=' ')
print('\n')
which will output
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

If you're trying to make each line 0 0 0 0 0 0 0 0 0 0, and so on, your problem is reusing the variable for your for loops:
for i in range(10):
for j in range(10):
print(i,end=' ') #Now i is 0 for the first line, 1 for the next, and so on
print('\n')

Use this:
for i in range(10):
for j in range(10):
print(i, end=' ')
print('')
Note that the inner loop uses a different variable name, allowing you to access the outer loop variable. Here's the output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9

The first variable is overwritten in each iteration, use a different variable in the second loop:
for i in range(10):
for j in range(10):
print(i,end=' ')
print('\n')

Related

space tower in python with loops

1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
How can I code this in python?
So far this is my code but I can't figure out the spaces.
number = int(input("enter a number to create your triangle: "))
for col in range(number,0,-1):
for row in range(1,col):
print(row, end=" ")
if col<number:
print(" "*(row*2), end="")
for row in range(col-1,0,-1):
print(row, end=" ")
print()
Instead of looping multiple times, you could create a base list containing all numbers as strings, iterate once to print each row and the reverse:
number = 9
base = [str(n) for n in range(1, number+1)]
for i, idx in enumerate(range(len(base)-1, 0, -1)):
if i == 0:
print(' '.join(base + base[::-1]))
base[idx] = ' '
print(' '.join(base + base[::-1]))
Out:
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
As I've pointed in this comment, using all "power" of print() and unpacking you can make it really easy.
By default all arguments passed to print() will be separated with space, so we can just unpack there range from 1 to current index, string of spaces and reverse range from current index to 1:
number = int(input("enter a number to create your triangle: "))
for i, n in enumerate(range(number, 0, -1)):
print(*range(1, n + 1), *(" " * (i * 2)), *range(n, 0, -1))
Output:
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
Important notice: Code above will work properly only for number below 10.

Create a board of random integers 1..6 with no more than two adjacent repeated values

First, here is my code:
import random
Tablero = []
for i in range(0,20):
Tablero.append( [0]*9 )
for Fila in range(20):
for Columna in range(9):
Tablero[Fila][Columna] = random.randint(1,6)
for i in range (0,20):
for j in range (0, 9) :
print(str.rjust(str(Tablero[i][j]), 4), end = "")
print();
input()
And the current output:
3 5 5 2 6 2 2 3 6
6 2 5 3 4 1 6 4 5
1 5 4 2 1 4 6 6 6
4 6 6 2 1 3 5 6 1
5 2 2 1 1 4 5 6 1
3 6 3 1 6 2 6 2 6
1 1 5 2 6 5 3 6 5
1 2 1 6 1 3 4 1 6
5 2 3 2 1 6 3 4 3
4 6 6 6 6 6 5 1 2
4 4 5 4 3 6 5 1 3
4 5 1 2 2 3 5 2 5
1 2 4 5 2 2 3 2 4
2 1 5 3 4 1 2 4 6
1 3 3 4 4 4 6 5 6
6 6 2 2 3 1 6 3 3
1 5 6 4 2 6 2 1 4
2 3 5 5 6 6 2 4 4
5 5 1 6 1 2 5 4 4
2 3 6 5 4 6 2 2 3
Problem: there are cases where the same numbers appear more than 3 times in a row, this only matters for the COLUMNS.
Example:
1 3 3 4 4 4 6 5 6
And it should be:
1 3 3 4 4 2 4 2 6
I am trying this code for like a week... I was able to do it separately but adding it to this program did not work.
A simple code would be greatly appreciated.
EDIT: The program is similar to the game candy crush. In this program, only the columns matter when adding points, that is to say, only these have to be aligned (3,3,3) would be equivalent to a certain score. Therefore, the program should not generate this : 1 3 3 4 4 4 6 5 6, because otherwise it would play on its own. Since then, one must enter the row and column of a number which will be eliminated and that in that position will fall the number that is above.

Python Nested Loop number pattern outputting additional line?

I am trying to create a number grid specifically using for loops. My code is as follows
def draw_grid (num):#x represents row y representes column
for x in range (num):
print(x+1, end= ' ' )
print()
for y in range (num):
print(y+1, end= ' ' )
And my ouput results as this when I draw a grid of 10 for example.
1
1 2 3 4 5 6 7 8 9 10 2
1 2 3 4 5 6 7 8 9 10 3
1 2 3 4 5 6 7 8 9 10 4
1 2 3 4 5 6 7 8 9 10 5
1 2 3 4 5 6 7 8 9 10 6
1 2 3 4 5 6 7 8 9 10 7
1 2 3 4 5 6 7 8 9 10 8
1 2 3 4 5 6 7 8 9 10 9
1 2 3 4 5 6 7 8 9 10 10
1 2 3 4 5 6 7 8 9 10
I have tried manipulating it several different ways but I cannot discern what is creating the 1 at the top and the 2-10 on the rightmost column? Should my Y value be coded differently?
Here is what is happening
def draw_grid (num):#x represents row y representes column
for x in range (num):
print(x+1, end= ' ' )
print()
for y in range (num):
print(y+1, end= ' ' )
In your outer loop you are printing x + 1 on every iteration with no newline, end = ' ' and then printing a new line, print(). On your first iteration its printing 1 with no newline followed by a newline from print() and then it enters your inner loop and is printing 1-10 again with no new line at the end. Now when the second iteration of your outer loop occurs it prints 2, that's going to be printed right after all the y values followed by print() and the process repeats.
What you want is this most likely
def draw_g(num):
for x in range(num):
for y in range(num):
print(y + 1, end = ' ')
print()
draw_g(10)
Here we are only using our outer loop to determine the amount of rows, times we will print all the values in our inner loop. For our first iteration we print all the values of y + 1 in range(num) once that is completed we use print() to advance to the next line and then the second iteration of of outer loop takes place, this repeats for x in range(num)
. And the result is this.
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

Rearrange data file

I am trying to reorganize a .txt file containing a list of data with traits in the columns and the family on the rows. Basically, I need to write a program that creates rows comparing the people in each family so that the traits persons 1 and 2, 1 and 3, and 2 and 3 are compared. i.e.:
A 1 2 7 8 9 10
A 1 3 7 9 9 11
etc.
where A is the family, the first 2 numbers are the people compared, the 3rd and 4th numbers are trait1 such as the measurements for each person, and the final numbers are trait2 such as the BMI values for each person.
My input is like this:
A 1 trait trait
A 2 trait trait
A 3 trait trait
I was able to create a data frame using:
data = pandas.read_csv('family.txt.', sep=" ", header = None)
print(data)
I cannot seem to figure out an efficient way to concatenate the data into the rows needed above. Any help is greatly appreciated!
Thank you
Ok, Consider your data was as follows
A 1 7 4 5 6
A 2 6 5 4 7
A 3 7 7 5 4
B 1 7 4 5 6
B 2 6 5 4 7
B 3 7 7 5 4
Where the first column is the family and the second column is the person_id and all subsequent columns are traits.
Some super dirty and super hastily written code below seems to give you what you want
file_lines = []
out_list = []
final_out = []
def read_file():
global file_lines
with open("sample.txt", 'r') as fd:
file_lines = fd.read().splitlines()
print file_lines
def make_output():
global file_lines, out_list, final_out
out_line = []
for line1 in file_lines:
for line2 in file_lines:
line1c = line1.split(" ")
line2c = line2.split(" ")
if line1c[0] == line2c[0]:
if line1c[1] >= line2c[1]:
continue
else:
out_list = []
out_list.append(line1c[0])
out_list.append(line1c[1])
out_list.append(line2c[1])
for i in range(2, len(line1c)):
out_list.append(line1c[i])
out_list.append(line2c[i])
print " ".join(out_list)
read_file()
make_output()
The output of print is
A 1 2 7 6 4 5 5 4 6 7
A 1 3 7 7 4 7 5 5 6 4
A 2 1 6 7 5 4 4 5 7 6
A 2 3 6 7 5 7 4 5 7 4
A 3 1 7 7 7 4 5 5 4 6
A 3 2 7 6 7 5 5 4 4 7
B 1 2 7 6 4 5 5 4 6 7
B 1 3 7 7 4 7 5 5 6 4
B 2 1 6 7 5 4 4 5 7 6
B 2 3 6 7 5 7 4 5 7 4
B 3 1 7 7 7 4 5 5 4 6
B 3 2 7 6 7 5 5 4 4 7
As you can see In family A person 1 is compared with 2 and 3. 2 is compared with 1 and 3 and 3 is compared with 1 and 2.
Obviously there will be duplication because each person is compared with every other person in the family twice.
It's trivial to remove this by maintaining a list of who has been compared with whom.
P.S: I know the script is really dirty but I just wanted to illustrate what i've done. Not write production code
EDIT: I wanted to write a slightly more complicated duplicate remover. But since the data is so simple a small modification in the continue criterion solved it. the output after this edit is
A 1 2 7 6 4 5 5 4 6 7
A 1 3 7 7 4 7 5 5 6 4
A 2 3 6 7 5 7 4 5 7 4
B 1 2 7 6 4 5 5 4 6 7
B 1 3 7 7 4 7 5 5 6 4
B 2 3 6 7 5 7 4 5 7 4
which is free of duplicates

nested loops results bunched together Python

for j in range(10):
for i in range(10):
print(j,end=" ")
My results are bunched together and I need to have 10 numbers per line. I cant use a print("0123456789"). I have tried print(j,j,j,j,j,j,j,j,j) and I get the results that I'm looking for but I'm sure this isn't the proper way to write the code.
If print(j,j,j,j,j,j,j,j,j) works then you simply need to add another print() after each iteration:
for j in range(10):
for i in range(10):
print(j,end=" ")
print()
Output:
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
Or simply:
for j in range(10):
print(" ".join(str(j) * 10))
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
Why are you using a nested for loop when you can use a single for loop:
for i in range(10):
print('{} '.format(i) * 10)
This is similar to Malik Brahimi's solution, except it doesn't put a space after the last digit on each line:
for i in range(10):
print(' '.join([str(i)]*10))
output
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
Just for fun, here's another way to do it with a single loop, this time using a format string with numbered fields.
fmt = ('{0} ' * 10)[:-1]
for i in range(10):
print(fmt.format(i))

Categories

Resources