I want to loop through a list of strings...
eg:
list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty']
I want to loop through the list items and search for the location of 'Hello!' within the string in each case an record its location.
Any ideas?
Use a list comprehension to collect the indexes of your wanted string:
>>> list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty', 'My Name is Josh']
>>> [s.find('Hello!') for s in list_of_strings]
[0, 0, 0, -1]
Note how the negative 1 shows that the final string does not contain the value you're looking for.
Use the string.find method.
>>> phrase = 'Hello, my name is Syko'
>>> phrase.find('Hello')
0
You could try list comprehensions
[(s, s.lower().index('hello')) for s in list_of_strings if s.lower() == 'hello']
>>> list_of_strings = ['Hello!, my name is Carl', 'Hello!, my name is Steve', 'Hello!, my name is Betty']
>>> indices = [position for position, phrase in enumerate(list_of_strings) if 'Hello!' in phrase]
>>> print indices
[0, 1, 2]
Using a comprehension list, loop over the list and check if the string has the Hello! string inside, if yes, append the position to the matches list.
Note: The enumerate build in brings you the index for each element in the list.
in case you are searching for multiple items in a list say Hello and name
find = lambda searchList, elem: [[i for i, x in enumerate(searchList) if x == e] for e in elem]
find(list_of_strings,['Hello','name'])
output is a list with the positions of the elements you are searching for in the order that you listed them
Related
I am trying to remove some words from string list of words.
list1= "abc dfc kmc jhh jkl".
My goal is to remove the words from 'dfc' to 'jhh'. I am new in Python, so I am trying some things with the index from c#, but they don't work here.
I am trying this:
index=0
for x in list1:
if x=='dfc'
currentindex=index
for y in list1[currentindex:]
if y!='jhh'
break;
del list1[currentindex]
currentindex=index
elif x=='jhh'
break;
Instead of a long for loop, a simple slice in Python does the trick:
words = ['abc', 'dfc', 'kmc', 'jhh', 'jkl']
del words[1:4]
print(words)
indexes start at 0. So you want to delete index 1-3. We enter 4 in the slice because Python stops -1 before the last index argument (so at index 3). Much easier than a loop.
Here is your output:
['abc', 'jkl']
>>> a = "abc dfc kmc jhh jkl"
>>> print(a.split("dfc")[0] + a.split("jhh")[1])
abc jkl
You can do this sample treatment with lambda:
b = lambda a,b,c : a.split(b)[0] + a.split(c)[1]
print(b(a, "dfc", "jhh"))
First, split the string into words:
list1 = "abc dfc kmc jhh jkl"
words = list1.split(" ")
Next, iterate through the words until you find a match:
start_match = "dfc"
start_index = 0
end_match = "jhh"
end_index = 0
for i in range(len(words)):
if words[i] == start_match:
start_index = i
if words[i] == end_match:
end_index = j
break
print ' '.join(words[:start_index]+words[end_index+1:])
Note: In the case of multiple matches, this will delete the least amount of words (choose the last start_match and first end_match).
list1= "abc dfc kmc jhh jkl".split() makes list1 as follows:
['abc', 'dfc', 'kmc', 'jhh', 'jkl']
Now if you want to remove a list element you can try either
list1.remove(item) #removes first occurrence of 'item' in list1
Or
list1.pop(index) #removes item at 'index' in list1
Create a list of words by splitting the string
list1= "abc dfc kmc jhh jkl".split()
Then iterate over the list, using a flag variable to indicate whether an element should be deleted from the list
flag = False
for x in list1:
if x=='dfc':
flag = True
if x == 'jhh':
list1.remove(x)
flag = False
if flag == True:
list1.remove(x)
There are several problems with what you have tried, especially:
list1 is a string, not a list
when you write list1[i], you get the character at index i (not a word)
in your for loop, you try to modify the string you iterate on: it is a very bad idea.
Here is my one-line style suggestion using re.sub(), which simply substitute a part of the string matching with the given regex pattern. It may be sufficient for your purpose:
import re
list1= "abc dfc kmc jhh jkl"
list1 = re.sub(r'dfc .* jhh ', "", list1)
print(list1)
Note: I kept the identifier list1 even if it is a string.
You can do like this
test = list1.replace("dfc", "")
Hello I'm new to this programming language
I wanted to add the word 'and' before the last item in my list.
For example:
myList = [1,2,3,4]
If I print it the output must be like:
1,2,3 and 4
Here is one way, but I have to convert the int's to strings to use join:
myList = [1,2,3,4]
smyList = [str(n) for n in myList[:-1]]
print(",".join(smyList), 'and', myList[-1])
gives:
1,2,3 and 4
The -1 index to the list gives the last (rightmost) element.
This may not be the most elegant solution, but this is how I would tackle it.
define a formatter function as follows:
def format_list(mylist)
str = ''
for i in range(len(mylist)-1):
str.append(str(mylist[i-1]) + ', ')
str.append('and ' + str(mylist[-1]))
return str
then call it like this
>>> x = [1,2,3,4]
>>> format_list(x)
1, 2, 3, and 4
You can also use string formating:
l = [1,2,3,4]
print("{} and {}".format(",".join(str(i) for i in l[:-1]), l[-1]))
#'1,2,3 and 4'
Using join (to join list elements) and map(str,myList) to convert all integers inside list to strings
','.join(map(str,myList[:-1])) + ' and ' + str(myList[-1])
#'1,2,3 and 4'
Your question is misleading, if you are saying "How to add a word before the last word in list?" it means you want to add 'and' string before last item in the list , while many people are giving answer using .format() method , You should specify you want 'and' for printing or in list for further use of that result :
Here is list method according to your question :
myList = [1,2,3,4]
print(list((lambda x,y:(x+['and']+y))(myList[:-1],myList[-1:])))
output:
[1, 2, 3, 'and', 4]
I have several strings like this:
mylist = ['pearsapple','grapevinesapple','sinkandapple'...]
I want to parse the parts before apple and then append to a new list:
new = ['pears','grapevines','sinkand']
Is there a way other than finding starting points of 'apple' in each string and then appending before the starting point?
By using slicing in combination with the index method of strings.
>>> [x[:x.index('apple')] for x in mylist]
['pears', 'grapevines', 'sinkand']
You could also use a regular expression
>>> import re
>>> [re.match('(.*?)apple', x).group(1) for x in mylist]
['pears', 'grapevines', 'sinkand']
I don't see why though.
I hope the word apple will be fix (fixed length word), then we can use:
second_list = [item[:-5] for item in mylist]
If some elements in the list don't contain 'apple' at the end of the string, this regex leaves the string untouched:
>>> import re
>>> mylist = ['pearsapple','grapevinesapple','sinkandapple', 'test', 'grappled']
>>> [re.sub('apple$', '', word) for word in mylist]
['pears', 'grapevines', 'sinkand', 'test', 'grappled']
By also using string split and list comprehension
new = [x.split('apple')[0] for x in mylist]
['pears', 'grapevines', 'sinkand']
One way to do it would be to iterate through every string in the list and then use the split() string function.
for word in mylist:
word = word.split("apple")[0]
Suppose, I have a list like the following:
names= ["my name xyz","your name is abc","her name is cfg zyx"]
I want to split them like following:
my_array[0]= ["my","name","xyz"]
my_arrray[1]= ["your","name","is","abc"]
my_array[2]= ["her","name","is","cfg","zyx"]
So that I can choose the words individually like following:
my_array[0][0]= my
my_array[0][1]= name
my_array[1][2]= is
my_array[0][2]= xyz
The python code i tried is following:
names_abc= names.splitlines(True)
#print(names_abc)
i=0
while i < len(names_abc):
my_array= names_abc[i].split()
print(my_array[0])
i=i+1
But this code doesn't do what I expect. It splits the list but cannot choose the words individually. For example, the output of the above code is following:
my
your
her
which is definitely wrong. I will be grateful to get a help from the python experts. Thanks in advance.
You could define my_array using a list comprehension:
my_array = [phrase.split() for phrase in names]
which is equivalent to
my_array = list()
for phrase in names:
my_array.append(phrase.split())
Demo:
In [21]: names= ["my name xyz","your name is abc","her name is cfg zyx"]
In [22]: my_array = [phrase.split() for phrase in names]
In [23]: my_array
Out[23]:
[['my', 'name', 'xyz'],
['your', 'name', 'is', 'abc'],
['her', 'name', 'is', 'cfg', 'zyx']]
In [24]: my_array[0][1]
Out[24]: 'name'
One problem with defining my_array this way:
while i < len(names_abc):
my_array= names_abc[i].split()
is that my_array is assigned to a new list each time through the while-loop. So my_array does not become a list of lists. Instead, after the while-loop, it only retains its last value.
Instead of printing my_array[0], try print(my_array) itself. It might become more clear why you're getting the result you were getting.
You could split() every element in your list, which by default will split on spaces.
>>> names= ["my name xyz","your name is abc","her name is cfg zyx"]
>>> [i.split() for i in names]
[['my', 'name', 'xyz'], ['your', 'name', 'is', 'abc'], ['her', 'name', 'is', 'cfg', 'zyx']]
I would create a new list and fill it with lists containing temporaray strings build from the characters of your starting list.
The temporary string is ended by the space, when a string is complete it is added to a temoprary list (list_tmp).
After all items are processed as strings and added to the temporary list, we append that list the final list:
a=["my name xyz","your name is abc","her name is cfg zyx"]
l=[]
for i in enumerate(a):
tmp_str = ''
list_tmp=[]
for j in i[1]:
if " " in j:
list_tmp.append(tmp_str)
tmp_str=''
else:
tmp_str+=j
else:
list_tmp.append(tmp_str)
l.append(list_tmp)
print l
print l[0][1]
I am able to detect matches but unable to locate where are they.
Given the following list:
['A second goldfish is nice and all', 3456, 'test nice']
I need to search for match (i.e. "nice") and print all the list elements that contain it. Ideally if the keyword to search were "nice" the results should be:
'A second goldfish is nice and all'
'test nice'
I have:
list = data_array
string = str(raw_input("Search keyword: "))
print string
if any(string in s for s in list):
print "Yes"
So it finds the match and prints both, the keyword and "Yes" but it doesn't tell me where it is.
Should I iterate through every index in list and for each iteration search "string in s" or there is an easier way to do this?
Try this:
list = data_array
string = str(raw_input("Search keyword: "))
print string
for s in list:
if string in str(s):
print 'Yes'
print list.index(s)
Editted to working example. If you only want the first matching index you can also break after the if statement evaluates true
matches = [s for s in my_list if my_string in str(s)]
or
matches = filter(lambda s: my_string in str(s), my_list)
Note that 'nice' in 3456 will raise a TypeError, which is why I used str() on the list elements. Whether that's appropriate depends on if you want to consider '45' to be in 3456 or not.
print filter(lambda s: k in str(s), l)
To print all the elements that contains nice
mylist = ['nice1', 'def456', 'ghi789', 'nice2', 'nice3']
sub = 'nice'
print("\n".join([e for e in mylist if sub in e]))
>>> nice1
nice2
nice3
To get the index of elements that contain nice (irrespective of the letter case)
mylist = ['nice1', 'def456', 'ghi789', 'Nice2', 'NicE3']
sub = 'nice'
index_list = []
i = 0
for e in mylist:
if sub in e.lower():
index_list.append(i)
i +=1
print(index_list)
>>> [0, 3, 4]