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 6 years ago.
Improve this question
Say i have a list formatted something like:a = [a,2,b,3,c,4,d,3]
and i want to write to any file that allows to create superscripts, like:
a^2
b^3
c^4
and so forth. What possible ways can this be done (The indices need to be formatted properly, like actual indices)?
As simple as this:
files=open('write.txt','a')
a = ['a','2','b','3','c','4','d','3']
count=0
while count<len(a):
files.write(a[count]+'^'+a[count+1]+'\n')
count=count+2
Here is a simple way to accomplish that. Replace the print statement with your write and you'll be in good shape.
First prep your list by dividing it into 2 pieces:
a = ['a',2,'b',3,'c',4,'d',3]
first = a[0::2]
second = a[1::2]
Next, loop the first list with enumeration and add the second value:
for i, f in enumerate(first):
super = '%s^%s' % (f, second[i])
print(super) # replace with write function
Output looks like this:
a^2
b^3
c^4
d^3
This should keep it simple!
It's basically just opening a file and then joining successive elements with a ^ and then joining all of these with a line-break. Finally this is written to a file and the file is closed:
with open('filename.txt', 'w') as file:
it = iter(a)
file.write('\n'.join('^'.join([first, str(second)]) for first, second in zip(it, it)))
If you don't want to use any joins and comprehensions you can also use formatting:
with open('filename.txt', 'w') as file:
template = '{}^{}\n' * (len(a) // 2)
formatted = template.format(*a)
file.write(formatted)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
they need to order with a problem
so I have the text in a certain order of numbers, something like gematria
input [12345] is what we call gematria and what do they need?
they need to line up the digits backwards
[54321]
have a different count and I would need help with that rather than doing twenty different if
def shiftall(s, n):
n %= len(s)
return s[n:] + s[:n]
it didn't help me much it only moves the simple text
For strings:
return s[::-1]
For integers:
return str(s)[::-1]
Note: This would go inside def shiftall(s, n):
Additional note: Now you don't even need the parameter n
If you want to reverse a number, then you can convert it to a string, reverse the string, and then convert it back to a number.
num = 12345
str_num = str(num)
# reverse and convert
num = int(str_num[::-1])
input=[12345,43545436,88888,843546]
def shiftall(s):
d=[]
for i in s:
res=''.join(reversed(str(i)))
d.append(int(res))
return d
print(shiftall(input))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
need to convert this list :
a = ["['0221', '02194', '02211']"]
type = list
to this list :
a = ['0221', '02194', '02211']
type = list
If your new to python this code would seem like very complicated, but i will explain whats in this piece of code:
a=["['0221', '02194', '02211']"]
a1=[]
nums_str=""
for i in a:
for j in i:
try:
if j=="," or j=="]":
a1.append(nums_str)
nums_str=""
nums=int(j)
nums_str+=str(nums)
except Exception as e:
pass
else:
a=a1.copy()
print(a)
print(type(a))
Steps:
Used for loop to read the content in list a.
Then again used a for loop to read each character in the string of i.
Then used try to try if i can typecast j into int so that it would only add the numbers to nums_str.
Then appended the nums_str to the list a1 if j if = "," or "]".
Continued the above process on each item in a.
After the for loop gets over, i change a to copy of a1.
You can use astliteral_eval to convert strings to Python data structures. In this case, we want to convert the first element in the list a to a list.
import ast
a = ast.literal_eval(a[0])
print(a)
# ['0221', '02194', '02211']
Note: Python built-in function eval also works but it's considered unsafe on arbitray strings. With eval:
a = eval(a[0]) # same desired output
You can try list comprehension:
a = a[0][2:][:-2].split("', '")
output:
a = ['0221', '02194', '02211']
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 4 years ago.
Improve this question
My dataset looks like following. I am trying to read numbers in "per" column without reading "%" symbol.Being a beginner in python,I was wondering if we can do such in python. Also, if you could provide the explanation that will be great!
State Year per
A 1990 6.10%
A 1989 4.50%
B 1990 3.4%
B 1989 1.25%
Thanks in advance,
In case it is a csv file, this should help (or there might be another way to get a dataframe):
import pandas as pd
data = pd.read_csv("somefile.csv")
data["per"] = data["per"].str.replace("%", "").to_numeric()
Your file type doesn't matter for this and no modules required. It works by taking each row and going to the last word. Then it splits the percentage and removes the percent symbol.
def readFile(filename):
percents = []
with open (filename,"r") as f:
for row in f:#for each line, we remove the first one late
splitRow = row.split()[-1]# spliting the elements by word, we want the last one only
percent = splitRow
percent = percent.split("%")[0]#removing the percent
percents.append(percent)#if you want it as an number instead of a string do percents.append(float(percent))
percents = percents[1:] # removes the header "per"
return percents
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to to extract some information from a data file. The following is the format I have in my data file:
44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)
From this line, I want to extract 256 which is the last number and 4 which is the first number from
(4,0x00002aab 4c000c00)
Could you please recommend some functions which will be useful for my case?
You should use str.split().
What it does is split the string every place there is a space, so you would get a list of strings like so:
n = '44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)'
o = n.split()
print o
Output:
['44', '2.463181s>', '(G)', 'GET_NBI:', '0x00002aaa', 'ecc00e90', '<-', '(4,0x00002aab', '4c000c00)', '(256', 'bytes)']
Then simply get the second-to-last index like o[-2] -> '(256'
Remove the extra parenthesis: '(256'[1:] -> '256', and If you wanna, turn it into an integer. int('256') -> 256
You could also use regular expressions, which in this case might be a bit more clear.
import re
txt = "44 2.463181s> (G) GET_NBI: 0x00002aaa ecc00e90 <- (4,0x00002aab 4c000c00) (256 bytes)"
results = re.findall(r"\((\d+)", txt)
# ["4", "256"]
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 8 years ago.
Improve this question
I have a group of strings which look like this:
M.HpyFIX.dna|GTNAAC
M1.HpyFXIII.dna|CCATC
M.HpyFI.dna|CAGT
M2.HpyFXIII.dna|CCATC
M.HpyFVI.dna|TGCA
M.HpyFVIII.dna|TCNNGA
M.HpyFORFX.dna|CCNNGG
M.HpyFII.dna|TCGA
M.HpyFVII.dna|ATTAAT
M.HpyFXII.dna|GTCA
M.HpyFV.dna|CCGG
M.HpyFXI.dna|CTNAG
M.HpyFIII.dna|GATC
M.HpyFIV.dna|GANTC
I wanna compare them only based on the string after the | (pipe). I dont want to use string.strip('|'). In the above case i would like to get each string one by one and apply the functions I have except for M1.HpyFXIII.dna|CCATC and M2.HpyFXIII.dna|CCATC which i would like to get into in a temporary list and then apply apply the functions.
The reason I want to use string comparisons is that I am using ETE to build phylogenetic trees and its much simpler with string comparisons
If not s.split('|')[1] to get the part of the string after the |, then perhaps
s[s.index('|')+1:]
Which grabs the substring from all characters past the | to the end of the string.
I wouldn't call using split as above a "massive headache", however, and it's arguably easier to read.
To transform the entire list, you can create a function that does what you want it to do, then use a list comprehension or map.
You could use the split() method, and then take the second string in the returned list.
_junk, myString = 'M.HpyFIX.dna|GTNAAC'.split('|')
Or if you don't want to store it in a string:
'M.HpyFIX.dna|GTNAAC'.split('|')[1]
Treat as csv file with custom delimiter
>>> import csv
>>> import collections
>>> with open('in.txt') as in_file:
... reader = csv.reader(in_file, delimiter='|')
... data = list(reader) #exhaust generator, convert it to list
... #now you have loaded your data in two-dimensional array, lets find dups
... dup_values = [x for x, y in collections.Counter([r[1] for r in data]).items() if y > 1]
... for r in data:
... if r[1] in dup_values:
... print r
...
['M1.HpyFXIII.dna', 'CCATC']
['M2.HpyFXIII.dna', 'CCATC']
Other option is str.partition:
x = "M.HpyFIX.dna|GTNAAC"
object, _, sequence = x.partition("|")
print(sequence)
# or grab the third element
print(x.partition("|")[1])
ls = ['M.HpyFIX.dna|GTNAAC', 'M1.HpyFXIII.dna|CCATC', 'M.HpyFVII.dna|ATTAAT']
nls = [ l.split('|')[1] for l in ls ]