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

  • Home
  • SEARCH
  • 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 9233245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:30:26+00:00 2026-06-18T06:30:26+00:00

I have a list that I create inside of function1 . I want to

  • 0

I have a list that I create inside of function1. I want to be able to access and modify it in function2. How can I do this without a global variable?

Neither function is nested within the other and I need to be able to generalize this for multiple lists in several functions.

I want to be able to access word_list and sentence_starter in other functions.

def Markov_begin(text):
    print create_word_lists(text)
    print pick_starting_point(word_list)
    return starting_list


def create_word_lists(filename):
   prefix_dict = {}    
   word_list = []
   sub_list = []
   word = ''

   fin = open(filename)
   for line in fin:
      the_line = line.strip()
      for i in line:
           if i not in punctuation:
               word+=(i)
           if i in punctuation:
               sub_list.append(word)
               word_list.append(sub_list)
               sub_list = []
               word = ''
      sub_list.append(word)
      word_list.append(sub_list)
   print 1
   return word_list

def pick_starting_point(word_list):
    sentence_starter = ['.','!','?']
    starting_list = []
    n = 0
    for n in range(len(word_list)-1):
        for i in word_list[n]:
            for a in i:
                if a in sentence_starter:
                    starting_list += word_list[n+1]
    print 2                
    return starting_list



def create_prefix_dict(word_list,prefix_length):
    while prefix > 0:
        n = 0
        while n < (len(word_list)-prefix):
            key = str(''.join(word_list[n]))
            if key in prefix_dict:
                prefix_dict[key] += word_list[n+prefix]
            else:
                prefix_dict[key] = word_list[n+prefix]
           n+=1
       key = ''
       prefix -=1

print Markov_begin('Reacher.txt')
  • 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-18T06:30:27+00:00Added an answer on June 18, 2026 at 6:30 am

    You should refactor this as a class:

    class MyWords(object):
      def __init__(self):
        self.word_list = ... #code to create word list
    
      def pick_starting_point(self):
        # do something with self.word_list
        return ...
    

    Usage

    words = MyWords()
    words.pick_starting_point()
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created the list that have device detected, and I want to send data
I have a List<string> and I want to iterate over this collection and do
I have a list of draggable table cells that can be dropped onto a
I have a list of items that are created from the results of website
I have a list of class object that I created as a variable in
I have created a list view that displays the names and dates of items
I have created a list form that gets attached to a main form in
So I'm running through a list of things and have code that creates an
I have created a repeat control that list some documents. I would like to
I have a gridview and in that gridview i created a list of imagebuttons

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.