Add "0" to number in strings if they have two digits only - python

Lets say, I have this array:
s = ["data_s01", "data_s99", "data_s133"]
I want to add "0" after "s" if there are only two digits. so the result is:
["data_s001", "data_s099", "data_s133"]
I have this now:
for v in s:
data = v.split('_s')
if "0" in data:
out_s = data[0] + "0" + data[1]
print(out_s)
But nothing is printed?

>>> ["data_s{:0>3}".format(x[6:]) for x in s]
['data_s001', 'data_s099', 'data_s133']

x=["data_s01", "data_s99", "data_s133"]
print ["".join(["data_s",k.split("_s")[1].zfill(3)]) for k in x]
Try this.

The print function should not be inside the if, since only original strings with no 0 would get printed. Then again, I don't know why you care if there is a 0 in there or not.

Related

specific characters printing with Python

given a string as shown below,
"[xyx],[abc].[cfd],[abc].[dgr],[abc]"
how to print it like shown below ?
1.[xyz]
2.[cfd]
3.[dgr]
The original string will always maintain the above-mentioned format.
I did not realize you had periods and commas... that adds a bit of trickery. You have to split on the periods too
I would use something like this...
list_to_parse = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
count = 0
for i in list_to_parse.split('.'):
for j in i.split(','):
string = str(count + 1) + "." + j
if string:
count += 1
print(string)
string = None
Another option is split on the left bracket, and then just re-add it with enumerate - then strip commas and periods - this method is also probably a tiny bit faster, as it's not a loop inside a loop
list_to_parse = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
for index, i in enumerate(list.split('[')):
if i:
print(str(index) + ".[" + i.rstrip(',.'))
also strip is really "what characters to remove" not a specific pattern. so you can add any characters you want removed from the right, and it will work through the list until it hits a character it can't remove. there is also lstrip() and strip()
string manipulation can always get tricky, so pay attention. as this will output a blank first object, so index zero isn't printed etc... always practice and learn your needs :D
You can use split() function:
a = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
desired_strings = [i.split(',')[0] for i in a.split('.')]
for i,string in enumerate(desired_strings):
print(f"{i+1}.{string}")
This is just a fun way to solve it:
lst = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
count = 1
var = 1
for char in range(0, len(lst), 6):
if var % 2:
print(f"{count}.{lst[char:char + 5]}")
count += 1
var += 1
output:
1.[xyx]
2.[cfd]
3.[dgr]
explanation : "[" appears in these indexes: 0, 6, 12, etc. var is for skipping the next pair. count is the counting variable.
Here we can squeeze the above code using list comprehension and slicing instead of those flag variables. It's now more Pythonic:
lst = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
lst = [lst[i:i+5] for i in range(0, len(lst), 6)][::2]
res = (f"{i}.{item}" for i, item in enumerate(lst, 1))
print("\n".join(res))
You can use RegEx:
import regex as re
pattern=r"(\[[a-zA-Z]*\])\,\[[a-zA-Z]*\]\.?"
results=re.findall(pattern, '[xyx],[abc].[cfd],[abc].[dgr],[abc]')
print(results)
Using re.findall:
import re
s = "[xyx],[abc].[cfd],[abc].[dgr],[abc]"
print('\n'.join(f'{i+1}.{x}' for i,x in
enumerate(re.findall(r'(\[[^]]+\])(?=,)', s))))
Output:
1.[xyx]
2.[cfd]
3.[dgr]

Taking a 8 bit number and split them into two 4 bit numbers

I have an integer integer = 10101001. I wanted to split that number into an array of 2 four bit numbers array = [1010,1001]. How do I do this? Are there any python methods?
This is a way to do it:
num = 10101001
str_num = str(num)
split_num = [int(str_num[0:4]), int(str_num[4:])]
print(split_num)
Output:
[1010, 1001]
You need to pass by the string version of you int
i = 10101001
str_i = str(i)
res = str_i[:len(str_i) // 2], str_i[len(str_i) // 2:]
print(res) # ('1010', '1001')
If that is indeed the general case, you can use a simple method.
def func(x):
x = str(x) # this takes x and turns it into a string
sub1 = int(x[:4]) # this takes the first 4 digits and turns it into an integer
sub2 = int(x[4:]) # this takes the last 4 digits and turns it into an integer
return [sub1, sub2]
Note that I used the fact that strins are subscriptable. You can fetch characters in a string just like a list.

Compare numbers in form: 12,3K , 1,84M in python

In need to compare numbers which look like: 12,3K , 1,84M, etc
eg:
a = 12,3K
b = 1,84M
if b > a :
print b
You need to use replace for it:
a = ("12,3K", "1,84M")
numbers = {"K": 1000, "M": 1000000}
result = []
for value in a:
if value:
i = value[-1]
value = float(value[:-1].replace(',', '.')) * numbers[i]
result.append(int(value))
print max(result)
You can add more numbers to dictionary and you will get more results.
I would recommend a function to convert a and b into the corresponding number like so (also I'd make a and b strings:
def convert(num):
return num.replace(',','').replace('K','000').replace('M','000000')
a = '12,3K'
b = '1,84M'
if convert(b) > convert(a) :
print b
If your values are strings, then the re module would make it easy to replace commas with '' and K or M with 3 or 6 zeroes. Then wrap in int() and compare. Where / how are you getting the values you're comparing?

Python: Change range location with loop

I would like my program to print every other letter in the string "welcome".
like:
e
c
m
Here is the code I have so far:
stringVar = "welcome"
countInt = 7
count = 0
oneVar = 1
twoVar = 2
showVar = stringVar[oneVar:twoVar]
for count in range(countInt):
count = count + 1
oneVar = oneVar + count
twoVar = twoVar + count
print(showVar)
Though it only shows the 2nd letter "e".
How can I get the variables oneVar and twoVar to update so that the range changes for the duration of the loop?
There is a built in notation for this, called "slicing":
>>> stringVar = "welcome"
>>> print(stringVar[::2])
wloe
>>> print(stringVar[1::2])
ecm
stringVar is iterable like a list, so the notation means [start : end : step]. Leaving any one of those blank implicitly assumes from [0 : len(stringVar) : 1]. For more detail, read the linked post.
Another more complex way of the doing the same would be
string_var = "welcome"
for index, character in enumerate(string_var, start=1): # 'enumerate' provides us with an index for the string and 'start' allows us to modify the starting index.
if index%2 == 0:
print character
Why its not working in your snipet:
Even though you increase oneVar and twoVar inside the loop, there is no change in the showVar as showVar is string which is immutable type, and its printing stringVar[1:2] which is e the 2nd index of welcome:
Just to fix your snippet:
You can just try like this;
stringVar = "welcome"
countInt = 7
for count in range(1,countInt,2):
print count, stringVar[count]
Output:
e
c
m

Python Differentiate mistake

I'm writing a program that reads in two proteins of the same (string) length and returns how many of the amino acid letters are different. I managed to write the some of the bits but unfortunately couldn't complete all of it so can any please guide through this by having a look on my code:
a = raw_input("Cheetah protein: ")
b = raw_input("Domestic cat protein: ")
u=zip(a,b)
d=dict(u)
x = 1
for i,j in d.items():
if i == j:
x = x + 1
print x
This the output I want to produce:
Cheetah protein: IGADKYFHARGNYDAA
Domestic cat protein: KGADKYFHARGNYEAA
2 difference(s).
I think you should describe better what you are trying to achieve. I don't understand this check:
if i == j:
If you want to check the differences, you should write instead:
if i != j:
After this fix your code gives me 3 differences for your example with cat and cheetah - are you sure the example is correct?
EDIT: OK, I see you're counting differences starting from one. Change the line
x = 1
to
x = 0
I don't think you want to assume that there is always at least one difference ;-)
a="IGADKYFHARGNYDAA"
b="KGADKYFHARGNYEAA"
u=zip(a,b)
x = 0 # not 1
for i,j in u: # you don't need a dict here
print i,j,
if i != j: # they differ iff they are not equal to each other
x = x + 1
print " neq"
else:
print " eq"
print x
func=lambda x,y: ((x!=y) and 1) or 0
print sum(map(func, a,b)), 'difference(s)'
Different proteins has different amino acids sequences ("letters"), but has also different lengths. No answers take care of this because you didn't asked for. Answering your question:
>>> a="IGADKYFHARGNYDAA"
>>> b="KGADKYFHARGNYEAA"
>>> sum(1 for x, y in zip(a,b) if x!=y)
2
I used "generator expressions" to generate a number one for each pair of different amino acids and summed up. Now if you want to spot the changed amino acids using a similar method:
>>> diff = ''.join('-' if x==y else y for x, y in zip(a,b))
>>> print 'A:', a, '\nB:', diff
A: IGADKYFHARGNYDAA
B: K------------E--

Categories

Resources