简体   繁体   中英

What is the best way to print position of items in the list of strings using python

I've started some web scraping things and I'm pretty new in python. I want to find position of a element in the list of strings in python. So far I've tried some if any statements but python always returns me "bool is not iterable". I'am using regex and I've managed to obtain valuable things and now want to display position of the matching string in list. When I use code from below I get an output like this:

for i in range(0, len(string_data)):
print(string_data[i])

<td class="kx o_1" data-bookmaker="16">
<td class="kx o_0" data-bookmaker="16">
<td class="kx o_2 winner" data-bookmaker="16">

What is to best way to find position of an item in list that matches word "winner".In my case it would be the second position if I count it from 0, but how can I do that?

There might be a cleaner, shorter way to do this, but we can write out a nice loop to track the element position, search the list of td elements, and once it encounters winner , print out that position:

position = 0 # start position at index 0

td_elements = driver.find_elements_by_tag_name("td") # get elements to iterate
# td_elements = driver.find_elements_by_xpath("//td[contains(@class, 'kx')]")
# ^ this is an alternate selector, in case tag_name is too generic.

# iterate td elements, searching for 'winner' in the class
for element in td_elements:

    # check if class attribute contains winner
    if ("winner" in element.get_attribute("class")):
        print(str(position) # winner! print position of element

    else: position++ # increment position if we did not find a winner

Hope this helps a bit. Another user posted a solution using BeautifulSoup , which seems to work well if you are already using BS. I am providing a pure Selenium example, in case that is what you are using here.

You can do this with np.where .

If you your list contains exactly string you want to match, eg:

import numpy as np
items = ['something', 'something else', 'winner']
winner_ids = np.where([item == 'winner' for item in items])

You mentioned re so here's how you can match against a substring:

import numpy as np
items = ['something', 'something else', 'something containing winner']
winner_ids = np.where([re.findall('winner', item) for item in items])

Be careful that np.where will return a list of items. In the two examples, winner_ids is (array([2]),) . If you're expecting to find a single winner, you can then do:

winner_id = winner_ids[0][0]

And now winner_id is 2 as you expect.

you can use enumerate to return the index value:

from bs4 import BeautifulSoup

html = '''
<td class="kx o_1" data-bookmaker="16">
<td class="kx o_0" data-bookmaker="16">
<td class="kx o_2 winner" data-bookmaker="16">'''

soup = BeautifulSoup(html, 'html.parser')
for idx, item in enumerate(soup.find_all('td')):
    print (idx, item['class'])

Output:

0 ['kx', 'o_1']
1 ['kx', 'o_0']
2 ['kx', 'o_2', 'winner']

And to just return if it has winner:

for idx, item in enumerate(soup.find_all('td')):
    if 'winner' in item['class']:
        print (idx, item['class'])

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