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 8339597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:55:30+00:00 2026-06-09T04:55:30+00:00

I am working on a script and the script should read a text file

  • 0

I am working on a script and the script should read a text file and test to see if the specified letters(a,a,r,d,v,a,r,k) are on each line. Im having a problem as I am trying to check for 3 different a’s instead of just one. My code is below:

#Variables
advk = ['a','a','r','d','v','a','r','k']
textfile = []
number = 0
life = 0

for line in open('input.txt', 'rU'):
    textfile.append(line.rstrip().lower())


while life == 0:
    if all(word in textfile[number] for word in advk):
        printed = number + 1
        print ("Aardvark on line " + str(printed))
        number+=1
        if number == len(textfile):
            life+=1

    else:
        number+=1
  • 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-09T04:55:31+00:00Added an answer on June 9, 2026 at 4:55 am

    Everytime you want to count something in python, keep the Counter class in mind.


    from collections import Counter
    
    advk = Counter(['a','a','r','d','v','a','r','k'])
    
    with open('input.txt', 'rU') as file:
        for i, line in enumerate(file.readlines()):
            if not advk - Counter(line.lower()):
                print ("Aardvark on line " + str(i+1))
    

    Given the input line

    dffdaardvarksdsda

    the Counter would look like these

    Counter({'d': 5, 'a': 4, 'f': 2, 's': 2, 'r': 2, 'k': 1, 'v': 1})
    

    and

    Counter({'a': 3, 'r': 2, 'd': 1, 'k': 1, 'v': 1})
    

    for your list of letters to search.

    We use a trick by simply substracting the two Counters advl - Counter(line.lower()) and check if the resulting Counter has no elements left.


    Other things to note:

    You can use the with statement to ensure your file gets closed.
    You can use enumerate instead counting the line numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on watin script which read forms on e-shop. In this test I
I'm currently working on a script which should analyze a dataset based on a
I have a working script from someone, there in is this line: this.event =
I have working registration script the only problem is that i do not know
I am trying to get my session login script working and I had the
I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to
I'm trying to get a file upload progress bar working in a rails 3
I'm trying to get simple code working, unfortunately I'm a python beginner. My script
I'm trying to troubleshoot and solve this problem: the server I'm working on (php
I have a script working well for creating ad hoc iPhone builds. I can

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.