How to find a index of an item in a string? - python

But I need the index of the second time appearance.
It is like I have a string "asd#1-2#qwe"
I can simply use index method to find the index value of first #, which is 3.
But now I wanna get the index of second #, which should be 7.

Use enumerate and a list comprehension:
>>> s = "asd#1-2#qwe"
>>> [i for i, c in enumerate(s) if c=='#']
[3, 7]
Or if string contains just two '#' then use str.rfind:
>>> s.rfind('#')
7
Using regex: This will work for overlapping sub-strings as well:
>>> s = "asd##1-2####qwe"
>>> import re
#Find index of all '##' in s
>>> [m.start() for m in re.finditer(r'(?=##)', s)]
[3, 8, 9, 10]

use this:
s = "asd#1-2#qwe"
try:
s.index('#',s.index('#')+1)
except:
print "not found"

Use the index method to get the first occurrence of #. If the index method allows a starting position, use the position of the first # + 1 for the start. If it doesn't, make a copy of the string starting at position of first # + 1 (possibly a copy and then a substring).

a = "asd#1-2#qwe"
f = '#'
idx = []
for i, v in enumerate(a):
if v == f:
idx.append(i)
print idx

Related

how to I return the index of multiple characters in a string when running a for loop?

I have the following code:
dup = ["aaa","bbb","ccc"]
s = "hfidgfaaahjfihdfhd"
for duplicate in dup:
if duplicate in s:
print(s.index(duplicate))
The output is 6; which is the index of the first character of "aaa" in the string s, however, I would like it to print all three of its indexes, i.e. 6,7,8.
Is there anyway I can tweak the code to achieve this?
Probably, the easiest tweak you could do is: instead of printing just the index of the duplicate, print all the numbers starting from the index of the duplicate up to index of the duplicate + length of the duplicate, like this:
dup = ["aaa","bbb","ccc"]
s = "hfidgfaaahjfihdfhd"
for duplicate in dup:
if duplicate in s:
print(*list(range(s.index(duplicate), s.index(duplicate) + len(duplicate))))
Here, I am using the list(range()) function to generate a list of all the numbers within the range s.index(duplicate) up to (but not included!) s.index(duplicate) + len(duplicate).
The asterisk at the beginning is used to unpack the list and print each value individually instead of printing it as a list: without the asterisk, you would get [6, 7, 8]. With the asterisk, you should get exactly 6 7 8.
An easier alternative, but with the exact same logic, would be this:
dup = ["aaa","bbb","ccc"]
s = "hfidgfaaahjfihdfhd"
for duplicate in dup:
if duplicate in s:
for index in range(s.index(duplicate), s.index(duplicate) + len(duplicate)):
print(index)
You can avoid the *list(range()) using a regular for loop with the range function
Well, you can do it by this way:
dup = ['aaa', 'bbb', 'ccc']
s = "hhdahwhdaaadfewfeas"
for i in range(0, len(s), len(dup[0]) - 1):
string_to_check = s[i:3+i:]
if dup[0] == string_to_check:
for j in range(0, len(string_to_check)):
print(f'Index for the letters: {j + i}')
I would suggest using regular expressions (re) for this (in particular Match.span()):
import re
dup = ["aaa", "bbb", "ccc"]
s = "hfidgfaaahjfihdfhd"
for duplicate in dup:
# finditer returns an iterator over all found matches
matches = re.finditer(duplicate, s)
for match in matches:
# The span() method returns a tuple containing
# the start and end position of the match.
# These can be passed into the built-in range() function
# to generate the indices you are interested in:
print(f"Match for '{duplicate}':", *range(*match.span()))
Will print:
Match for 'aaa': 6 7 8
If you now changed s to "hfidgfaaahjfihdfhdaaabbb", the result would be:
Match for 'aaa': 6 7 8
Match for 'aaa': 18 19 20
Match for 'bbb': 21 22 23

Cut character string every two commas

I would like to separate my string every both commas but I can not, can you help me.
This is what I want: ['nb1,nb2','nb3,nb4','nb5,nb6']
Here is what I did :
a= 'nb1,nb2,nb3,nb4,nb5,nb6'
compteur=0
for i in a:
if i==',' :
compteur+=1
if compteur%2==0:
print compteur
test = a.split(',', compteur%2==0 )
print a
print test
The result:
2
4
nb1,nb2,nb3,nb4,nb5,nb6
['nb1', 'nb2,nb3,nb4,nb5,nb6']
Thanks you by advances for you answers
You can use regex
In [12]: re.findall(r'([\w]+,[\w]+)', 'nb1,nb2,nb3,nb4,nb5,nb6')
Out[12]: ['nb1,nb2', 'nb3,nb4', 'nb5,nb6']
A quick fix could be to simply first separate the elements by commas and then join the elements by two together again. Like:
sub_result = a.split(',')
result = [','.join(sub_result[i:i+2]) for i in range(0,len(sub_result),2)]
This gives:
>>> result
['nb1,nb2', 'nb3,nb4', 'nb5,nb6']
This will also work if the number of elements is odd. For example:
>>> a = 'nb1,nb2,nb3,nb4,nb5,nb6,nb7'
>>> sub_result = a.split(',')
>>> result = [','.join(sub_result[i:i+2]) for i in range(0,len(sub_result),2)]
>>> result
['nb1,nb2', 'nb3,nb4', 'nb5,nb6', 'nb7']
You use a zip operation of the list with itself to create pairs:
a = 'nb1,nb2,nb3,nb4,nb5,nb6'
parts = a.split(',')
# parts = ['nb1', 'nb2', 'nb3', 'nb4', 'nb5', 'nb6']
pairs = list(zip(parts, parts[1:]))
# pairs = [('nb1', 'nb2'), ('nb2', 'nb3'), ('nb3', 'nb4'), ('nb4', 'nb5'), ('nb5', 'nb6')]
Now you can simply join every other pair again for your output:
list(map(','.join, pairs[::2]))
# ['nb1,nb2', 'nb3,nb4', 'nb5,nb6']
Split the string by comma first, then apply the common idiom to partition an interable into sub-sequences of length n (where n is 2 in your case) with zip.
>>> s = 'nb1,nb2,nb3,nb4,nb5,nb6'
>>> [','.join(x) for x in zip(*[iter(s.split(','))]*2)]
['nb1,nb2', 'nb3,nb4', 'nb5,nb6']

