how to find the unique items in a list in python
I have a list,
mylist=[4, 4, 2, 1, 2] my expected output is =
list(set(mylist)) but its not helping
thanks in advance
You may use
collections.Counterto get the count of all the items present in the list, then filter it using list comprehension the items having count as
>>> from collections import Counter >>> mylist=[4, 4, 2, 1, 2] >>> [k for k, v in Counter(mylist).items() if v==1] 
See also questions close to this topic
Selenium Python Unable to scroll down, while fetching google reviews
I am trying to fetch google reviews with the help of selenium in python. I have imported webdriver from selenium python module. Then I have initialized self.driver as follows:-
self.driver = webdriver.Chrome(executable_path="./chromedriver.exe",chrome_options=webdriver.ChromeOptions())
After this I am using the following code to type the company name on google homepage whose reviews I need, for now I am trying to fetch reviews for "STANLEY BRIDGE CYCLES AND SPORTS LIMITED ":-
company_name = self.driver.find_element_by_name("q") company_name.send_keys("STANLEY BRIDGE CYCLES AND SPORTS LIMITED ") time.sleep(2)
After this to click on the google search button, using the following code:-
Then finally I am on the page where I can see results. Now I want to click on the View on google reviews button. For that using the following code:-
self.driver.find_elements_by_link_text("View all Google reviews").click() time.sleep(2)
Now I am able to get reviews, but only 10. I need at least 20 reviews for a company. For that I am trying to scroll the page down using the following code:
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(5)
Even while using the above code to scroll the down the page, I am still getting only 10 reviews. I am not getting any error though.
Need help on how to scroll down the page to get atleast 20 reviews. As of now I am able to get only 10 reviews. Based on my online search for this issue, people have mostly used: "driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")" to scroll the page down whenever required. But for me this is not working. I checked the the height of the page before and after ("driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")") is the same.
Create a new column from itereated rows of timedate data
I am attempting to create a downward velocity model for offshore drilling which uses the variables Depth (which increases every 1 foot) and DateTime data which is more intermittent and is only updated every foot of depth:
Dept DateTime 1141 5/24/2017 04:31 1142 5/24/2017 04:32 1143 5/24/2017 04:40 1144 5/24/2017 04:42 1145 5/25/2017 04:58
I am trying to get something like this:
Where Velocity iterated down dept/(DateTime gap)
one to one mapping in shell script
I am in process of migration. Migrating from old set of servers to new set of servers, where there is no logical relationship in the server names between the 2 sets. I have a script that runs on old server, takes all necessary backups and the run another script to copy the backups to new server and execute it.
I can combine both scripts(taking backup and copying to new server), if I can include a logic to map the old server to new server. Is there a way I can do this.
Old server New server King Queen Bat Ball water fire sand rock
What I am expecting is, if the script is run on server 'King', I want the script to identify that the corresponding new server is 'Queen' and copy the backups to Queen.
Python, removing items from list of buttons
So I have this code that reads in a spreadsheet to a dataframe, and then I have two functions that run when buttons are pressed. The first creates a list of new buttons for every column in the dataframe. The second is meant to wipe the list of buttons clear and delete the buttons from the screen.
I am having two issues. First, the building of the buttons in the first function works fine, but the list is weird. In my sample data, there are four columns. The buttons start as "...button" and then got to "...button2", "...button3", "...button4". I have no idea why there don't go either 0,1,2,3 or 1,2,3,4.
Then when I am deleting buttons in the second function, it crashes when it gets to the third button. No idea why. I am both removing the buttons from the grid and getting them out of the button list. I tried clearing the list as a whole instead of deleting every button as it steps through the list, but that hasn't worked. I tried several methods, as follows:
self.column_buttons =  del self.column_buttons[:] self.column_buttons.clear()
None of those will empty the list. If I build the button list, then use any of the above to try and clear it, then build it again, the buttons in the ist are 5-8, re-clearing and re-building then gets me 9-12, and so on.
Here is my code....
def fxThree(self): print(self.column_buttons) # make a new button in a new frame for each column in the file for x in range(len(self.df1_columns)): self.new_button = tk.Button(self.frame3, text=self.df1_columns[x], command=lambda x=x: delete_button(self, x)) self.new_button.grid(row=10, column=(x)) self.column_buttons.append(self.new_button) print(self.column_buttons) print("\n") def fxFour(self): for x in range(len(self.column_buttons)): print(x) print("Removing button: " + str(x)) self.column_buttons[x].grid_remove() print("Deleting list entry :" + str(x)) del self.column_buttons[x] del self.column_buttons[:] print(self.column_buttons)
This is what the output from the first function looks like in the console:
[<tkinter.Button object .!frame3.!button>, <tkinter.Button object .!frame3.!button2>, <tkinter.Button object .!frame3.!button3>, <tkinter.Button object .!frame3.!button4>]
This is the output and error from the second function:
0 Removing button: 0 Deleting list entry :0 1 Removing button: 1 Deleting list entry :1 2 Removing button: 2 Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\user\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:/Users/user/Documents/Data Management/Python/SheetMapper/sheet_mapper_01.3.py", line 312, in fxFour self.column_buttons[x].grid_remove() IndexError: list index out of range
I even tried having it iterate backwards starting at the last entry in case I was wiping out entries as I went, and still got a not in index error.
Get index of next smallest element in the list in Haskell
I m a newbie to Haskell. I am pretty good with Imperative languages but not with functional. Haskell is my first as a functional language.
I am trying to figure out, how to get the index of the smallest element in the list where the minimum element is defined by me.
Let me explain by examples.
For example :
Function signature minList :: x -> [x]
let x = 2 let list = [2,3,5,4,6,5,2,1,7,9,2] minList x list --output 1 <- is index
This should return 1. Because the at list is 3. It returns 1 because 3 is the smallest element after x (=2).
let x = 1 let list = [3,5,4,6,5,2,1,7,9,2] minList x list -- output 9 <- is index
It should return 9 because at list is 2 and 2 is the smallest element after 1. x = 1 which is defined by me.
What I have tried so far.
minListIndex :: (Ord a, Num a) => a -> [a] -> a minListIndex x  = 0 minListIndex x (y:ys) | x > y = length ys | otherwise = m where m = minListIndex x ys
When I load the file I get this error
• Couldn't match expected type ‘a’ with actual type ‘Int’ ‘a’ is a rigid type variable bound by the type signature for: minListIndex :: forall a. (Ord a, Num a) => a -> [a] -> a at myFile.hs:36:17 • In the expression: 1 + length ys In an equation for ‘minListIndex’: minListIndex x (y : ys) | x > y = 1 + length ys | otherwise = 1 + m where m = minListIndex x ys • Relevant bindings include m :: a (bound at myFile.hs:41:19) ys :: [a] (bound at myFile.hs:38:19) y :: a (bound at myFile.hs:38:17) x :: a (bound at myFile.hs:38:14) minListIndex :: a -> [a] -> a (bound at myFile.hs:37:1)
When I modify the function like this
minListIndex :: (Ord a, Num a) => a -> [a] -> a minListIndex x  = 0 minListIndex x (y:ys) | x > y = 2 -- <- modified... | otherwise = 3 -- <- modifiedd where m = minListIndex x ys
I load the file again then it compiles and runs but ofc the output is not desired.
What is the problem with
| x > y = length ys | otherwise = m
In short: Basically, I want to find the index of the smallest element but higher than the x which is defined by me in parameter/function signature.
Thanks for the help in advance!
How can I convert list into datetime with miliseconds?
I have these numbers in a list of strings:
and I would like to convert it into: