Reputation: 861
I am having a data in the form of ids
and value
, where the value of ids are not continuous , i want to store the data in arraylist.
For Java:
ArrayList<Integer>[] Data = new ArrayList[1000000];
for(int i=0;i<Data.length;i++) Data[i] = new ArrayList<>();
for(int i=0;i<n;i++){
int id_index = in.nextInt();
int value = in.nextInt();
Data[id_index].add(value);
}
How to implement the same functionality in Python ?
Upvotes: 0
Views: 58
Reputation: 1406
I think different way. python is a dynamic type language. dose not need to define variables like java.
just append every things u like, by every size you want!
>>> x=[]
>>> x.append([12])
>>> x
[[12]]
>>> x.append([15,16,17])
>>> x
[[12], [15, 16, 17]]
Upvotes: 0
Reputation: 5613
As Alex Hall's answer stated, you can use a list of lists as the following:
data = [[] for i in range(1000000)]
I just want to add the second part of your code:
for _ in range(n):
id_index, value = raw_input().split()
data[int(id_index)].append(int(value))
If you are using python 3.x then change raw_input()
to input()
Upvotes: 0
Reputation: 36043
A list of lists will do fine, which can be created as so:
data = [[] for i in range(1000000)]
Upvotes: 1