地图似乎没有迭代
map does not seem to iterate
我是 python 编码的新手,这是我给自己布置的一项任务,目的是了解这门语言。
我正在尝试 return DNA 串的反面:
- T -> A
- A -> T
- C -> G
- G -> C
下面我写了代码,但是好像没有迭代。我在函数中放了一个 print(x)
行来检查它是否完全通过字符串,但是没有字符串被 returned.
也没有 returned 错误。
s = "AACCGGCCAA"
def repl(x):
print(x)
if x == "A": return "T"
elif x == "T": return "A"
elif x == "C": return "G"
elif x == "G": return "C"
output = map(repl, s)
print(output)
预期输出为:
TTGGCCGGTT
我得到的所有输出都是 <map object at 0x02A9F8D0>
,为什么不是字符串?
map
函数是惰性的。试试这个 output = list(map(repl, s))
.
或者如果你想输出字符串而不是列表,output = ''.join(map(repl, s))
。
我是 python 编码的新手,这是我给自己布置的一项任务,目的是了解这门语言。
我正在尝试 return DNA 串的反面:
- T -> A
- A -> T
- C -> G
- G -> C
下面我写了代码,但是好像没有迭代。我在函数中放了一个 print(x)
行来检查它是否完全通过字符串,但是没有字符串被 returned.
也没有 returned 错误。
s = "AACCGGCCAA"
def repl(x):
print(x)
if x == "A": return "T"
elif x == "T": return "A"
elif x == "C": return "G"
elif x == "G": return "C"
output = map(repl, s)
print(output)
预期输出为:
TTGGCCGGTT
我得到的所有输出都是 <map object at 0x02A9F8D0>
,为什么不是字符串?
map
函数是惰性的。试试这个 output = list(map(repl, s))
.
或者如果你想输出字符串而不是列表,output = ''.join(map(repl, s))
。