我知道Python中的列表用方括号[]
包围。
我的输出看起来如下所示:
[(451,165,76,-77,[98,42])]
这是一个元组列表吗?我们如何访问这些项目?
感谢。
答案 0 :(得分:2)
这是一个包含list
的{{1}},其中前四个元素为tuple
,最后一个integers
为element
list
} 2
。
您可以正确访问integers
的所有值。以下是一些例子:
indexing
答案 1 :(得分:2)
只需在终端试一试,这一切都是不言自明的:
>>> x = [(451,165,76,-77,[98,42])]
>>> type(x)
<type 'list'>
>>> x[0]
(451, 165, 76, -77, [98, 42])
>>> x[0][1]
165
>>> x[0][4]
[98, 42]
>>> type(x[0][4])
<type 'list'>
>>> x[0][4][1]
42
无论如何,仅仅因为输出看起来像一个列表,它不需要是一个列表。它可以是一个更复杂的数据结构(某个用户定义类的对象),它通过重载__str__()
或__repr__()
指定打印方式(详见https://docs.python.org/3/reference/datamodel.html#object.str)
答案 2 :(得分:2)
是的,这是一个元组列表。
a = [(451,165,76,-77,[98,42])]
type(a) #> list
type(a[0]) #> tuple
您可以像这样访问元组的第一项:
a[0][0] #> 451
元组的最后一项是一个列表:
a[0][4] #> [98, 42]
要访问该列表的元素,只需添加另一层括号:
a[0][4][0] #> 98
答案 3 :(得分:0)
这是一个包含一个元组的列表,它自己包含5个元素,其中一个元素是一个表。
访问:
a=[(451,165,76,-77,[98,42])]
a[0][0][2]
将返回76
a[0][0][4][0]
将返回98
答案 4 :(得分:0)
所以我写了一个类型检查员,这对我很有用,因为我们只能猜测数据输出为字符串时的类型。我们知道它可能是Person
或Frombulate
类型的对象,他们选择str()
作为给定。
def type_diver(obj, prefix="", index = ""):
print "%s'%s': %s%s" % (prefix, obj, type(obj), " - Accessible via " + index if index else "")
if not isinstance(obj, str):
try:
for ind, elem in enumerate(obj):
type_diver(elem, " " * len(prefix) + "|---", index + "[%s]" % ind)
except TypeError:
pass # Don't call recursively on non-iterable objects
对于基本python对象,在您的输入中提供:
'[(451, 165, 76, -77, [98, 42])]': <type 'list'>
|---'(451, 165, 76, -77, [98, 42])': <type 'tuple'> - Accessible via [0]
|---'451': <type 'int'> - Accessible via [0][0]
|---'165': <type 'int'> - Accessible via [0][1]
|---'76': <type 'int'> - Accessible via [0][2]
|---'-77': <type 'int'> - Accessible via [0][3]
|---'[98, 42]': <type 'list'> - Accessible via [0][4]
|---'98': <type 'int'> - Accessible via [0][4][0]
|---'42': <type 'int'> - Accessible via [0][4][1]
但是我可以写一个str()
输出看起来像这样的自定义类。要知道的唯一方法就是问。
如果您的数据为hpaulj suggested [numpy数组],您可能会看到以下内容:
'[(451, 165, 76, -77, [98, 42])]': <type 'numpy.ndarray'>
|---'(451, 165, 76, -77, [98, 42])': <type 'numpy.void'> - Accessible via [0]
|---'451': <type 'numpy.int32'> - Accessible via [0][0]
|---'165': <type 'numpy.int32'> - Accessible via [0][1]
|---'76': <type 'numpy.int32'> - Accessible via [0][2]
|---'-77': <type 'numpy.int32'> - Accessible via [0][3]
|---'[98 42]': <type 'numpy.ndarray'> - Accessible via [0][4]
|---'98': <type 'numpy.int32'> - Accessible via [0][4][0]
|---'42': <type 'numpy.int32'> - Accessible via [0][4][1]
鉴于每个人都有一个他们对打字感兴趣的不同对象,这是一个更通用的解决方案。但是,它只调查可迭代的对象。
答案 5 :(得分:0)
虽然直接的Python描述是一个包含整数和列表的元组的列表,但显示也与numpy
结构化数组一致。
定义化合物dtype:
In [596]: dt = np.dtype('i,i,i,i,(2)i')
In [597]: dt
Out[597]: dtype([('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', '<i4', (2,))])
使用此列表输入创建一个数组。复合dtype(结构化数组)的数据采用元组列表的形式。每个元组代表数组的记录或元素的数据:
In [598]: arr = np.array([(451,165,76,-77,[98,42])], dtype=dt)
In [599]: arr
Out[599]:
array([(451, 165, 76, -77, [98, 42])],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', '<i4', (2,))])
这个数组的打印看起来就像问题:
In [600]: print(arr)
[(451, 165, 76, -77, [98, 42])]
这是一个包含5个字段的1个元素数组。最后一个字段有形状(2,),实际上是一个2元素数组。
In [601]: arr['f4']
Out[601]: array([[98, 42]], dtype=int32)
In [602]: _.shape
Out[602]: (1, 2)