How do delete new lines in input? - python

The input for the code is supposed to be something like
10
1 0 1 0 1 0
and the output is supposed to be the absolute value difference of the number of 1s and 0s. The code works when I enter just the 2nd line but when I enter the first line the array is only [1, 0] so the output doesn't work either. How can I make it so both lines are registered?
array = input()
array = array.replace(" ","")
array = [int(x) for x in str(array)]
one = (array.count(1))
zero = (array.count(0))
output = zero - one
if output < 0:
#output = output * -1
print(output)

You want to take 2 lines of input from the user. But input always only reads 1 line. However, you may make 2 calls to input, one for each line, then join the two lines. E.g.
array = input() # line 1
array += input() # line 2
The rest of your code could be written as follows:
array = array.replace(" ","")
array = [int(x) for x in array]
one = array.count(1)
zero = array.count(0)
output = zero - one
print(output)

Related

Flipping a matrix-like string horizontally

The goal of this function is to flip a matrix-like string horizontally.
For example the string: '100010001' with 2 rows and three columns would look like:
1 0 0
0 1 0
0 0 1
but when flipped should look like:
0 0 1
0 1 0
1 0 0
So the function would return the following output:
'001010100'
The caveat, I cannot use lists or arrays. only strings.
The current code I have written up, I believe, should work, however it is returning an empty string.
def flip_horizontal(image, rows, column):
horizontal_image = ''
for i in range(rows):
#This should slice the image string, and map image(the last element in the
#column : to the first element of the column) onto horizontal_image.
#this will repeat for the given amount of rows
horizontal_image = horizontal_image + image[(i+1)*column-1:i*column]
return horizontal_image
Again this returns an empty string. Any clue what the issue is?
Use [::-1] to reverse each row of the image.
def flip(im, w):
return ''.join(im[i:i+w][::-1] for i in range(0, len(im), w))
>>> im = '100010001'
>>> flip(im, 3)
'001010100'
The range function can be used to isolate your string into steps that represent rows. While iterating through the string you can use [::-1] to reverse each row to achieve the horizontal flip.
string = '100010001'
output = ''
prev = 0
# Iterate through string in steps of 3
for i in range(3, len(string) + 1, 3):
# Isolate and reverse row of string
row = string[prev:i]
row = row[::-1]
output = output + row
prev = i
Input:
'100
010
001'
Output:
'001
010
100'

How to convert '2.6840000e+01' type like datas to float in Python?

I got a "input.txt" file that contains lines like:
1 66.3548 1011100110110010 25
Then i apply some functions column by column:
column stays the same,
column is rounding in a spesific way,
column is converted from binary to decimal,
column is converted from hexadecimal to binary.
And finaly i get this:
[1.0000000e+00 6.6340000e+01 4.7538000e+04 1.0010100e+05]
Then i write this to "fall.txt".
All the operations is working correctly. But i want to see the numbers like:
1 66.34 47538 100101
I placed the columns of the relevant rows in list_for_1. Then i applied the functions to indexes and put them to another list list_for_11. Finally i put all the answers in a matrix. I wrote the matrix to the "fall.txt".
Here's what i did:
with open("input.txt", "r") as file:
#1. TİP SATIRLAR İÇİN GEREKLİ OBJELER
list_for_1 = list()
list_for_11 = list()
#list_final_1 = list()
for line in file:
#EĞER SATIR TİPİ 1 İSE
if line.startswith("1"):
line = line[:-1]
list_for_1 = line.split(" ") #tüm elemanları 1 listede toplama
#1. tip satır için elemanlara gerekli işlemlerin yapılması
list_for_11.append(list_for_1[0]) #ilk satır 1 kalacak
list_for_11.append(float_yuvarla(float(list_for_1[1]))) #float yuvarlama
list_for_11.append(binary_decimal(list_for_1[2])) #binary'den decimal'e
list_for_11.append(hexa_binary(list_for_1[3])) #hexa'dan binary'e
m = 0
n = 0
array1 = np.zeros((6,4))
for i in list_for_11: #listedeki elemanları matrise yerleştirme
if(m > 5):
break
if(isinstance(i, str)):
x = int(i, 2)
array1[m][n] = float(i)
n += 1
if(n == 4):
n = 0
m += 1
with open("fall.txt","w") as ff:
ff.write(str(array1))
ff.write("\n")
Over here i actually send float type to matrix but it's not working:
if(isinstance(i, str)):
x = int(i, 2)
array1[m][n] = float(i)
I'm sort of a new python user, so i might write unnecessarily long and complex codes. If there's any shorter way to do what i did, i would like to get opinions for that as well.
Here's a function to format your numbers the way you want them:
def formatNumber(num):
if num % 1 == 0:
return int(num)
else:
return num
Your list of numbers:
l = [1.0000000e+00, 6.6340000e+01, 4.7538000e+04, 1.0010100e+05]
Reformatting your list of numbers:
for x in l:
print(formatNumber(x))
Output:
1
66.34
47538
100101

