arrays - How do I know what data type to use in Python? -


I'm working through Python on some tutorials and in a situation where I'm trying to decide What type of data / structure should I use in a fixed position.

I am not clear on the difference between arrays, lists, dictionaries and tuples.

How do you decide which is appropriate - let my current understanding distinguish between them - they seem to be the same thing.

What are the benefits / typical use cases for each one?

Tuples first. There are things like this list that can not be modified. Since the contents of the tupal can not change, you can use the topless as a key in a dictionary. This is the most useful place for them in my opinion. For example, if you have a list like object = ["Ford Pickup", 1993, 995] and you have a list with prices If you want to create a small memory database, you can do something like this:

  ikey = tuple (item [0], object [1]) idata = item [2] db [ikey] = Idata   

The list appears to be used in array or other programming languages ​​and usually for the same type of things in Python is. However, they are more flexible that you can keep a variety of things in the same list. Generally, this is the most flexible data structure because you can put the entire list in a list of any other list, but they can not be efficient enough for actual data crunching.

  a = [1, "Fred", 7.3] b = [] b.append (1) b [0] = "fred" b.append (a) # second of B now Element is the full list  

The dictionary is often done much like lists, but now you can use any irreversible thing as a dictionary. However, unlike lists, dictionaries Is not a natural order and can not be sorted in place. Of course you can create your own class, which includes a sorted list and a dictionary so that an arched dictionary can be treated as an example on the Python cookbook site.

  c = {} d = ("ford pickup", 1993) c [d] = 9995  

When you are crunching heavy duty data And if you do not want lists or lists of frames, the arrays are close to the bit level they are often not used outside of scientific applications. Leave them until you know for sure that you need them.

Lists and guides are the true workshops of Python data collection.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -