Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:04:17+00:00 2026-06-18T00:04:17+00:00

We have a large number of strings containing substrings that are possibly integers eg.

  • 0

We have a large number of strings containing substrings that are possibly integers eg.

mystring = "123 345 456 567 678 789"

and need to verify that:

a. each substring is in fact an integer eg. in mystring = "123 345 456 567 abc 789" fails when it reaches ‘abc’

b. each integer is within the range 0 <= i <= 10000 eg. mystring = "123 -345 456 567 678 789" fails when it reaches ‘-345’

One solution is:

mylist= [int(i) for i in mystring.split() if isinstance(int(i), int) and (0 <= int(i) <= 10000)]

Questions are:

i. In the list comprehension, for each i, does the int(i) get evaluated once or multiple times?

ii. Is there an alternative method that could be faster (as the volume of strings is large and each string could contain hundreds to thousands of integers)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:04:17+00:00Added an answer on June 18, 2026 at 12:04 am

    I think that I would probably use something like:

    try:
        if not all( (0 <= int(i) <= 10000) for i in mystring.split() ):
           raise ValueError("arg!")
    except ValueError:
        print "Oops, didn't pass"
    

    This has the advantage that it short circuits if something fails to convert to an int or if it doesn’t fall in the correct range.

    Here’s a silly test:

    def test_str(mystring):
        try:
            return all( (0 <= int(i) <= 10000) for i in mystring.split() )
        except ValueError:
            return False
    
    print test_str("123 345 456 567 abc 789")
    print test_str("123 345 456 567 -300 789")
    print test_str("123 345 456 567 300 789")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have large number of strings, approximately 15,000 that I stored in a SQLite
I have a large number of strings that I need to store in a
I have an application that manages a large number of strings. Strings are in
I have a large number of objects which are identified by names (strings). So,
I have a large number of records (10,000, increasing every day) that essentially is
I have a large number of documents (mainly PDFs) that I want to index
I have a large number of csv files that look like this below: xxxxxxxx
I have a large number of C# WCF services that are being called by
We have a large number of Python bytearray strings of variable length which need
I have a table containing a large number of rows. Each row has 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.