How to find out which data type to use in Python?

I am working on some Python tutorials and am in a position where I am trying to decide which type / structure of data to use in a particular situation.

I do not understand the differences between arrays, lists, dictionaries, and tuples.

How do you decide which one is suitable - my current understanding does not allow me to distinguish between them at all - they seem the same.

What are the benefits / typical use cases for each of them?

+3
source share
5 answers

. , . , . , . , , item = ["Ford pickup", 1993, 9995], , - :

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

, , Python. , . , , , .

a = [1,"fred",7.3]
b = []
b.append(1)
b[0] = "fred"
b.append(a) # now the second element of b is the whole list a

, , . , , . , , , dict . Python Cookbook .

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

, , . . , , .

Python.

+3

, ? :

, , , , . , .

dict - .

+6

defaultdict

from collections import defaultdict

s = 'asdhbaklfbdkabhvsdybvailybvdaklybdfklabhdvhba'
d = defaultdict(int)

for c in s:
   d[c] += 1

print d['a']   # prints 7
+3

/? dict.

0

: m . , .

: - / . , .

: , "" , / . , .

0

Source: https://habr.com/ru/post/1720309/


All Articles