Game board printing - list

I'm trying to print the board of my game with this funcion:
boardis a list with the coordinates or cells occupated in the game
lst is a list with all the coordinates just to refer to.
def board_to_string(lst,board):
for n in lst:
for m in range(3):
# it should be board[i]
if n[m] in board[0]:
n[m] = "o"
else:
n[m] = " "
for lst2 in lst:
print("---------------")
print(str(lst2))
a = [['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']]
b = [['b1','a3','a2'],['a2','g1']]
board_to_string(a,b)
Output is:
---------------
[' ', 'o', ' ']
---------------
['o', ' ', ' ']
---------------
['o', ' ', ' ']
There are two problems, I want it to search the coordinate in every sublist board has. I tried adding for i in range(3)but it prints twice and it doesn't work either.
The other problem is that I'm supposed to print a string but I can't think of a way to this, only with lists.

Replace your code with this:
def board_to_string(lst,board):
print("------")
for n in lst:
for m in range(3):
# it should be board[i]
if n[m] in board[0]:
print("o", end="")
elif n[m] in board[1]:
print("x", end="")
else:
print(" ", end="")
print("|", end="")
print("\n------")
a = [['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']]
b = [['b1','a3','a2'],['a1','c1']]
board_to_string(a,b)
Note that I corrected last but one row (list b), too

Related

Q: Python3 - If Statements for changing list lengths

I am attempting to analyze data sets as lists of differing lengths. I am calling lines (rows) of my data set one by one to be analyzed by my function. I want the function to still be run properly regardless of the length of the list.
My Code:
f = open('DataSet.txt')
for line in iter(f):
remove_blanks = ['']
entries = line.split()
''.join([i for i in entries if i not in remove_blanks])
trash = (entries[0], entries[1])
time = int(entries[2])
column = [int(v) for v in entries[3:]]
def myFun():
print(entries)
print_string = ''
if column[0] == 100:
if column[1] >= 250 and column[2] == 300:
if len(column) >= 9:
digit = [chr(x) for x in column[4:9]]
print_string = ('code: ' + ''.join(str(digit[l]) for l in range(5)) + ' ')
if len(column) >= 13:
optional_digit = [chr(d) for d in column[9:13]]
for m in range(0, 4):
print_string += 'Optional Field: ' + optional_digit[m] + ''
else:
print_string += 'No Optional Field '
pass
pass
print(print_string)
print('')
myFun()
f.close()
What is happening is if the length of a line of my data is not long enough (i.e. the list ends at column[6]), I get the error:
line 17, in function
print('Code: ' + digit[l])
IndexError: list index out of range
I want it to still print Code: #number #number #number #number and leave any non-existent columns as blanks when it is printed so that one line may print as Code: ABC9 and the next print as Code: AB if there are differing list lengths.
Please help! :)
Well, just make sure you're not looping over a list longer than available:
print_string = 'code: ' + ''.join(str(digit[l]) for l in range(min(5, len(digit)))) + ' '
or better:
print_string = "code {} ".format("".join(str(dig) for dig in digit[:5]))
Although I have a feeling you're over-complicating this.

List value assignment within for loop

What can I do when I get the following error in this code?
def reverse_word(word):
index = len(word)
new_word = []
for i in range(index - 1, -1, -1):
new_word.append(word[i])
return ''.join(new_word)
def reverse_sentence(sentence):
l = sentence.split()
for i in l:
l[i] = reverse_word(i)
l = ' '.join(l)
print(l)
a = "Hello !Nhoj Want to have lunch?"
reverse_sentence(a)
TypeError: list indices must be integers or slices, not str.
What can I write instead of this line:
l[i] = reverse_word(i)
... this line: l[i] = reverse_word(i) throws an error because i is a string (word), but in l[i], i must be an index.
You probably wanted to do something like this:
words = sentence.plit()
new_sentence = []
for i,word in enumerate(words): #or just for word in words, you don't need the index
new_sentence.append(reverse_word(word))
and then join at return time return ' '.join(new_sentence)
This implementation follows your logic but uses strings instead of lists.
def reverse_word(word):
new_word = ''
for i in range(len(word) - 1, -1, -1):
new_word += word[i]
return new_word
def reverse_sentence(sentence):
r = ''
for word in sentence.split():
r += reverse_word(word) + ' '
return r[:-1]
>>> a = "Hello !Nhoj Want to have lunch?"
>>> reverse_sentence(a)
>>> 'olleH johN! tnaW ot evah ?hcnul'

