Python:阵列v.清单 [英] Python: Array v. List

查看:88
本文介绍了Python:阵列v.清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Python列表与数组-何时使用?

Possible Duplicate:
Python List vs. Array - when to use?

我正在用Python处理一些项目,并且我有几个问题:

I'm working on a few projects in Python, and I have a few questions:

  1. 数组和列表有什么区别?
  2. 如果从问题1中不能明显看出,我应该使用哪个?
  3. 您如何使用首选的? (创建数组/列表,添加项目,删除项目,随机选择项目)

推荐答案

除非您需要C数组库中的某些非常特定的功能,否则请使用列表.

Use lists unless you want some very specific features that are in the C array libraries.

python确实具有三个原始数据结构

python really has three primitive data structures

tuple = ('a','b','c')
list = ['a','b','c']
dict = {'a':1, 'b': true, 'c': "name"}

list.append('d') #will add 'd' to the list
list[0] #will get the first item 'a'

list.insert(i, x) # Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).    

list.pop(2) # will remove items by position (index), remove the 3rd item
list.remove(x) # Remove the first item from the list whose value is x.

list.index(x) # Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list.count(x) # Return the number of times x appears in the list.

list.sort(cmp=None, key=None, reverse=False) # Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

list.reverse() # Reverse the elements of the list, in place.

有关数据结构的更多信息,请参见: http://docs.python.org/tutorial/datastructures.html

More on data structures here: http://docs.python.org/tutorial/datastructures.html

这篇关于Python:阵列v.清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