简体   繁体   中英

Compare a selenium object list with a list of strings

I am automating Twitter posting and have a list of extracted selenium objects in items_on_queue that I need to compare with a list of objects in things_to_tweet.

I need to compare these two lists and eliminate any duplicates I find to output a unique list that I can use to post.

The problem is that they're of a different format and I don't know how to normalize them to compare them.

items_on_queue = [<selenium.webdriver.remote.webelement.WebElement (session="0ea37ee02c8889e324028b8a7a66568f", element="0.3932763505379022-2")>, <selenium.webdriver.remote.webelement.WebElement (session="0ea37ee02c8889e324028b8a7a66568f", element="0.3932763505379022-3")>]

things_to_tweet = [('The Nanome User Story', '...', 'https://blog.matryx.ai/the-nanome-stack-user-story-530a95812484'), ('Goldman Sachs CEO sees Bitcoin as part of a financial n', '...', 'https://cryptodaily.co.uk/2017/11/goldman-sachs-ceo-sees-bitcoin-part-financial-new-world/'), ('$8,000? Goldman Sachs Analysts See Possible Bitcoin Pri', '...', 'https://www.coindesk.com/8000-goldman-sachs-analysts-see-possible-bitcoin-price-jump/')]

The interesting thing about the selenium oject list is that each item is in the same format as those in things_to_tweet. I only need to ad .text to the object and the result appears as such(example):

el = <selenium.webdriver.remote.webelement.WebElement (session="0ea37ee02c8889e324028b8a7a66568f", element="0.3932763505379022-2")>

el.text = ('The Nanome User Story', '...', 'https://blog.matryx.ai/the-nanome-stack-user-story-530a95812484')

This is the same format I have in things_to_tweet

How can I compare these two different format lists to eliminate duplicates? I guess I need to do a comparison of all el.text in a list compared with all el from the other list, but don't know how to do it.

Any ideas? Thank you

You can loop over both lists using zip() :

for item, tweet in zip(items_on_queue, things_to_tweet):
    if item.text == tweet:
        # do something

or you can loop over both lists:

for el in items_on_queue:
    for tweet in items_to_tweet:
        if tweet == el.text:
            # do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM