'Python for each row, while column A value is the same, add to list. When column A value changes, separate list
I'm trying to assess a CSV/Excel file that is organized like this:
172 Afghanistan AFG 1961 0
173 Afghanistan AFG 1962 0
174 Afghanistan AFG 1963 0
175 Afghanistan AFG 1964 0
176 Afghanistan AFG 1965 0
177 Afghanistan AFG 1966 0
178 Afghanistan AFG 1967 0
179 Afghanistan AFG 1968 0
180 Afghanistan AFG 1969 0
181 Afghanistan AFG 1970 0
182 Afghanistan AFG 1971 0
183 Afghanistan AFG 1972 0
184 Afghanistan AFG 1973 1
185 Afghanistan AFG 1974 0
186 Afghanistan AFG 1975 0
187 Afghanistan AFG 1976 3
188 Afghanistan AFG 1977 0
189 Afghanistan AFG 1978 0
190 Afghanistan AFG 1979 0
191 Afghanistan AFG 1980 0
192 Afghanistan AFG 1981 0
193 Afghanistan AFG 1982 0
194 Afghanistan AFG 1983 0
195 Afghanistan AFG 1984 0
196 Afghanistan AFG 1985 0
197 Afghanistan AFG 1986 0
198 Afghanistan AFG 1987 0
199 Afghanistan AFG 1988 0
200 Afghanistan AFG 1989 0
279 Albania ALB 1961 0
280 Albania ALB 1962 0
281 Albania ALB 1963 0
282 Albania ALB 1964 2
Essentially, I'm trying to put the 2nd column (country names) in one list with no repeats (country_names = ['Afghanistan', 'Albania',…]), and another list of lists of the last column into a list like this: list_of_regime_lists=[[0,0,0,0,0,0,0,…],[0,0,0,0,0,0,0,…], etc.]
Is there a way to write a line of code WITHOUT USING NUMPY OR PANDAS along the lines of "while column remains same value, append line[4] to list?"
Here's what I have so far:
def read_file(fp):
reader = csv.reader(fp)
next(reader, None)
country_names = []
regime_list = []
list_of_regime_lists = []
for line in reader:
country = line[1]
regime = int(line[4])
if country != "":
if country not in country_names:
if regime_list != []:
list_of_regime_lists.append(regime_list)
regime_list = []
country_names.append(country)
regime_list.append(regime)
if country in country_names:
regime_list.append(regime)
return country_names, list_of_regime_lists
Solution 1:[1]
Thanks to @ekhumoro
def read_file(fp):
reader = csv.reader(fp)
next(reader, None)
country_names = []
regime_list = []
list_of_regime_lists = []
for line in reader:
country = line[1]
regime = int(line[4])
if country != "":
if country not in country_names:
regime_list = []
list_of_regime_lists.append(regime_list)
country_names.append(country)
regime_list.append(regime)
if country in country_names:
regime_list.append(regime)
return country_names, list_of_regime_lists
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CloudyMinds |