Is it possible to convert a string to a list index in Python (2.7)?

I am an absolute beginner currently working on a Tic Tac Toe game and in the below example I am stuck on how to convert a string (e.g. "row1[0]") to a list index (e.g. row1[0]). Basically, I am unsure why eval(aa) = a does not work, but, for example, row1[0] = a does work (and yes, I am aware that eval() is usually frowned upon but have been unable to find any alternatives, as dictionaries, exec, and compile have all failed).
Please also note this is not the full code, just one of my attempts at figuring out the above. Would really appreciate your input on this specific step, I've been unable to find an answer so far. Thanks.
row1 = [_,_,_]
row2 = [_,_,_]
row3 = [_,_,_]
a = raw_input("Player 1, choose your marker - X or O: ")
aa = raw_input("Player 1, choose box (row#[box # - 1]): ")
#Attempt at assigning "X" or "O" to a row index.
eval(aa) = a
print row1
print row2
print row3
eval('row1[0]') = 'X' doesn't work for the same reason that 'a' = 'b' would not work.
eval('row1[0]') returns the value of the list row1 at index 0, not the reference to it.
If row1 is ['', '', ''] then eval(row1[0]) will return an empty string (''), and you can't assign the string 'X' to an empty string.
If you would like to use eval then the user will have to input the list name and the index separately.
row1 = ['', '', '']
eval('row1')[0] = 'X'
print row1
# ['X', '', '']
I realize that you are a beginner but my suggestion is to use a class so you will be able to use getattr. getattr is a built-in function in Python that accepts an object and a string and returns the object's attribute with that name.
class Board(object):
def __init__(self):
self.row1 = ['', '', '']
self.row2 = ['', '', '']
self.row3 = ['' ,'', '']
board = Board()
shape = raw_input("Player 1, choose your marker - X or O: ")
row_and_col = raw_input("Player 1, choose box (row#,col#): ") # user will input 1,0
# for example
row_number, col_number = row_and_col.split(',') # split divides the string it is called
# upon with the character it gets as
# the argument. In this case
# 'row_and_col' is '1,0' so after this
# line row_number is '1' and col_number
# is '0'
relevant_row = getattr(board, 'row' + row_number)
# now relevant_row actually holds the reference to the list,
# not its value as eval would have returned
relevant_row[int(col_number)] = shape

comparing two lists of unequal length at each index