How to find the sum of a certain column in a .txt file in Python?

I have a .txt file with 3 rows and 3 columns of data shown below:
1.5 3.1425 blank
10 12 14
8.2 blank 9.5
I am looking to create a function that allows a user to input a number of either 1,2,or 3 and get the sum of that specified column
The error I receive is as follows:
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
summarizer(2)
File "/Users/"practice.py", line
403, in summarizer
print(sum(float(col2)))
ValueError: could not convert string to float: '.'
I'm just practicing my indexing and am running into trouble when trying to pick a specific column or row to analyze. I have the following code, but get errors pertaining to my index being out of range, or a float object not being iterable
def summarizer(searchNum):
infile = open('nums.txt','r')
fileContents = infile.readlines()
infile.close
newList = []
for numbers in fileContents:
numVals = numbers.split('\t')
for i in range(len(numVals)):
for j in range(0, len(numVals[i])):
newList+=numVals[i][j]
col1 = numVals[i][0]
col2 = numVals[i][1]
col3 = numVals[i][2]
if searchNum == 1:
print (sum(float(col1)))
elif searchNum == 2:
print(sum(float(col2)))
else:
print(sum(float(col3)))
If a user inputs summarizer(3), I would like the output to be 23.5 since 14+9.5+0= 23.5
I put comments on the script. You can create three column lists to collect each value in the corresponding columns. Then sum it at the end.
def summarizer(searchNum):
infile = open('nums.txt','r')
fileContents = infile.readlines()
infile.close
col1, col2, col3 = [], [], [] #initialize the columns
for numbers in fileContents:
numVals = numbers.replace('\n','').split('\t') #also remove newline at the end (\n)
col1.append(float(numVals[0]) if numVals[0] else 0) #convert to float if not blank else 0 then add to col1
col2.append(float(numVals[1]) if numVals[1] else 0)
col3.append(float(numVals[2]) if numVals[2] else 0)
if searchNum == 1:
print(sum(col1))
elif searchNum == 2:
print(sum(col2))
else:
print(sum(col3)) #print the sum of col3
return
Result:
summarizer(3)
23.5
You need to make sure that text file is perfectly formatted with tabs. Then you need to append each row to a list, and split each value by tabs.
Then you need to get rid of 'blanks' and '\n' or whatever other non-numbers.
Then sum them.
This is how I would do it
infile = open('nums.txt','r')
fileContents = infile.readlines()
infile.close
newList = [] # List of lists. Each list is a column
for line in fileContents:
newList.append(line.split('\t'))
# - Blank must be 0. Let's get rid of \n as well
for i in range(len(newList)):
for j in range(len(newList[i])):
if '\n' in newList[i][j]:
newList[i][j] = newList[i][j].replace('\n', '')
try:
newList[i][j] = float(newList[i][j]) # get rid of string entries
except ValueError:
newList[i][j] = 0
sum = 0
if searchNum == 1:
for i in range(len(newList)):
sum += newList[i][0]
if searchNum == 2:
for i in range(len(newList)):
sum += newList[i][1]
if searchNum == 3:
for i in range(len(newList)):
sum += newList[i][2]
print(sum)
Explanation of the "could not convert string to float: '.' " error:
col2 variable has a string "blank" (which is not a integer) .
When you apply float on a string which is not a integer ( in our case float(col2)) it throws the error which u mentioned.
What your code actually does:
1.It creates a n*n 2d array and puts all the elements from textfile to the 2d array.
2.You assign the last element in each column to variable col1,col2,col3
3.You apply sum operation on the last element in each column
What you were trying to do :
1.Create a n*n 2d array and puts all the elements from textfile to the 2d array.
2.Apply sum operation on each column element and display the result:
So ur code is not actually doing what you wanted to do.
I have written the below code which does wat u actually intended to do
Solution Code
def summarizer(searchNum):
infile = open('nums.txt','r')
fileContents = infile.readlines()
infile.close
newList = []
for numbers in fileContents:
# - replace the "blank" string and with 0 and makes every instance
#- float type
numbers =numbers.replace("blank","0").replace('\n','').split('\t')
# - creates the 2d array of the items from you text file
for i in range(1,len(numbers)+1):
newList[i].extend(float(numbers[i-1]))
# - prints the sum based on column index u wanted
print(sum(newList(searchNum)))
You can do this easier by using the csv library
https://docs.python.org/2/library/csv.html

