Use arguments in a function from another function

Asked

Viewed 42 times

0

I have a text document with the following format:

word_1 word_2 word_3
word_4 word_5 word_6

For each line of that text document:

  • first: I want to save the words and add "_info" to each word;
  • second: the arguments I want to use in the next function (which repeats for each line) are:

    def my_function 
    ("word_1","word_2","word_3","word_1_info","word_2_info","word_3_info")
    

After the first calculations with the arguments of the first line, the following arguments will be:

def my_function ("word_4","word_5","word_6","word_4_info","word_5_info","word_6_info")

The closest I’ve been was reading (because I’m having trouble adding "_info" to each word), the information from the text file as follows:

f = open("test.txt","r")
array=[]

for line in f:
    x = re.findall(r"[a-zA-Z]+_+\d",line)
    array.append(x)


def my_function("array[0][0]","array[0][1]","array[0][2]")

How can I improve my code to do what I need?

1 answer

0

Hello, I’ll try to help you :)

For python this "array=[ ]" is actually a list

>>> type([]) ===:> class list

When indicating a file you can pass directly to a loop.

I imagine this file internally would be like this:

word_4

word_5

word_6

word_4

word_5

new_list = list()

for linhas in open("test.txt"):

#lines .... txt reading had taken place but there will be an n

lines.strip() # this way will not occur an empty line

ref = f'{lines.strip()}_info' # using f string

nova_list.append(ref) # adds the list

If in this file there is more than 10 contents.

Will write all ?

Could use a Skimmer

def my_function (*nova_list)

loop.....

If you really want to access each one by the function

def my_function (exem1, exem2, exem3, exem4 ,exem5)

print(exem1)

print(exem2)

Just call the function by passing the list

my_function(*nova_list)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.