This question already has answers here:
Find string between two substrings [duplicate]
(20 answers)
Closed 2 years ago.
If you were to have a string of characters/numbers in a sequence - say AACBDEFZZBGFAAFFGGCCEEZZ
How could you iterate over the string, and then have every character between the AA and ZZ appear in a list? The end result in this case being:
[CBDEF], [FFGGCCEE] ?
Thanks
You can use str.index:
def between_markers(s, starting_marker="AA", ending_marker="ZZ"):
# keep track of where the previous marker was found
prev_ind = 0
ret = []
while True:
try:
# find the starting marker
start = s.index(starting_marker, prev_ind) + len(starting_marker)
# find the ending marker
end = s.index(ending_marker, start)
prev_ind = end + len(ending_marker)
# slice and append to return list
ret.append(s[start:end])
except ValueError:
# couldn't find one of the two markers so we're done
break
return ret
between_markers("AACBDEFZZBGFAAFFGGCCEEZZ") # ['CBDEF', 'FFGGCCEE']
Related
This question already has answers here:
How does python startswith work?
(2 answers)
Closed 5 months ago.
I'm learning how to manipulate strings in python. I'm currently having an issue using the "startswith()" function. I'm trying to see how many lines start with a specific character I.E "0" but I'm not getting any results. Where did I go wrong? The text file only contains random generated numbers.
random = open("output-onlinefiletools.txt","r")
r = random.read()
#print(len(r))
#small = r[60:79]
#print(r[60:79])
#print(len(r[60:79]))
#print(small)
for line in random:
line = line.rstrip()
if line.startswith(1):
print(line)
You are searching for 1 as an int, and I wouldn't use random as it is not protected but is generally used as part of the random lib; the lines are treated as strings once read thus you need to use startswith on a string and not an int.
myFile = open("C:\Dev\Docs\output-onlinefiletools.txt","r")
r = myFile.read()
# return all lines that start with 0
for line in r.splitlines():
if line.startswith("0"):
print(line)
Output:
00000
01123
0000
023478
startwith takes the prefix as argument, in your case it will be line.startswith("0")
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
Below is the prompt I am having trouble with. I have come up with a solution that pretty much gets the proper output, but with an extra return/new line that should be a space and not a new line. Can anyone see what could be causing the new line?
Write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest).
Ex: If the input is:
10 -7 4 39 -6 12 2
the output is:
2 4 10 12 39
For coding simplicity, follow every output value by a space. Do not end with new line.****
My Code:
#Get Input
user_input = input()
#Split input into individual entries within list
user_list = user_input.split()
map_object = map(int, user_list)
list_of_integers = list(map_object)
#Remove Negative Values from list
list_of_integers = [i for i in list_of_integers if i >= 0]
#Sort List In Ascending Order
list_of_integers.sort()
#Convert list of integers back into a string
list_of_integers = [str(int) for int in list_of_integers]
#Join string by a space
str_of_ints =" ".join(list_of_integers)
print(str_of_ints)
If you want to remove the newline from the print you can just do this
print(str_of_ints, end='') # end character is now empty not \n
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I need your help, can anyone tells me that what this part of code is doing?
if line[-6:] == '[edit]':
state = line[:-6]
and
town = line[:(line.index('(')-1)]
the whole code is :
def get_list_of_university_towns():
'''Returns a DataFrame of towns and the states they are in from the
university_towns.txt list. The format of the DataFrame should be:
DataFrame( [ ["Michigan", "Ann Arbor"], ["Michigan", "Yipsilanti"] ],
columns=["State", "RegionName"] )
The following cleaning needs to be done:
1. For "State", removing characters from "[" to the end.
2. For "RegionName", when applicable, removing every character from " (" to the end.
3. Depending on how you read the data, you may need to remove newline character '\n'. '''
with open('university_towns.txt') as file:
data = []
for line in file:
data.append(line[:-1])
# print(data)
state_town = []
for line in data:
if line[-6:] == '[edit]':
state = line[:-6]
# print(state)
elif '(' in line:
town = line[:(line.index('(')-1)]
# print(town)
state_town.append([state,town])
else:
town = line.rstrip()
state_town.append([state,town])
# print(state_town)
ans = pd.DataFrame(state_town, columns = ['State','RegionName'])
return ans
get_list_of_university_towns()
if line[-6:] == '[edit]':
state = line[:-6]
Is simply checking if last 6 character of given string are a [edit], if so then this value is assigned to a state var.
town = line[:(line.index('(')-1)]
Here the town var gets the value of a characters of a line that are in front of ( excluding.
EDIT:
Lets split this like:
index_to_par = line.index('(')
town = line[:(index_to_par - 1)]
So index_to_par is getting index on line string of a first (
Example if we have string like some string (example) then we get a value of 12 because ( is on a 12 place of this string.
Then we assing to a town a slice of this line as line[:(index_to_par - 1)] - we want to have values from begining of the string (as there is no value in front of :) to a our index_to_par - 1 (so all values before first occurence of (). You can read more about string sliceing here ---> https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I'm converting a set to a list, and then to a string in Python. I can remove the brackets in the string output, but I also want to remove the quotes around the string.
This is what I tried:
instance_list = ec2.describe_instances()
for reservation in instance_list["Reservations"]:
for instance in reservation.get("Instances", []):
tree = objectpath.Tree(instance)
private_ips = set(tree.execute('$..PrivateIpAddress'))
if len(private_ips) == 0:
private_ips = None
if private_ips:
private_ips_list = list(private_ips)
private_ips_list = str(private_ips_list).replace('[','').replace(']','').replace('\','')
else:
public_ips_list = None
This is the error I get:
File ".\aws_ec2_list_instances.py", line 64
private_ips_list = str(private_ips_list).replace('[','').replace(']','').replace('\','')
^
SyntaxError: EOL while scanning string literal
If I change the bottom line to this, without the final replace, the script works.
private_ips_list = str(private_ips_list).replace('[','').replace(']','')
But the quotes are still there:
Private IP: '10.48.136.41'
How can I remove the quotes from the output?
you can do:
a = set(["Blah", "Hello"])
str1 = ''.join(a)
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 7 years ago.
I am tying to find sha1sum for an .img file and the original device. Here's the method for doing that and the output i'm getting.
Code:
def hashcalc(self, file_path):
cmd1 = ["gksudo","sha1sum",file_path]
cmd2 = ["gksudo","sha1sum","/dev/mmcblk0"]
proc1 = subprocess.check_output(cmd1)
proc2 = subprocess.check_output(cmd2)
print proc1
print proc2
OUTPUT:
1ba1a6bbd66c335633d53d9bfff7366936e2e0e3 /home/user/Project/2gb.img
1ba1a6bbd66c335633d53d9bfff7366936e2e0e3 /dev/mmcblk0
Now how do I remove the path '/home/.../2gb.img' and '/dev/mmcblk0'. I want to compare those values. But normal '==' will not work as it contains the path as well. How do i remove that path. Please help.
Try using split and then compare:
proc1.split()[0] == proc2.split()[0]
string.split(" ") will split the the string by space and returns a list.
proc1.split(" ") will return ["1ba1a6bbd66c335633d53d9bfff7366936e2e0e3","/home/user/Project/2gb.img"]
You can get the first value of the list which will return the required value.
proc1.split(" ")[0] == "1ba1a6bbd66c335633d53d9bfff7366936e2e0e3"