Reputation: 27
I want to print the first 10 lines of a list
I am new to python. This actually my first python project. I don't know how to go about it.
The list looks like this. How do we call such a list in python?
numbers = [[0.4437678, 0.4318693, 0.3873539, 0.03706345]]
[[0.4437677, 0.4318692, 0.3873538, 0.03706344]]
[[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
[[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
[[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
...
...
...
[[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
up to about 500 lines
I want to read and print the first 10 lines of this list
Upvotes: 0
Views: 1634
Reputation: 5982
An example with a generator and no leading zero when slicing the list.
my_list = [1,2,3,4,5,6,7,8,9,10,11]
print(* (x for x in my_list[:10]), sep='\n' )
1
2
3
4
5
6
7
8
9
10
Here, I believe *
is used to unpack the generator object.
Also, pandas uses the shorthand [:]
syntax a lot so it will be good to get used to.
Upvotes: 1
Reputation: 2200
The most efficient way is to use list
slicing which goes like input_list[0:10]
where 0
is inclusive index and 10
is exclusive. So you would end up processing elements from 0 through 9.
for element in input_list[0:10]:
# process with element
...
Upvotes: 1
Reputation: 6143
You don't have specify your dataset assign to which variable. If you use this
mydata = [[0.4437678, 0.4318693, 0.3873539, 0.03706345]]
[[0.4437677, 0.4318692, 0.3873538, 0.03706344]]
[[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
[[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
[[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
...
...
...
[[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
up to about 500 lines
use this code
mydata[0:10]
This will give you first 10 raws of your dataset
Upvotes: 1
Reputation: 4489
You have a nested list. See below for how to access using indices:
test = [['item1']]
print(test)
>> [['item1']]
print(test[0])
>> ['item1']
print(test[0][0])
>> 'item1'
You haven't given how your series of lists is stored, but you'll probably be able to cut off how many you want using slicing:
series[:10]
So if your series looked like this (a list of nested lists):
series = [[['item1']],
[['item2']],
[['item3']],
[['item4']],
[['item5']],
[['item6']],
[['item7']],
[['item8']],
[['item9']],
[['item10']],
[['item11']]]
And you wanted to grab the first 10, it would be:
series[:10]
If you want to unnest, you would need to access each maybe in a loop:
for i in range(0, 10):
print(series[i][0][0])
To give you:
item1
item2
item3
item4
item5
item6
item7
item8
item9
item10
Upvotes: 3