I have a file with many lines each having over 20 numerical values, e.g.:
123 1 18 180776 4303656 1605 16468 0 51429 24230 0 0 0 4 8 0 8710 14705 1836 1 4 95 0 24538 0 187860 4264028 449 4711 0 2537 2537 0 0 5 0 0 0 6138 12880 1590 1 22 76 ...
I’d like to:
- read one line
- save it in some python data type (what would be the best ?)
- do some operations on each number taken from the above line, e.g.:
- check if it is not above sth (and save the result)
- check if it is not belowe sth (and save the result)
- count its length (number of digits) (and save the result)
- compare it to the previous number from the same column
–
So after a one line (with numbers) I should have:
- store each number
- store additional attributes for each number
Each number is compared to previous one from the same column, store the result but then I can forget the previous line.
What data type would be the best to store above data ?
It’s hard to say which data type would be best without knowing what you’re gonna do with the aggregated information. However, a simple solution would be to use a list for each line, containing 2-tuples with the actual number and a dictionary with attributes:
Here’s how to get this list starting from a line of text:
Then iterate the list and set attributes: