简体   繁体   English

使用python打印字符串列表中项目位置的最佳方法是什么

[英]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_id2正如你所期望的。

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.

相关问题 如何使用 Python 3 中的 format 方法打印字符串列表中的项目 - How to Print items in a List of Strings using the format method in Python 3 在python中删除字符串列表元素内的“索引”的最佳方法是什么? - What is the best way to remove an "index" inside the elements of a list of strings in python? 在python中将具有3个子列表的列表打印到文件中的最佳方法是什么? - What is the best way to print a list with 3 sublists to a file in python? 使用2个字符串列表,试图找到一种方法来从相反的列表中以相同的位置/顺序打印字符串。(Python) - With 2 list of strings, trying to find a way to print the string in the same position/order from the opposite list.(Python) 在PHP中转义Python字符串的最佳方法是什么? - What is the best way to escape Python strings in PHP? 在 Python 中随机化字符串列表的最佳方法 - Best way to randomize a list of strings in Python 在 Python 中打印带有分隔符的表格的最佳方法是什么 - What is the best way to print a table with delimiters in Python 在python中打印列表输出的最佳方法 - Best way to print list output in python 在Python中使用行和列作为参数在.txt文件上的正确位置上打印单词的最佳方法 - Best way to print word on .txt file on right position using line and column as parameter in Python 什么是使用python识别列表中的元组长度为0的最佳方法 - what is best way to indentify that the length of tuple in a list is 0 using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM