What you want is called an associative array. In python these are called dictionaries.
Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
myDict = {}
myDict["john"] = "johns value"
myDict["jeff"] = "jeffs value"
Alternative way to create the above dict:
myDict = {"john": "johns value", "jeff": "jeffs value"}
Accessing values:
print(myDict["jeff"]) # => "jeffs value"
Getting the keys (in Python v2):
print(myDict.keys()) # => ["john", "jeff"]
In Python 3, you'll get a dict_keys, which is a view and a bit more efficient (see views docs and PEP 3106 for details).
print(myDict.keys()) # => dict_keys(['john', 'jeff'])
If you want to learn about python dictionary internals, I recommend this ~25 min video presentation: https://www.youtube.com/watch?v=C4Kc8xzcA68. It's called the "The Mighty Dictionary".
Answer from miku on Stack OverflowPython Array with String Indices - Stack Overflow
How can I get the index of a string in Python list, which contains a certain character? - Stack Overflow
How do I search a list of Strings for a certain word and return the index?
Python - string variable as list indexes - Stack Overflow
Videos
What you want is called an associative array. In python these are called dictionaries.
Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
myDict = {}
myDict["john"] = "johns value"
myDict["jeff"] = "jeffs value"
Alternative way to create the above dict:
myDict = {"john": "johns value", "jeff": "jeffs value"}
Accessing values:
print(myDict["jeff"]) # => "jeffs value"
Getting the keys (in Python v2):
print(myDict.keys()) # => ["john", "jeff"]
In Python 3, you'll get a dict_keys, which is a view and a bit more efficient (see views docs and PEP 3106 for details).
print(myDict.keys()) # => dict_keys(['john', 'jeff'])
If you want to learn about python dictionary internals, I recommend this ~25 min video presentation: https://www.youtube.com/watch?v=C4Kc8xzcA68. It's called the "The Mighty Dictionary".
Even better, try an OrderedDict (assuming you want something like a list). Closer to a list than a regular dict since the keys have an order just like list elements have an order. With a regular dict, the keys have an arbitrary order.
Note that this is available in Python 3 and 2.7. If you want to use with an earlier version of Python you can find installable modules to do that.
For example if I have a list
cities = ["The capital of france is Paris", "The capital of france is Berlin"," The capital of france is London"," The capital of france is Barcelona"]
How do I search the list for "Berlin" and get the index of the sentence containing "Berlin"?
you can take the following steps:
import csv
list= [tuple(row) for row in csv.reader(open(filename, 'rU'))]
Above code will get you your data in the form :
list = [('Orange', 'Apple', 'Strawberry', 'Banana'), (1, 12, 5, 11), (2, 3, 10, 9)]
the next step you can perform is :
new_list = [x[3] for x in list]
output :
new_list = ['Banana', 11, 9]
I hope this solved your problem, if not, tell me. I'm happy to help!
Peace out //
Parthik Bhandari
Ex.
from collections import defaultdict
import csv
the_data = defaultdict(list)
with open("fruit_data.csv", "r") as fd:
reader = csv.DictReader(fd)
for row in reader:
for fruit, number in row.items():
the_data[fruit].append(number)
for fruit, the_list in the_data.items():
print fruit, the_list
O/P:
Orange ['1', '2']
Strawberry [' 5', ' 10']
Apple [' 12', ' 3']
Banana [' 11', ' 9']