Python - how to store lines of numerical values?
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 l开发者_开发知识库ine, 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 ?
Usually you would use a list
for this
When you read a line into a variable, say row
row.split()
will give you a list
but each element will be a string, and you need numbers. You can get a list of numbers (looks like they are all integers) using a list comprehension like this
[int(x) for x in row.split()]
you can also use a list comprehension to compare with sth
This filters just the items that are < sth
[int(x) for x in row.split() if int(x)<sth]
This returns a list of bool
where True
means that the corresponding item is < sth
[int(x)<sth for x in row.split()]
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:
line = [(1, {'even': False, 'foo': 'bar'}), ..., (2332, {'even': True, 'foo': 'baz'}), ...]
Here's how to get this list starting from a line of text:
line = "4 0 2837 9323 ..."
line = [(int(n), dict()) for n in line.split()]
Then iterate the list and set attributes:
for n, attributes in line:
...
attributes['foo'] = 'bar'
The standard data types should be fine for most purposes: i.e. list
or tuple
. Use list
if you will be altering the values in-place.
You should be able to do something like this (untested code):
all_the_lines = []
with open("filename.txt", 'r') as f:
for line in f:
data = [int(x) for x in line.split()]
# do something with the data if you want
all_the_lines.append(data)
After this, all_the_lines will hold a list
of list
s with the raw data (converted to integers). You need to figure out how you will hold the other information you want to save: for example, pointers to all elements which were above a certain number. You can calculate all this during the loop above, or afterward.
If you want to perform heavy-duty mathematical calculations on the numbers (it sounds like you don't), consider using array
s from the numpy
package instead.
Have a look at NumPy
- How to load an array
- How to test array values
精彩评论