0

I have some Python experience, but (obviously) not an overwhelming wealth of knowledge. In short, I need to create validation arrays for Neural Network testing using 1/5th of a main array. So, I am able to append i % 5 == 0 values to a test array. From there, I can still print every 5th value. Now, when I go to remove values, I get the out of index or range error; I know it is because the remove is changing the overall value of len(string_arr). However, I have been unsuccessful in figuring out a way to compensate.

Below is a 'dummy' program to solve what I need, but not the actual thing I am working on. I need 1/5th on a validation array, remove that 1/5th from the main array, and have 4/5th left on the main to train on. Below, I tried to appended to another array and remove those values in order to not mess up the len(string_arr)... did not work.

Thank you

english_list = open('file')
for word in english_list.readlines():
word = word[:-1].lower()
if len(word) == 6:
    string_arr.append(word)
    target_arr.append(0)
print(string_arr)
print(len(string_arr))


for i in range(len(string_arr)):
    if i % 5 == 0:
        test_arr.append(string_arr[i])

for i in range(len(string_arr)):
    if i % 5 == 0:
        one_more.append(string_arr[i])

for i in range(len(string_arr)):
    if one_more[i] == string_arr:
        string_arr.remove(one_more[i])

print(test_arr)
print(len(test_arr))
print(string_arr)
print(len(string_arr))
3
  • Can you use external libraries? If so check this method from sklearn Commented Feb 27, 2019 at 14:07
  • 1
    Perhaps you would like to correct the indentation of your code so it is runnable and makes sense. Commented Feb 27, 2019 at 14:08
  • @JosepJoestar thank you and I am aware of some of the simplicity the sklearn adds to all this, but I must create the separate arrays Commented Feb 27, 2019 at 14:33

1 Answer 1

1

You can do it in a slightly more compact manner with fancy list indexing:

small_cut = string_arr[::5]
remaining = [j for sub in zip(string_arr[1::5], string_arr[2::5], string_arr[3::5], string_arr[4::5]) for j in sub]

This is assuming you're working with normal python lists; if you're working with something like a numPy array, there might be even easier approaches (potentially more efficient, too) to do what you want.

Sign up to request clarification or add additional context in comments.

3 Comments

NameError: name 'j' is not defined
@brunodesthuilliers did you get the whole line?
err... copy-paste issue indeed, my bad xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.