Unbroken chain? Python iteration not being processed

So I've written a bit of code to stack integers in a list from the zeroth position. For some reason I cannot decipher, the while loop below is not being processed. I have followed all good style and syntax requirements that I know, and the while loop works when run by itself.
def row(line):
"""
Function that merges a single row or column.
"""
result_length = len(line)
print result_length
# Create a list of zeros the same length as the 'line' argument
pts_alloc = 0
dummy = 0
result = line
result[0:] = [pts_alloc for dummy in range(len(result))]
print result
#Iterate over the 'line' list looking for non-zero entries and
#stack them from 'result[0]'
line_count = 0
result_place = 0
while (line_count <= (len(line)-1)):
if (line[line_count] > 0):
result[result_place] = line[line_count]
print result
result_place += 1
line_count += 1
return result
print row([4, 0, 0, 5])
Is there a major error in this code that I've missed? Is there some syntax requirement that I am unaware of?
The problems seems to be this part:
result = line
result[0:] = [pts_alloc for dummy in range(len(result))]
By replacing a slice of result, with result = line, you are replacing that same slice in line, too, as result is just another reference to the same list, not a copy.
Since the slice is the entire list, anyway, just do:
result = [pts_alloc for dummy in range(len(result))]
Also, you are declaring a lot of unnecessary variables. You could shorten your code to this:
def row(line):
result = [0] * len(line)
result_place = 0
for x in line:
if x > 0:
result[result_place] = x
result_place += 1
return result
Or even this:
def row(line):
non_zero = [x for x in line if x > 0] # take non-zero values
return non_zero + [0] * (len(line) - len(non_zero)) # pad with zeros

How to create a dataset using sequence file in python

I have a protein sequence file looks like this:
>102L:A MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNAAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL -------------------------------------------------------------------------------------------------------------------------------------------------------------------XX
The first one is the name of the sequence, the second one is the actual protein sequence, and the first one is the indicator that shows if there is any missing coordinates. In this case, notice that there is two "X" in the end. That means that the last two residue of the sequence witch are "NL" in this case are missing coordinates.
By coding in Python I would like to generate a table which should look like this:
name of the sequence
total number of missing coordinates (which is the number of X)
the range of these missing coordinates (which is the range of the position of those X)
4)the length of the sequence
5)the actual sequence
So the final results should looks like this:
>102L:A 2 163-164 164 MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNAAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL
And my code looks like this so far:
total_seq = []
with open('sample.txt') as lines:
for l in lines:
split_list = l.split()
# Assign the list number
header = split_list[0] # 1
seq = split_list[1] # 5
disorder = split_list[2]
# count sequence length and total residue of missing coordinates
sequence_length = len(seq) # 4
for x in disorder:
counts = 0
if x == 'X':
counts = counts + 1
total_seq.append([header, seq, str(counts)]) # obviously I haven't finish coding 2 & 3
with open('new_sample.txt', 'a') as f:
for lol in total_seq:
f.write('\n'.join(lol))
I'm new in python, would anyone help please?
Here's your modified code. It now produces your desired output.
with open("sample.txt") as infile:
matrix = [line.split() for line in infile.readlines()]
header_list = [row[0] for row in matrix]
seq_list = [str(row[1]) for row in matrix]
disorder_list = [str(row[2]) for row in matrix]
f = open('new_sample.txt', 'a')
for i in range(len(header_list)):
header = header_list[i]
seq = seq_list[i]
disorder = disorder_list[i]
# count sequence length and total residue of missing coordinates
sequence_length = len(seq)
# get total number of missing coordinates
num_missing = disorder.count('X')
# get the range of these missing coordinates
first_X_pos = disorder.find('X')
last_X_pos = disorder.rfind('X')
range_missing = '-'.join([str(first_X_pos), str(last_X_pos)])
reformat_seq=" ".join([header, str(num_missing), range_missing, str(sequence_length), seq, '\n'])
f.write(reformat_seq)
f.close()
Some more tips:
Don't forget about python's string functions. They will solve a lot of your problems automatically. The documentation is very good.
If you searched for how to do just part 2 or just part 3 in your question, you would find the results elsewhere.

Categories