Sep-28-2020, 08:25 PM
(This post was last modified: Sep-28-2020, 08:25 PM by deanhystad.)
The problem is not the function extract_horse_data, though I don't know why you didn't write it as:
Instead of using global variables you should use function arguments.
You mention you can print out the data in the form and it looks ok. What do you mean by that, and where did you put your print statement? Can you amend your example to include the print statement?
def extract_horse_data(): horses_name.append(xx[horse_count][18]) for i in range(19) horses_info.append(xx[horse_count][i]) # or use horses_info.extend()My guess is that horse_count is not what you think it is. You probably started with something like this:
all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)] data = [] def extract_data(): for i in [1, 3, 5]: data.append(all_data[index][i]) for index in range(len(all_data)): if int(all_data[index][1][0]) in [3, 6, 8]: extract_data() print(data)
Output:['3b', '3d', '3f', '6b', '6d', '6f', '8b', '8d', '8f']
The index used by the for loop and the index used by extract_data() are the same variable. But then you changed the code to something like this:all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)] index = 0 def extract_data(): for i in [1, 3, 5]: data.append(all_data[index][i]) def get_data(indices): for index in range(len(all_data)): if int(all_data[index][1][0]) in indices: extract_data() data = [] get_data([3, 6, 8]) print(data)
Output:['1b', '1d', '1f', '1b', '1d', '1f', '1b', '1d', '1f']
Global variables are bad. The "index" used in extract_data() is the global variable which is zero (or whatever it was last set). The "index" used in get_data() is a local variable. Looping in get_data() is not changing "index" in "extract_data()".Instead of using global variables you should use function arguments.
all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)] def extract_data(index): for i in [1, 3, 5]: data.append(all_data[index][i]) def get_data(indices): for index in range(len(all_data)): if int(all_data[index][1][0]) in indices: extract_data(index) data = [] get_data([3, 6, 8]) print(data)This is only a guess. You did not include any code about how xx is made and what clear_data does. You got a bit too overzealous with your redacting and the indentation in your post makes me wonder how all the pieces are related. Also I am left wondering why extract_data appends 19 values, but the repeat in your printout is 9. Are you showing a real printout generated using the extract_data() provided in the post? Is the data messed up in xx?
You mention you can print out the data in the form and it looks ok. What do you mean by that, and where did you put your print statement? Can you amend your example to include the print statement?