我想选择所有小于50的值我该怎么做

2024-10-01 04:10:11 发布

您现在位置:Python中文网/ 问答频道 /正文

match_records = soup.find_all('td',class_ = 'rankings-block__banner--matches')
match_records

records = []
for i in match_records:
    records.append(i.text)
records

match_records2 = soup.find_all('td',class_ = 'table-body__cell u-center-text')
match_records2
for i in match_records2:
    records.append(i.text)
print(records)

输出:-

['17', '32', '3,793', '28', '3,244', '32', '3,624', '25', '2,459', '27', '2,524', '30', '2,740', '30', '2,523', '32', '2,657', '17', '1,054', '7', '336', '25', '1,145', '11', '435', '20', '764', '7', '258', '11', '330', '9', '190', '14', '232', '6', '97', '9', '0']

Tags: textinformatchallfindblockclass
3条回答

lambda表达式和filter的帮助下,您可以很容易地做到这一点

records = list(filter(lambda a: int(a.replace(',', '')) < 50, records))

你还需要绳子吗

在代码的末尾,变量records包含字符串列表

您可以在填充列表时添加“if”语句,以确保只包含50多个值,也可以在填充列表后分析列表

注意strint对象之间的区别,注意逗号,(请参见1234),您需要去掉它们

我将在您的代码之后添加以下代码段:

fixed_records = [int(record.replace(',','')) for record in records]

这将创建通缉名单。 请注意,这并不是最有效的(内存+时间),但它易于使用和理解

使用if语句:

match_records = soup.find_all('td',class_ = 'rankings-block__banner matches')
match_records
records = []
for i in match_records:
    if (a:=int(i.text.replace(',', '')) < 50:
        records.append(a)
print(records)

match_records2 = soup.find_all('td',class_ = 'table-body__cell u-center-text')
match_records2
for i in match_records2:
    if (a:=int(i.text.replace(',', '')) < 50:
        records.append(a)
print(records)

和海象接线员联系。如果您的Python版本低于3.8,请使用:

match_records = soup.find_all('td',class_ = 'rankings-block__banner matches')
match_records
records = []
for i in match_records:
    a = int(i.text.replace(',', ''))
    if a < 50:
        records.append(a)
print(records)

match_records2 = soup.find_all('td',class_ = 'table-body__cell u-center-text')
match_records2
for i in match_records2:
    a = int(i.text.replace(',', ''))
    if a < 50:
        records.append(a)
print(records)

相关问题 更多 >