Yield关键字返回0x107e4b3c0,而Return函数仅返回列表中的最后一个值

时间:2018-12-01 07:55:02

标签: python python-2.7

我创建了一个小程序来输出Name char和char的位置,但是它没有按我的预期工作。

收益不返回任何内容,而Return语句仅返回char和num列表的最后一个值。

name_letters = "Alexi Rodrigo"

def get_number():
    for number in xrange(1, len(name_letters)+1):
        yield number

def letters_list():
    for letter in name_letters:
         yield letter


print "                      "  
print "======================"
print " "+" "+"Text w/ Numbers"
print "======================"
print "                      " 


output_text =  get_number(), letters_list()

print output_text


## Output should be  Letter + Position Number

1 个答案:

答案 0 :(得分:0)

为什么不尝试关注...

for n, c in enumerate(name_letters):
    print n, c

如果您不想对空间进行编号

name_letters = "Alexi Rodrigo"

n = 0
for c in name_letters:
    if c != ' ':
        print n, c
        n += 1
    else:
        print c