Reputation: 321
I have a textfile with data. For example, I want to print out all the values for "animal". So when "animal" is choosen, it will print out "Monkey", "Elephant" and "Dog". It works somewhat, but it only prints out the first value. For example if I choose "animal", it only print out Monkey.
Is there a way to make it print out all of them? Maybe there is better ways to do it?
Data2.txt:
Adidas, shoe
Monkey, animal
Soup, food
Elephant, animal
Dog, animal
Taco, food
file = open('data2.txt')
data = file.readlines
stuffs = []
types = []
for line in data():
line = line.strip()
stuff, type = line.split(', ')
stuffs.append(stuff)
types.append(type)
animals = types.index('animal')
print (stuffs[animals])
Upvotes: 2
Views: 104
Reputation: 180481
Use a collections.defaultdict to group the types and the csv module to parse your file:
import csv
from collections import defaultdict
with open("test.txt") as f:
# create rows splitting on commas
r = csv.reader(f, skipinitialspace=True)
# create dict to store all the types
d = defaultdict(list)
# v = row[0], k = row[1]
for v,k in r:
d[k].append(v)
Output:
defaultdict(<class 'list'>, {'shoe': ['Adidas'],
'food': ['Soup', 'Taco'],
'animal': ['Monkey', 'Elephant', 'Dog']})
Then just lookup by key:
print(d["animal"])
print(d["shoe"])
['Monkey', 'Elephant']
['Adidas']
You don't ever need to call readlines unless you actually want a list, you could have iterated over the file object or simply passed it to the csv module and iterate over the reader object as in the code above.
Upvotes: 1
Reputation: 5668
d = {}
with open('data','r' ) as f:
for line in f:
le, r = line.split(',')
d.setdefault(r.strip(),[]).append(le.strip())
for k,v in d.items():
print(k,v)
shoe ['Adidas']
food ['Soup', 'Taco']
animal ['Monkey', 'Elephant', 'Dog']
Upvotes: 1
Reputation: 10417
Use numpy this way:
import numpy as np
a = np.loadtxt("myFile")
#Then it's simple!
a[a[:,1] == 'animal'][0]
Upvotes: 0
Reputation: 82929
The way you populate your lists, you have one list with animals, and one with the corresponding types, in the same position. Using index
, you will only get the first match, but you need all.
One way would be to use zip
to iterate pairs of animals and types, and print each animal where the type is correct.
for s, t in zip(stuffs, types):
if t == "animal":
print(s)
Or you can use a list comprehension to collect all the animals in a list:
>>> [s for s, t in zip(stuffs, types) if t == "animal"]
['Monkey', 'Elephant', 'Dog']
Or, change the way you store your data. For example, instead of having two lists with corresponding indices and zipping those lists back to one list of pairs, you could create a list of pairs to start with:
pairs = []
for line in data():
line = line.strip()
pairs.append(line.split(', '))
print([s for s, t in pairs if t == "animal"])
Or even use a dictionary, mapping types to stuffs, as suggested in some of the other answers.
Upvotes: 1
Reputation: 27802
I think a better idea would be to use a dict:
file = open('data2.txt')
data = file.readlines
categories = {}
for line in data():
line = line.strip()
stuff, type = line.split(', ')
categories.setdefault(type, []).append(stuff)
print (categories['animal'])
Upvotes: 1
Reputation: 7057
You need to loop through types because types.index('animal') will only return you the first one. Once you find the index, you can find the corresponding index in stuffs. Try this:
i = 0
for type in types:
if (type == 'animal'):
print (stuffs[i])
i = i + 1
Upvotes: 1