Python新手 [英] Python newbie
问题描述
我是一个Python新手,他决定看看Python的大惊小怪。
坦率地说,我有点困惑。在与Perl(从1994年开始使用Perl v4开始,并且从那以后一直这样做)之后有几个月的b $ b经验之后,这就是令我困惑的事情:
perl -e''@ a =(1,2,3); map {$ _ * = 2} @a; map {print" $ _ \ n" ;; } @a;''
Python中的等价物如下所示:
Python 2.5.1(r251:54863,2008年6月15日) ,18:24:51)
[GCC 4.3.0 20080428(Red Hat 4.3.0-8)] on linux2
输入help,copyright ;,信用或许可证或欲获得更多信息。
>> a = [1,2,3]
地图((lambda x:2 * x),a)
[2,4,6]
>> map((print),a)
文件"< stdin>",第1行
map((打印),a)
^
语法错误:语法无效
>> for x in a :print x
....
1
2
3
>> for x in a:x = 2 * x
....
>> for x in a:print x
....
1
2
3
>>>
有几个问题:
1)为什么数组a ;在使用
map进行转换后没有变化?
2)为什么传递内置函数print是不合法的?映射?
3)为什么数组a在使用for进行明确的
转换后没有改变循环?
4)是否有等价的\ $ a(Perl引用),这将允许我
决定何时使用变量值和何时通过引用?
PHP还允许使用foreach更改数组循环:
#!/ usr / local / bin / php
<?php
$ a =数组(1,2,3) );
foreach($ a as& $ x){$ x = $ x * 2; } $ / $
array_walk($ a,create_function(''$''',''print(" $ a\\\
");''));
?>
我怎样才能确保
for x in a:x = 2 * x
实际上更改了数组的元素a?
http://mgogala.freehostia .com
_ * = 2} @a; map {print"
_ \ n" ;; } @a;''
Python中的等价物如下所示:
Python 2.5.1(r251:54863,2008年6月15日) ,18:24:51)
[GCC 4.3.0 20080428(Red Hat 4.3.0-8)] on linux2
输入help,copyright ;,信用或许可证或欲获得更多信息。
>> a = [1,2,3]
地图((lambda x:2 * x),a)
[2,4,6]
>> map((print),a)
文件"< stdin>",第1行
map((打印),a)
^
语法错误:语法无效
>> for x in a :print x
....
1
2
3
>> for x in a:x = 2 * x
....
>> for x in a:print x
....
1
2
3
>>>
有几个问题:
1)为什么数组a ;在使用
map进行转换后没有变化?
2)为什么传递内置函数print是不合法的?映射?
3)为什么数组a在使用for进行明确的
转换后没有改变循环?
4)是否有相当于\
a(Perl引用),这将允许我
决定何时按值使用变量以及何时按引用使用?
PHP还允许使用foreach更改数组循环:
#!/ usr / local / bin / php
<?php
I am a Python newbie who decided to see what that Python fuss is all about.
Quite frankly, I am a bit perplexed. After having had few months of
experience with Perl (started in 1994 with Perl v4, and doing it ever
since) , here is what perplexes me:
perl -e ''@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;''
The equivalent in Python looks like this:
Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a=[1,2,3]
map((lambda x: 2*x),a)
[2, 4, 6]
>>map((print),a)
File "<stdin>", line 1
map((print),a)
^
SyntaxError: invalid syntax
>>for x in a: print x
....
1
2
3
>>for x in a: x=2*x
....
>>for x in a: print x
....
1
2
3
>>>
There are several questions:
1) Why is the array "a" unchanged after undergoing a transformation with
map?
2) Why is it illegal to pass a built-in function "print" to map?
3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?
4) Is there an equivalent to \$a (Perl "reference") which would allow me to
decide when a variable is used by value and when by reference?
PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php
<?php
$a=array(1,2,3);
foreach($a as &$x) { $x=$x*2; }
array_walk($a,create_function(''$a'',''print("$a\n"); ''));
?>
How can I make sure that
for x in a: x=2*x
actually changes the elements of the array "a"?
http://mgogala.freehostia.com
_*=2 } @a; map { print "
_\n"; } @a;''
The equivalent in Python looks like this:
Python 2.5.1 (r251:54863, Jun 15 2008, 18:24:51)
[GCC 4.3.0 20080428 (Red Hat 4.3.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>a=[1,2,3]
map((lambda x: 2*x),a)
[2, 4, 6]
>>map((print),a)
File "<stdin>", line 1
map((print),a)
^
SyntaxError: invalid syntax>>for x in a: print x
....
1
2
3>>for x in a: x=2*x
....
>>for x in a: print x
....
1
2
3>>>
There are several questions:
1) Why is the array "a" unchanged after undergoing a transformation with
map?
2) Why is it illegal to pass a built-in function "print" to map?
3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?
4) Is there an equivalent to \
a (Perl "reference") which would allow me to
decide when a variable is used by value and when by reference?
PHP also allows changing arrays with "foreach" loop:
#!/usr/local/bin/php
<?php
这篇关于Python新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!