-2

I am writing a Python 3.x program that reads from a text file, extracts the necessary information, and performs further processing.

The linked question does not solve my problem, I am aware of how to use Google and have tested that code, it DOES NOT WORK.

I have a variable, pointStore, which contains only integers. I am unable to find a solution where I can extract only the elements I need from this list or a solution that would allow me to remove corresponding elements in other lists as well. Take for example I am collecting phone numbers in one list, names in another list, and addresses in the third. If I remove an element from one, I would like to remove its corresponding elements in other lists. How would I go about doing this if I can't even get the values I need in the first place?

I think a messy solution would be to write pointStore line-by-line to a file, read that file, and iterate over it line by line only grabbing the elements I need but I would like to write cleaner code.

I've looked around quite a bit on several different websites including Stack Overflow and I can't find an answer that actually solves MY problem or an answer that can be applied to my problem.

def function():
    userInput = input("Enter a cutoff value: ")
    for element in myList:
        if element < userInput:
           myList.remove(element)

When I try to run the code above, it removes some values from the list, but not in accordance with the criteria I set, ex. I specified 5000, it removed 1800 but not 3882.

I expected the program to iterate over the list myList and remove values that are less than the user's input, stored in userInput.

  • 1
    `pointStore = [element for element in pointStore if int(element) < int(pVCInput)]` – IcedLance Jun 25 '19 at 11:07
  • So you want to ``filter`` out some values? – MisterMiyagi Jun 25 '19 at 11:08
  • 1
    As for *why* this is going wrong: You are modifying your list while iterating over it. The list iterator is not made for this. See https://stackoverflow.com/q/56397337/5349916 – MisterMiyagi Jun 25 '19 at 11:11
  • 1
    Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – MisterMiyagi Jun 25 '19 at 11:13
  • @IcedLance Your solution causes the following error: "UnboundLocalError: local variable 'pointStore' referenced before assignment". As I said, I searched Stack Overflow and several other websites. I've tried regex but I don't know how to convince it to only pull elements greater than the user's specified cutoff. – Kenneth Martin Jun 25 '19 at 11:43
  • @MisterMiyagi That doesn't work in this program, which is why I asked a new question. That solution doesn't actually create a new list with the preferred values, it creates a new list with ALL the values. – Kenneth Martin Jun 25 '19 at 11:45
  • @MisterMiyagi The rest of the application works as intended. This is the part that doesn't which is why I submitted this portion for inspecting. The elementsChecked is a typo here, not on my code. My apologies. I'll rewrite my code here to better conform to stack overflow standards. – Kenneth Martin Jun 25 '19 at 12:04
  • @KennethMartin Please provide the complete data, i.e. also the input and ``myList``, to reproduce the problem. If a list comprehension does not work for you, please provide the actual code used as well. If any errors occur, please provide at least the top of the traceback pointing to where in your code the error was thrown. – MisterMiyagi Jun 25 '19 at 12:21
  • @MisterMiyagi The input can be any integer, including or excluding commas. The list that I am iterating contains 206 elements, that is not a "minimal reproducible example". They are all integers, this should be good enough. I have provided plenty of information -- what the list contains, what I need the function to do, and what the function is doing. I explained that list comprehensions simply copy the entire list into a new list. I have provided the error my code gives me. My code has been reformatted. Every time I change something you want something else different, too. – Kenneth Martin Jun 25 '19 at 12:39
  • @KennethMartin It is clear that your code does not work. What is not clear is how the recommended solutions do not work. And we cannot help you with that if you don't show us how you applied them. – MisterMiyagi Jun 25 '19 at 12:47

2 Answers2

1

If you want specific code, provide something other people can run and correct. Such as, how do you create myList. If it is a list of ints, you could make a sample.

myList = [8, 6, 7, 5, 3, 0, 9]
userInput = "5"
cutoff=int(userInput)
filtered_list = [ i for i in myList if i>cutoff ]
print(filtered_list)

Now there is something to run, and you can see the results:

[8, 6, 7, 9]

If your list looks more like.

listOfStrings = ["12,345", "67,890"]

You can change it to a list of ints.

myList = [int(s.replace(",", "")) for s in listOfStrings]

Then you can filter it. Another way is to use the locale. https://stackoverflow.com/a/2954231/2067492

matt
  • 10,892
  • 3
  • 22
  • 34
  • MyList is read from a file using regex. The pattern of the values I'm reading is '\d+,\d+' -- so a number like 12,345. The comma is removed. Is it possible that my list is still considered to contain strings, and that is causing the problem? – Kenneth Martin Jun 25 '19 at 13:08
  • You don't have an example of the code. If you did find_all or something, then you have strings (or tuples) and not ints. You can just do int("12,345") and it turns into an int depending on your locale, you don't have to actually "remove the comma". – matt Jun 25 '19 at 13:10
  • @KennethMartin also, you're example code doesn't work because you're removing elements while iterating over them. If you're also trying to compare strings that *could* be another source of error. – matt Jun 25 '19 at 13:11
  • @KennethMartin maybe you do need to remove the comma, ill add an example of that to the answer. – matt Jun 25 '19 at 13:16
0

After restructuring some code, @matt solved my issue. List comprehensions were simply copying the entire list, his solution actually did what I needed and the rest of the program now works as intended.

myList = [int(s.replace(",", "")) for s in listOfStrings]