python¶ÁÎı¾Êý¾Ý
ÓÐÒ»¸öÎı¾ test.txt Àï±ßÈ«ÊÇÊý¾Ý£¬ÅÅÁиñʽ»¯ÈçÏ£º
1 2 3
4 5 6
7 8 9
ÔõôÓÃpython¶ÁÈ¡¸ÃÎı¾²¢½«Ã¿ÐÐÈý¸öÊý×ÖÏà¼Ó£¬×îºóÒÔÎı¾ out.txt Êä³ö½á¹û£¬±ÈÈ磺
1+2+3=6
4+5+6=15
-------
ÎÒ×Ô¼º¹¹½¨ÁËÒ»¸öº¯Êý def sum(a,b,c): return a+b+c
Ïë¶ÁÈ¡Ò»ÐÐÊý¾Ý·Ö±ð¸³¸øa£¬b£¬c È»ºóµ÷Óú¯ÊýµÃ³ö½á¹û£¬Ö®ºóÑ»·¶ÁȡÿÐв¢¼ÆËã½á¹ûÊä³ö¡£
×Ô¼º¸Õ¸Õѧϰpython£¬²»ÖªÈçºÎ¶ÁÈ¡Îı¾ºó·Ö±ð½«ÊýÖµ¸³¸øa£¬b£¬c Çë¸ßÊÖÖ¸µãÒ»ÏÂ
¸øÐÂÊÖ½âÊÍһϰɣ¬±Ï¾¹ÎÒÒ²ÊÇ´ÓÐÂÊÖ¹ýÀ´µÄ
ÒÔµÚÒ»ÐÐΪÀý£º
'+'.join(i.split()) + '=' + str(reduce(operator.add, map(lambda x:int(x), i.split())))
½âÊÍÈçÏ£º
--Ç°°ë²¿·Ö£º
i Ϊ'1 2 3'
i.split() Ϊ['1', '2', '3']
'+'.join(i.split()) Ϊ'1+2+3'
--ºó°ë²¿·Ö£º
lambda x:int(x) ÄäÃûº¯Êý£¬½«'1'ת»»ÎªÊý×Ö1
map(lambda x:int(x), i.split()) ÄÚÖõÄÓ³É亯Êý£¬¶Ôi.split()µÄÿ¸öÔªËØÓÃÇ°ÃæµÄº¯Êý×÷Ó³Éä
½á¹ûΪ[1,2,3]
reduce(operator.add, map(lambda x:int(x), i.split())) Ò»¸öÐÂÊÖ²»Ì«ÈÝÒ׶®µÄÄÚÖú¯Êý£¬Ê±¼ä¹Øϵ£¬LZ×Ô¼º¿´Êé°É£¬Ëü¾ÍÊǶԸոÕ[1,2,3]×÷ÀÛ¼Ó£¬½á¹ûΪ10
*************************************************
Ïà¹ØÎÊ´ð£º
×î½üÓÃpythonдһ¸ö³ÌÐò,×ÜÊÇÒì³£Í˳ö£¬µ«ÊÇȴû·¨ÓÃtry..exceptÀ´²¶×½
ÓÉÓÚû·¨·¢Í¼Æ¬£¬ËùÒÔ°ÑÏà¹ØµÄÐÅÏ¢´òÓ¡³öÀ´
Òì³£µÄÐÅÏ¢´óÖÂÈçÏ£º
python.exeÓöµ½ÎÊÌâÐèÒª¹Ø±Õ¡£ÎÒÃǶԴËÒýÆðµÄ²»±ã±íʾ±§Ç¸ ......
±¾ÎÄÕª×Ô¡¶¿É°®µÄPython¡·¡°¹âÅ̹ÊÊ¡±CDay-1 ʵÓû¯ÖÐÎÄ¡£
[img]http://t.douban.com/mpic/s3901817.jpg[/img]
CDay-1 ʵÓû¯ÖÐÎÄÖÐÎÄ´¦Àí£¬Íê³É¹¦ÄܵÄʵÓû¯
ÄãÅöµ½99%µÄÎÊÌ⣬ÆäËûÈË֮ǰÒѾÓöµ½¹ýÁË£¬Ë ......
ÇëÎÊÏ£¬ÎÒµÄÊÇpython2.5°²×°ÁËPILÄ£¿é£¬È»ºó±àд³ÌÐò´ò¿ª±¾»úÉϵÄÒ»¸öͼƬʱ£¬ÎªÊ²Ã´ÀϳöÏÖ´íÎóÄØ£¬ÀÏÊÇÕÒ²»µ½Í¼Æ¬£¬´íÎóÌáʾÈçÏ£ºFile "C:\Python25\Lib\site-packages\PIL\Image.py", line 1888, in o ......
ËÍƼöÒ»±¾Ñ§Ï°PYTHONµÄÊ飬лл
¡¶python¼òÃ÷½Ì³Ì¡·£¬¡¶pythonºËÐıà³Ì¡·
×÷Ϊϵͳ¹ÜÀí·½Ã棬¡¶Python UNIXºÍLinuxϵͳ¹ÜÀíÖ¸ÄÏ¡·ÊDZ¾·Ç³£²»´íµÄÊé¡£
http://club.book.csdn.net/pic3/255142.jpg
ÒýÓÃ
×÷Î ......
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,fff777ggg'
ÓÃÕýÔò±í´ïʽȡ³ö Ç°ºó×ÖĸÏàͬµÄÊý¾Ý ½á¹ûÈçÏÂ:
111 ddd
лл~
Python code:
import re
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,ff ......