Home > Mobile >  i want to select all the values less than 50 how can i do that
i want to select all the values less than 50 how can i do that

Time:09-24

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)

output:-

['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']

CodePudding user response:

At the end of your code the variable records holds a list of strings.

You can either add 'if' statements when filling it, to ensure only 50 values get in, or, you can analyze the list after filling it.

Notice the difference between str and int objects, and notice the commas , (see 1,234) you'll need to get rid off.

I'd add the following snippet after your code:

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

This will create the wanted list. Note that this is not the most efficient (memory time), but it is simple to use and understand.

CodePudding user response:

You can easily do this with the help of lambda expression and filter.

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

Do you need string again?

CodePudding user response:

Use an if statement:

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)

With the walrus operator. If your Python version is under 3.8, use:

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)
  • Related