Je veux définir un tableau en python. comment ferais-je ça? Dois-je utiliser la liste?comment définir un tableau en python?
2
A
Répondre
4
liste est meilleure, mais vous pouvez utiliser le tableau comme ceci:
array('l')
array('c', 'hello world')
array('u', u'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])
Plus d'infos there
5
4
Pourquoi voulez-vous utiliser un tableau sur une liste? Voici un comparison of the two qui indique clairement les avantages des listes.
1
Si vous avez besoin d'un tableau parce que vous travaillez avec d'autres constructions de bas niveau (comme en C), vous pouvez utiliser ctypes.
import ctypes
UINT_ARRAY_30 = ctypes.c_uint*30 # create a type of array of uint, length 30
my_array = UINT_ARRAY_30()
my_array[0] = 1
my_array[3] == 0
3
Il existe plusieurs types de tableaux en Python, si vous voulez un tableau classique, il serait avec le module de tableau:
import array
a = array.array('i', [1,2,3])
Mais vous pouvez aussi utiliser tuples sans avoir besoin d'autres modules d'importation :
t = (4,5,6)
Ou listes:
l = [7,8,9]
Un tuple est plus efficace dans l'utilisation, mais il a une taille fixe, alors que vous pouvez facilement ajouter de nouveaux éléments à une liste:
>>> l.append(10)
>>> l
[7, 8, 9, 10]
>>> t[1]
5
>>> l[1]
8
Questions connexes
- 1. Comment convertir un tableau HTML en tableau en python
- 2. Entier tableau en Python
- 3. importer un tableau en python
- 4. Comment créer un tableau d'entiers en Python?
- 5. Comment j'élèverais un tableau bidimensionnel en python?
- 6. Définir le tableau en vue tableau?
- 7. Comment définir un tableau avec un autre tableau?
- 8. Définir un tableau imbriqué en C++
- 9. Comment faire un os.path.join avec un tableau en python?
- 10. Comment définir un tableau avec plusieurs valeurs?
- 11. tableau binaire en python
- 12. Comment puis-je # définir un tableau en C?
- 13. Itère un tableau multidimensionnel en Python
- 14. créer un tableau 2d en python?
- 15. Tri un tableau python
- 16. Comment définir une classe en Python
- 17. Définir l'horloge matérielle en Python?
- 18. Comment sélectionner un élément aléatoire d'un tableau en Python?
- 19. affichant un tableau 2d coloré dans matplotlib en Python
- 20. Définir des produits en Python
- 21. Comment définir un tableau de types personnalisés dans WSDL?
- 22. Définir dynamiquement l'héritage en Python
- 23. Comment déclarer un tableau de zéros en python (ou un tableau d'une certaine taille)
- 24. Définir et envoyer un tableau d'objets JSON
- 25. fusion tableau indexé en Python
- 26. comment définir MATLABPATH en Python et en utilisant mlabwrap?
- 27. Afficher un tableau en tant qu'image raster en python
- 28. Python Scrapy, comment définir un pipeline pour un article?
- 29. Python distutils.Extension: définir un niveau de concurrence
- 30. Définir un double tableau sans taille fixe?