How to return the position of numbers, which are same as input numbers, in a list in python?

I have a list such as:
[1,2,3,2,1,5,6]
I want to find the all the positions of 1 in the list. I tried if statement, but I only get the position of first 1, not all 1s.
The real output if I use if statement looks like [0], but the expected result should be [0,4].
You can use a list comprehension iterating over enumerate(your_list) and using an if statement as part of it to catch only the wanted values, as below:
data = [1,2,3,2,1,5,6] # Your list
num = 1 # The value you want to find indices for
pos = [i for i, v in enumerate(data) if v == num]
print(pos)
# [0, 4]
Try using enumerate:-> this give a tuple with its index and value from the list
lndexs=[]
for index,value in enumerate([1,2,3,2,1,5,6]):
if value==1:
lndexs.append( index)
lstOfNumbers = [1,2,3,2,1,5,6]
searchFor = 1
lstOfIndexs = [index for index in range(len(lstOfNumbers)) if lstOfNumber[index]==searchFor]
Or you can use the python function filter
lstOfNumbers = [1,2,3,2,1,5,6]
searchFor = 1
lstOfIndexs = filter(lambda x: lstOfNumbers[x]==searchFor, range(len(lstOfNumbers)))

How to get string list index?

I have a list text_lines = ['asdf','kibje','ABC','beea'] and I need to find an index where string ABCappears.
ABC = [s for s in text_lines if "ABC" in s]
ABC is now "ABC".
How to get index?
Greedy (raises exception if not found):
index = next(i for i, s in enumerate(text_lines) if "ABC" in s)
Or, collect all of them:
indices = [i for i, s in enumerate(text_lines) if "ABC" in s]
text_lines = ['asdf','kibje','ABC','beea']
abc_index = text_lines.index('ABC')
if 'ABC' appears only once. the above code works, because index gives the index of first occurrence.
for multiple occurrences you can check wim's answer
Simple python list function.
index = text_lines.index("ABC")
If the string is more complicated, you may need to combine with regex, but for a perfect match this simple solution is best.
Did you mean the index of "ABC" ?
If there is just one "ABC", you can use built-in index() method of list:
text_lines.index("ABC")
Else, if there are more than one "ABC"s, you can use enumerate over the list:
indices = [idx for idx,val in enumerate(text_lines) if val == "ABC"]

Append two integers to list when seperated by '..' Python

If i have a list strings:
first = []
last = []
my_list = [' abc 1..23',' bcd 34..405','cda 407..4032']
how would i append the numbers flanking the .. to their corresponding lists ? to get:
first = [1,34,407]
last = [23,405,4032]
i wouldn't mind strings either because i can convert to int later
first = ['1','34','407']
last = ['23','405','4032']
Use re.search to match the numbers between .. and store them in two different groups:
import re
first = []
last = []
for s in my_list:
match = re.search(r'(\d+)\.\.(\d+)', s)
first.append(match.group(1))
last.append(match.group(2))
DEMO.
I'd use a regular expression:
import re
num_range = re.compile(r'(\d+)\.\.(\d+)')
first = []
last = []
my_list = [' abc 1..23',' bcd 34..405','cda 407..4032']
for entry in my_list:
match = num_range.search(entry)
if match is not None:
f, l = match.groups()
first.append(int(f))
last.append(int(l))
This outputs integers:
>>> first
[1, 34, 407]
>>> last
[23, 405, 4032]
One more solution.
for string in my_list:
numbers = string.split(" ")[-1]
first_num, last_num = numbers.split("..")
first.append(first_num)
last.append(last_num)
It will throw a ValueError if there is a string with no spaces in my_list or there is no ".." after the last space in some of the strings (or there is more than one ".." after the last space of the string).
In fact, this is a good thing if you want to be sure that values were really obtained from all the strings, and all of them were placed after the last space. You can even add a try…catch block to do something in case the string it tries to process is in an unexpected format.
first=[(i.split()[1]).split("..")[0] for i in my_list]
second=[(i.split()[1]).split("..")[1] for i in my_list]

Categories

Resources