[英]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.我已经开始了一些网络抓取工作,而且我对 python 还很陌生。 I want to find position of a element in the list of strings in python.我想在python的字符串列表中找到元素的位置。 So far I've tried some if any statements but python always returns me "bool is not iterable".到目前为止,我已经尝试了一些语句,但 python 总是返回“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?在列表中找到匹配单词“winner”的项目位置的最佳方法是什么。在我的情况下,如果我从 0 开始计算它,它将是第二个位置,但我该怎么做?
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:可能有一种更简洁、更短的方法来做到这一点,但我们可以写出一个很好的循环来跟踪元素位置,搜索td
元素列表,一旦遇到winner
,就打印出该位置:
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.另一个用户使用BeautifulSoup
发布了一个解决方案,如果您已经在使用 BS,它似乎工作得很好。 I am providing a pure Selenium example, in case that is what you are using here.我提供了一个纯 Selenium 示例,以防您在这里使用。
You can do this with np.where
.你可以用np.where
做到这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:你提到了re
所以这里是你如何匹配子字符串:
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.请注意np.where
将返回项目列表。 In the two examples, winner_ids
is (array([2]),)
.在这两个示例中, winner_ids
是(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.现在winner_id
是2
正如你所期望的。
you can use enumerate
to return the index value:您可以使用enumerate
返回索引值:
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'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.