Python:这里使用[]是什么意思?

发布于 2024-09-24 13:10:53 字数 413 浏览 5 评论 0原文

python中这两个语句有什么区别?

var = foo.bar

var = [foo.bar]

认为它正在将 var 放入包含 foo.bar 的列表中,但我不确定。另外,如果这是行为并且 foo.bar 已经是一个列表,那么在每种情况下你会得到什么?

例如:如果 foo.bar = [1, 2] 我会得到这个吗?

var = foo.bar #[1, 2]

var = [foo.bar] #[[1,2]] where [1,2] is the first element in a multidimensional list

What is the difference in these two statements in python?

var = foo.bar

and

var = [foo.bar]

I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

For example: if foo.bar = [1, 2] would I get this?

var = foo.bar #[1, 2]

and

var = [foo.bar] #[[1,2]] where [1,2] is the first element in a multidimensional list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2024-10-01 13:10:54

[] 是一个空列表。

[foo.bar] 正在创建一个新列表 ([]),其中 foo.bar 作为列表中的第一项,然后可以由其索引引用:

var = [foo.bar]
var[0] == foo.bar # returns True 

所以您猜测您对 foo.bar = [1,2] 的分配是完全正确的。

如果您还没有这样做,我建议您在 Python 交互式解释器中尝试一下这种事情。这使得事情变得非常简单:

>>> []
[]
>>> foobar = [1,2]
>>> foobar
[1, 2]
>>> [foobar]
[[1, 2]]

[] is an empty list.

[foo.bar] is creating a new list ([]) with foo.bar as the first item in the list, which can then be referenced by its index:

var = [foo.bar]
var[0] == foo.bar # returns True 

So your guess that your assignment of foo.bar = [1,2] is exactly right.

If you haven't already, I recommend playing around with this kind of thing in the Python interactive interpreter. It makes it pretty easy:

>>> []
[]
>>> foobar = [1,2]
>>> foobar
[1, 2]
>>> [foobar]
[[1, 2]]
阳光①夏 2024-10-01 13:10:54

是的,它正在创建一个包含一个元素 foo.bar 的列表。

如果 foo.bar 是 [1,2],您确实会得到 [[1,2]]。

例如,

>> a=[]
>> a.append([1,2])
>> a[0] 
[1,2]
>> b=[[1,2]]
>> b[0]
[1,2]

为了详细说明这个确切的例子,

>> class Foos:
>>   bar=[1,2]
>> foo=Foos()
>> foo.bar
[1,2]
>> a=[foo.bar]
>> a
[[1,2]]
>> a[0]
[1,2]

Yes, it's making a list containing one element, foo.bar.

If foo.bar is [1,2], you indeed get [[1,2]].

For instance,

>> a=[]
>> a.append([1,2])
>> a[0] 
[1,2]
>> b=[[1,2]]
>> b[0]
[1,2]

To elaborate a bit more on that exact example,

>> class Foos:
>>   bar=[1,2]
>> foo=Foos()
>> foo.bar
[1,2]
>> a=[foo.bar]
>> a
[[1,2]]
>> a[0]
[1,2]
御弟哥哥 2024-10-01 13:10:54

我认为它正在将 var 放入包含 foo.bar 的列表中,但我不确定。另外,如果这是行为并且 foo.bar 已经是一个列表,那么在每种情况下您会得到什么?

  • 是的,它创建了一个新列表。

  • 如果foo.bar已经是一个列表,它将简单地变成一个列表,包含一个列表。

    <前><代码>h[1]>>>> l = [1, 2]
    h[1]>>>> [l]
    [[1, 2]]
    h[3]>>>>升[升][0]
    [1, 2]

I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

  • Yes, it creates a new list.

  • If foo.bar is already a list, it will simply become a list, containing one list.

    h[1] >>> l = [1, 2]
    h[1] >>> [l]
    [[1, 2]]
    h[3] >>> l[l][0]
    [1, 2]
    
秋千易 2024-10-01 13:10:54

这几乎意味着它是一个数组/列表,其中 foo.bar 是列表/数组中的第一项。

That pretty much means it's an array/list of stuff with foo.bar being the first item in the list/array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文