I was wondering how would you find the index of a element in a list if you only had part of it. For example
list = ["this", "is", "an", "example", "for", "python"]
how would you find it if you only had "pyt" and needed the index for python???
Straightforward for loop:
def find_starts(val, my_list):
for i, v in my_list:
if v.startswith(val):
return i
(If there is no match then this returns None.) This could be made neater with a list comprehension: see this related question for more details.
As the question is not 100% clear i assume you want to find all items that include the specified string.
alist = ["this", "is", "an", "example", "for", "python"]
val = 'is'
def find_in_list(alist, val):
res = []
for e, v in enumerate(alist):
if val in v:
res.append(e)
return res
find_in_list(alist, val)
You have to work with a solution that work when some element are similar (for example ["foo", "asd", "foobar"] and you have "foo" what would be detected as a correct element?)
The given function will return a list of indexes, which were identified as a correct element.
def find(foo, list):
#Find similar elements
similarElements = [e for e in list if foo in e]
#Find their indexes
indexes = [similarElements.index(i) for i in similarElements]
return indexes
This solution isn't the fastest way, but an easy and understandable.
>>> list = ["this", "is", "an", "example", "for", "python"]
>>> word = "python"
>>> y = [i for i,j in enumerate(list) if j == word]
>>> y
[5]
However, this is for the whole word, try this aswell.
>>> word = "py"
>>> for w in list:
... if word in w:
... sus.append(w)
... print(sus)
...
['python']
Related
For example, let's say you have the list t = ["hi", "bye"]
If I wanted to grab the index of bye, I would just do t.index("bye") and that would get me 1
But let's say I have t = ["hi", ["good", "bye"]], t.index("bye") will no longer work.
Is there some way to grab the index of an element in a nested list, such that nested lists can be easily indexed.
I.e. for t = ["hi", ["good", "bye"]]
t.index("bye") # Outputs [1][1] or something similar
I should mention that for my purposes, there will be no duplicate elements in any of my lists or sublists so there can only be one output.
You can use recursion to solve this:
def find(lst, x):
for i, l in enumerate(lst):
if isinstance(l, (list, tuple)):
f = find(l, x)
if f:
return (i,) + f
elif l == x:
return (i,)
return False
a = ["hi", "bye"]
b = ["hi", ["good", "bye"]]
print(find(a, 'bye')) # (1,)
print(find(b, 'bye')) # (1, 1)
print(find(b, 'good')) # (1, 0)
[Edit suggested by Achxy_]
in my list I want the function to return only the maximum values within the list
my_list = ["and", "the", "plus", "from", "i" , "09"]
How can I get the output to be the maximum values ("plus", "from")
I did something like that, and I could not continue ...
my_list = ["and", "the", "plus", "from", "i" , "09"]
list1 = str(my_list)
list2 = list1.split(',')
for i in list2:
if len(i) == max(len(i)):
print(i)
Thanks for the help
def myFunction(words: list):
output = []
for word in sorted(words, key=len, reverse=True):
maxLen = len(sorted(words, key=len, reverse=True)[0])
if(len(word) < maxLen):
break
output.append(word)
return {
"words": output,
"length": maxLen
}
It takes the words list and sorts it from greatest to least length, then sets the maxLen variable to the length of the first thing in that list. Then, it looks to see if the length of the word the for loop is less than maxLen, and if it is, it stops the loop.
If you would like to get the lenght of longest string in list, you can use the shorhand version foor lop like max([len(item) for item in my_list]). but as far as I understood you would like to get the the string even if there are strings with same length you can try this:
def longest_items(my_list):
max_len = 0
items = []
for item in my_list:
if len(item) > max_len:
max_len = len(item)
items.clear()
items.append(item)
elif len(item) == max_len:
items.append(item)
return tuple(items)
This function will return longest string or strings in a tuple.
You can quickly use some list comprehension:
>>> my_list = ["and", "the", "plus", "from", "i" , "09"]
>>> lengths = [len(s) for s in my_list]
>>> max_lengths = [s for s in my_list if len(s) == max(lengths)]
>>> max_lengths
["plus", "from"]
The list in lengths stores the length of each string in my_list. Then, by using the max() function, we can quickly obtaining the maximum value in lengths, which corresponds to the maximum length of a string. Finally, we re-iterate over my_list and compare each string value to max(lengths), checking if each string is the maximum length.
Note that, if desired, this all can be condensed into one list comprehension:
>>> max_lengths = [i for i in my_list if len(i) == max([len(s) for s in my_list])]
>>> max_lengths
["plus", "from"]
my_list = ["and", "the", "plus", "from", "i", "09"]
length = 0
arr = []
for i in my_list:
if len(i) > length:
length = len(i)
arr = []
if len(i) == length:
arr.append(i)
length = len(i)
print(arr)
list2 = list1.split(',')
split is a string function, it turns a string into a list. Like "john, tim, and paul" would turn into ["john", " tim", " and paul"]
max takes an iterator and returns the highest value. you did max(len(i)) ... len(i) is just going to return an integer, so you did max(6). Instead, you want to make a list of the lengths. max([len(x) for x in list1]) would give you the length of the longest item in list1.
I'm trying to remove all instances of words from a list. I searched and pretty much all the answers are similar to my code below but I can't get it to work. The list simply returns the same list without removing any words. For the print function at the very end if I enter a word like "and" manually instead of the parameter it works so I assume it has something to do with the "remove_words" list.
I'm using vscode and imported a text file with a few paragraphs in them.
myFile = open("mytext.txt")
remove_words = ["and", "a", "to", "the", "if", "is", "it", "of"]
mylist = myFile.read().lower()
newlist = mylist.split(" ")
def remove_items(thelist, item):
final_list = [i for i in thelist if i != item]
return final_list
print(remove_items(newlist, remove_words))
item is a list so just replace the comprehension code as below.
def remove_items(thelist, item):
final_list = [i for i in thelist if not i in item]
return final_list
You are passing remove_words as the item parameter, meaning that you are passing a list and not a word
Your issue is that remove_words is a list of strings, while remove_items expects one string. If you want to not have to change remove_items, you would have to pass each item in remove_words to it separately:
for word in remove_words:
newlist = remove_items(newlist, word)
print(newlist)
As others pointed out and given examples of, though, if you changed remove_items, you could have it accept a list of words to remove, thus simplifying your code.
instead of using "!=" try "not in". Hope that works.
As other answers mention, you pass the list of items rather than a single item to be removed. Try this instead:
def remove_items(thelist, items):
final_list = [i for i in thelist if i not in items]
return final_list
It is best to use with open when handling files.
Use filter to filter out the unwanted words:
remove_words = ["and", "a", "to", "the", "if", "is", "it", "of"]
with open("mytext.txt") as myFile:
mylist = myFile.read().lower()
newlist = mylist.split(" ")
def remove_items(thelist, item):
return list(filter(lambda a: a not in item, thelist))
print(remove_items(newlist, remove_words))
I have a list with multiple strings
e.g.:
list = ["This", "is", "a", "test"]
Now I want to create a function that "cuts out" the first and the last char of each string in the given list.
So for this certain example the output of the function should be:
"Tsisaatt"
I have already experimented with list[0][1], but this code:
newList = []
newList = newList.append(list[0][0])
newList = newList.append(satz[0][1])
newList = newList.append(satz[0][2])
...
...
did't do what I want. Did I miss anything or is my idea completely wrong?
Thanks in advance for your feedback!
test.py
list = ["This", "is", "a", "test"]
print("".join([l[0] + l[-1] for l in list]))
Step 1: Just fetch first item with l[0], plus it with last item with l[-1]. It will be ['Ts', 'is', 'aa', 'tt'].
Step 2: Change the new list to string with "".join().
Execution result:
$ python test.py
Tsisaatt
you can do:
newlist.append(mylist[0][0])
newlist.append(mylist[0][-1])
you can write these two lines inside for loop and at last you can join the result
Use list comprehensions
"".join(i[0]+i[-1] for i in list)
You can do it using list comprehension -
output_str = "".join(["".join([x[0], x[-1]]) for x in input_list])
the inner ''.join will join a local list of x[0] and x[-1] and the outer ''.join will join all such list of strings.
You can acces first element of a string using [0] and last element using [-1]
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
print(output)
You can use this function
def cut_first_and_last(text_list):
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
return output
or for one-liner
def cut_first_and_last(text_list):
return "".join([text[0]+text[-1] for text in text_list])
If you aren't familiar with list comprehension you can use a simple for loop like this:
mylist = ["this", "is", "a", "list"]
output_string = ""
for item in word_list:
output_string.join(item[0], item[1])
print(output_string)
If you are familiar with list comprehensions just use this one liner:
mylist = ["this", "is", "a", "list"]
print("".join(["".join(item[0],item[1]) for item in mylist])
This question already has answers here:
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Closed 5 months ago.
I'm writing a small NLP algorithm and I need to do the following:
For every string x in the list ["this", "this", "and", "that"], if the string x and the next string are identical, I want to print the string.
s = ["this", "this", "and", "that"]
for i in xrange(1,len(s)):
if s[i] == s[i-1]:
print s[i]
EDIT:
Just as a side note, if you are using python 3.X use range instead of xrange
strings = ['this', 'this', 'and', 'that']
for a, b in zip(strings, strings[1:]):
if a == b:
print a
Sometimes, I like to stick with old-fashioned loops:
strings = ['this', 'this', 'and', 'that']
for i in range(0, len(strings)-1):
if strings[i] == strings[i+1]:
print strings[i]
Everyone knows what's going on without much thinking, and it's fairly efficient...
Most Pythonic is a list comprehension, which is exactly built for looping and testing at the same time:
>>> strings = ['this', 'this', 'and', 'that']
>>> [a for (a,b) in zip(strings, strings[1:]) if a==b]
['this']
Or, to avoid temporary objects (h/t #9000):
>>> import itertools as it
>>> [a for (a,b) in it.izip(strings, it.islice(strings,1)) if a==b]
['this']
TEST = ["this", "this", "and", "that"]
for i, s in enumerate(TEST):
if i > 0 and TEST[i-1] == s:
print s
# Prints "this"
why not simply ? :
strings = ['this', 'this', 'and', 'that', 'or', 'or', 12,15,15,15, 'end']
a = strings[0]
for x in strings:
if x==a:
print x
else:
a = x
Is that homework?
l = ["this", "this", "and", "that", "foo", "bar", "bar", "baz"]
for i in xrange(len(l)-1):
try:
if l.index(l[i], i+1) == i+1:
print l[i]
except ValueError:
pass
Generally speaking, if you're processing over items in a list and you need to look at the current item's neighbors, you're going to want to use enumerate, since enumerate gives you both the current item and its position in the list.
Unlike the approaches that use zip, this list comprehension requires no duplication of the list:
print [s for i, s in enumerate(test[:-1]) if s == test[i + 1]]
Note that it fails if there aren't at least two elements in test, and that test must be a list. (The zip approaches will work on any iterable.)
Here's a little different approach that uses a special class to detect repeats in a sequence. Then you can actually find the repeats using a simple list comprehension.
class repeat_detector(object):
def __init__(self, initial=None):
self.last = initial
def __call__(self, current):
if self.last == current:
return True
self.last = current
return False
strings = ["this", "this", "and", "that"]
is_repeat = repeat_detector()
repeats = [item for item in strings if is_repeat(item)]
Use the recipe for pairwise() from the stdlib itertools documentation (I'll quote it here):
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
And you can do:
for a, b in pairwise(L):
if a == b:
print a
Or with a generator expression thrown in:
for i in (a for a, b in pairwise(L) if a==b):
print i