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])
Related
How to check if a nested list contains a substring?
strings = [[],["one", "two", "three"]]
substring = "wo"
strings_with_substring = [string for string in strings if substring in string]
print(strings_with_substring)
this script just prints :
[]
how to fix it? output should be:
two
==
Sayse, solution you provided doesn't work for me. I am new to python. I am sure I am missing something here. any thoughts?
import re
s = [[],["one", "two", "three"]]
substring = "wo"
# strings_with_substring = [string for string in strings if substring in string]
strings_with_substring = next(s for sl in strings for s in sl if substring in s)
print(strings_with_substring)
You are missing another level of iteration. Here is the looping logic without using a comprehension:
for sublist in strings:
for item in sublist:
if substring in item:
print(item)
Roll that up to a comprehension:
[item for sublist in strings for item in sublist if substring in item]
You're looking for
next(s for sl in strings for s in sl if substring in s)
This outputs "two", if you want a list of all elements then change the next for your list comprehension with given ammendments, or likewise, change next to any if you just want a boolean result
Since you said it should just print the string ~ You could use itertools to flatten your list and run it through a filter that you loop over.
from itertools import chain
strings = [[], ['one', 'two', 'three']]
substring = 'wo'
for found in filter(lambda s: substring in s, chain.from_iterable(strings)):
print(found)
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 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']
If I have a list of strings such as:
[("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
What should I do in order to get rid of all the 8s in each string? I tried using strip or replace in a for loop but it doesn't work like it would in a normal string (that not in a list). Does anyone have a suggestion?
Try this:
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print([s.strip('8') for s in lst]) # remove the 8 from the string borders
print([s.replace('8', '') for s in lst]) # remove all the 8s
Beside using loop and for comprehension, you could also use map
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylst = map(lambda each:each.strip("8"), lst)
print mylst
A faster way is to join the list, replace 8 and split the new string:
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylist = ' '.join(mylist).replace('8','').split()
print mylist
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print mylist
j=0
for i in mylist:
mylist[j]=i.rstrip("8")
j+=1
print mylist
Here's a short one-liner using regular expressions:
print [re.compile(r"8").sub("", m) for m in mylist]
If we separate the regex operations and improve the namings:
pattern = re.compile(r"8") # Create the regular expression to match
res = [pattern.sub("", match) for match in mylist] # Remove match on each element
print res
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
msg = filter(lambda x : x != "8", lst)
print msg
EDIT:
For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8.
Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped.
To make this (kinda work?) with how the intent of the question was we could perform something similar to this
msg = filter(lambda x: x != "8", map(lambda y: list(y), lst))
I am not in an interpreter at the moment so of course mileage may vary, we may have to index so we do list(y[0]) would be the only modification to the above for this explanation purposes.
What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"].
This would result in a data type that looks like this
msg = [["a", "a", "a", "a"], ["b", "b"]...]
So finally to wrap that up we would have to map it to bring them all back into the same type roughly
msg = list(map(lambda q: ''.join(q), filter(lambda x: x != "8", map(lambda y: list(y[0]), lst))))
I would absolutely not recommend it, but if you were really wanting to play with map and filter, that would be how I think you could do it with a single line.