How to code no duplicates and sorted in python? - python

I am trying to code a function which can index no duplicates and sorted, and return to a list, here is what I wrote:
def get_common_words_used(sentence, common_words):
new_list = ""
for item in sentence:
if item == common_words:
new_list = new_list.lower()
new_list = [item] + new_list
new_list.sort()
return new_list
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
This prints Nothing
But my expected output is ['go']
Please help.

This should help.
def get_common_words_used(sentence, common_words):
new_list = []
for item in set(sentence.split()): #Split by space and use set to remove duplicates.
if item in common_words:
new_list.append(item)
return sorted(new_list) #sort
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
test_get_common_words_used()
Output:
['go']

First you need to split the given string into words then you need to check whether that item present in given list or not
def get_common_words_used(sentence, common_words):
new_list = []
for item in sentence.split(" "):# You need to split the string into words by space
if item in common_words:
new_list.append(item)
new_list = list(set(new_list))# List will have the duplicates
new_list.sort()
return new_list
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
Output:
['go']

Related

How to Find the Length of max itam in List? python

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.

Remove all instances from a list

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))

How to print quantity of identical elements in a list in Python?

I wanted to know how I can print the quantity of the items in a list in Python. For example:
list1 = ["hello", "goodbye", "hello"]
message = ", ".join(list1)
print(message)
I would like my output to be "2x hello, 1x goodbye". I have not found any library that does something like that, but I just started coding so I might be missing something. If you know any library that does this or if you have any clue on how I could do it, I am open to any advice.
Thank you in advance!
Edit: thank you all for the big help. All the answers were useful and solved the problem.
You can use Counter to do this:
from collections import Counter
list1 = ["hello", "goodbye", "hello"]
counts = Counter(list1)
message = ", ".join(f"{counts[key]}x {key}" for key in counts)
print(message)
This code uses f-strings and list comphrensions.
You can use a Counter collection for that matter: https://docs.python.org/3/library/collections.html#collections.Counter
list1 = ["hello", "goodbye", "hello"]
unique_values = set(list1)
result = []
for value in unique_values:
result.append("{}x {}".format(list1.count(value), value))
message = ", ".join(result)
print(message)
you can create dictionary with each element in your list as key to the dictionary. and then print or use the dict for counting the occurance.
# temp_dict will have the count for each element.
temp_dict={}
for item in list1:
if item in temp_dict.keys():
temp_dict[item]+=1
else:
temp_dict[item]=1
#temp_dict will contain the count of each element in your list1.
print(temp_dict)
for key,value in temp_dict.items():
print(str(value)+'x'+key)
#Aplet123 already gave an answer but if you don't want to import Counter, you can use the below code.
list1 = ["hello", "goodbye", "hello"]
x= [str(list1.count(i))+'x'+i for i in set(list1)]
message = ", ".join(x)
print(message)
you can use pandas in this example:
import pandas as pd
list1 = ["hello", "goodbye", "hello"]
counts = Counter(list1)
a = pd.Series(counts)
for i in range(2):
print(a[i] , "x" , a.index[i])
output:
2 x hello
1 x goodbye

Cut of first and last char of a string in python

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])

Python list finding with index

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']

Categories

Resources