本文介绍了当需要对象的内容时,Python返回对象的内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规格: Python 3.3.2

Specs: Python 3.3.2

我正在尝试做的事情:

Create a simple name and employee number dictionary
application. Have the user enter a list of names and employee numbers. Your
interface should allow a sorted output (sorted by name) that displays employee
names followed by their employee numbers.

我想出的是:

# enter a list of names of employees
# enter a list of employee numbers
# zip them together

def hrcat():
    name = input('Please enter names of employees: ')
    number = input('Please enter employee numbers: ')

    output = zip(name,number)
    print(output)

问题:

给定两个名称和数字列表时,它返回对象的内存地址;看起来像这样:

When given two lists of names and numbers, it returns the memory address of an object; something looks like this:

>>> hrcat()
Please enter names of employees: a,b,c
Please enter employee numbers: 1,2,3
<zip object at 0x7fea10ce4b90>

我想知道为什么它返回内存地址而不是该对象的实际内容?我在网上搜索了Google,但找不到解决该问题的答案.感谢您的见解!

I wonder why it returns the memory address instead of the actual content of that object? I googled online but wasn't able to find answers addressing this question. Thank you for your insights!

推荐答案

在Python 3.x中 zip 返回一个迭代器(看起来像<zip object at 0x7fea10ce4b90>).您可以应用list来查看内容,

In Python 3.x zip returns an iterator (which looks like <zip object at 0x7fea10ce4b90>). You can apply list to view the contents,

list(zip(name,number))

尽管,如果您只是在制作字典,可以忘记列表,而直接使用迭代器填充它,

Although, if you are just making a dictionary, can forget about the list and use the iterator directly to populate it,

dict(zip(name,number))

这篇关于当需要对象的内容时,Python返回对象的内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:04