I have two lists of unequal length such as
list1 = ['G','T','C','A','G']
list2 = ['AAAAA','TTTT','GGGG','CCCCCCCC']
I want to compare these two lists at each index only against the corresponding positions i.e list2[0] against list1[0] and list2[1] against list1[1] and so on upto the length of list1.
And get two new lists one having the mismatches and the second having the position of mismatches for example in the language of coding it can be stated as :
if 'G' == 'GGG' or 'G' # where 'G' is from list1[1] and 'GGG' is from list2[2]
elif 'G' == 'AAA'
{
outlist1 == list1[index] # postion of mismatch
outlist2 == 'G/A'
}
ok this works. There are definitely ways to do it in less code, but I think this is pretty clear:
#Function to process the lists
def get_mismatches(list1,list2):
#Prepare the output lists
mismatch_list = []
mismatch_pos = []
#Figure out which list is smaller
smaller_list_len = min(len(list1),len(list2))
#Loop through the lists checking element by element
for ind in range(smaller_list_len):
elem1 = list1[ind][0] #First char of string 1, such as 'G'
elem2 = list2[ind][0] #First char of string 2, such as 'A'
#If they match just continue
if elem1 == elem2:
continue
#If they don't match update the output lists
else:
mismatch_pos.append(ind)
mismatch_list.append(elem1+'/'+elem2)
#Return the output lists
return mismatch_list,mismatch_pos
#Make input lists
list1 = ['G','T','C','A','G']
list2 = ['AAAAA','TTTT','GGGG','CCCCCCCC']
#Call the function to get the output lists
outlist1,outlist2 = get_mismatches(list1,list2)
#Print the output lists:
print outlist1
print outlist2
Output:
['G/A', 'C/G', 'A/C']
[0, 2, 3]
And just to see how short I could get the code I made this function which I think is equivalent:
def short_get_mismatches(l1,l2):
o1,o2 = zip(*[(i,x[0]+'/'+y[0]) for i,(x,y) in enumerate(zip(l1,l2)) if x[0] != y[0]])
return list(o1),list(o2)
#Make input lists
list1 = ['G','T','C','A','G']
list2 = ['AAAAA','TTTT','GGGG','CCCCCCCC']
#Call the function to get the output lists
outlist1,outlist2 = short_get_mismatches(list1,list2)
EDIT:
I'm not sure if I'm cleaning the sequence as you want w/ the N's and -'s. Is this the answer to the example in your comment?
Unclean list1 ['A', 'T', 'G', 'C', 'A', 'C', 'G', 'T', 'C', 'G']
Clean list1 ['A', 'T', 'G', 'C', 'A', 'C', 'G', 'T', 'C', 'G']
Unclean list2 ['GGG', 'TTTN', '-', 'NNN', 'AAA', 'CCC', 'GCCC', 'TTT', 'CCCTN']
Clean list2 ['GGG', 'TTT', 'AAA', 'CCC', 'GCCC', 'TTT', 'CCCT']
0 A GGG
1 T TTT
2 G AAA
3 C CCC
4 A GCCC
5 C TTT
6 G CCCT
['A/G', 'G/A', 'A/G', 'C/T', 'G/C']
[0, 2, 4, 5, 6]
this works fine for my question:
#!/usr/bin/env python
list1=['A', 'T', 'G', 'C', 'A' ,'C', 'G' , 'T' , 'C', 'G']
list2=[ 'GGG' , 'TTTN' , ' - ' , 'NNN' , 'AAA' , 'CCC' , 'GCCC' , 'TTT' ,'CCCATN' ]
notifications = []
indexes = []
for i in range(min(len(list1), len(list2))):
item1 = list1[i]
item2 = list2[i]
# Skip ' - '
if item2 == ' - ':
continue
# Remove N since it's a wildcard
item2 = item2.replace('N', '')
# Remove item1
item2 = item2.replace(item1, '')
chars = set(item2)
# All matched
if len(chars) == 0:
continue
notifications.append('{}/{}'.format(item1, '/'.join(set(item2))))
indexes.append(i)
print(notifications)
print(indexes)
It gives the output as
['A/G', 'G/C', 'C/A/T']
[0, 6, 8]

How to avoid duplicate printing of random choiced names from list ( Python)

This program prints duplicate names generated from list please help me get rid of it I added a operator fr it but it's not working
#Subscriber Selector
import random
print "Welcome to Subscriber Picker"
sub_list = ["Ali Abbas","Ansar Abbasi","Hasan Abidi","Saadia Afzaal","Iqbal Ahmad","Iftikhar Ahmad","Khaled Ahmed","Ahmed Tamim","Maulana Mahboob Alam","Malik Barkat Ali"]
def add_list():
input_1 = int(raw_input("How many new users do you want to add? "))
for z in range (0,input_1):
sub_list.append(raw_input ("Enter Name" +" "+ str(z+1) + ":"))
return
add_list()
def generator():
input_2=int(raw_input("How many subscribers to generate? "))
print "-----"
index=0
temp_list = []
ran_name = random.randint(0, len(sub_list)-1)
temp_list.append(sub_list[ran_name])
while len(temp_list) < input_2:
ran_name=random.randint(0,len(sub_list)-1)
temp_list.append(sub_list[ran_name])
if(temp_list[index] == temp_list[index+1]):
temp_list.pop(index)
else:
index = index + 1
for x in temp_list:
print x
print"-----"
return
generator()
Here you go:
temp_list = random.sample( sub_list, input_2 )