写一个loopin一行;处理文件路径 [英] write a loopin one line; process file paths

查看:59
本文介绍了写一个loopin一行;处理文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将以下循环压缩成一行?


# - * - 编码:utf-8 - * -

#python


import re,os.path


imgPaths = [u''/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh /DSCN2059m-s.jpg'',

u''/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2062m-s.jpg'',

u''/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2097m-s.jpg'',

u''/ Users / t / web / Periodic_dosage_dir / lanci / t4 /oh/DSCN2099m-s.jpg'',

u''/ Users / t / web / Icons_dir / icon_sum.gif'']


#将图像路径更改为完整大小的图像(如果存在)

#即如果图像以-s.jpg结尾,则找到没有''-s'的图像。

temp = imgPaths [:]

imgPaths = []

for myPath in temp:

p = myPath

(dirName,fileName)= os.path.split(myPath)

(fileBaseName,fileExtension)= os.path.splitext(fileName)

if(re。搜索(R '-s $'',fileBaseName,re.U)):

p2 = os.path.join(dirName,fileBaseName [0:-2])+ fileExtension

如果os.path.exists(p2):p = p2

imgPaths.append(p)


temp = []

print imgPaths


Xah
xa*@xahlee.org

A? http://xahlee.org/

is there a way to condense the following loop into one line?

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg'',
u''/Users/t/web/Icons_dir/icon_sum.gif'']

# change the image path to the full sized image, if it exists
# that is, if image ends in -s.jpg, find one without the ''-s''.
temp=imgPaths[:]
imgPaths=[]
for myPath in temp:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(re.search(r''-s$'',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths.append(p)

temp=[]
print imgPaths

Xah
xa*@xahlee.org
a?? http://xahlee.org/

推荐答案

'',fileBaseName,re.U)):

p2 = os.path.join(dirName,fileBaseName [0:-2])+ fileExtension

如果os.path.exists(p2):p = p2

imgPaths.append(p)


temp = []

print imgPaths


Xah
xa*@xahlee.org

a ?? http://xahlee.org/

'',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths.append(p)

temp=[]
print imgPaths

Xah
xa*@xahlee.org
a?? http://xahlee.org/




Xah Lee写道:

Xah Lee wrote:
有没有办法将以下循环压缩成一行?

# - * - 编码:utf-8 - * -
#python

import re,os.path

imgPaths = [u''/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2059m-s.jpg'',
你'/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2062m-s.jpg'',
你'/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2097m-s.jpg'',
你'/ Users / t / web / Periodic_dosage_dir / lanci / t4 / oh / DSCN2099m- s.jpg'',
你'/ Users / t / web / Icons_dir / icon_sum.gif'']
#将图像路径更改为完整大小的图像(如果存在)
#即,如果图像以-s.jpg结尾,找到一个没有''-s''。
temp = imgPaths [:]
imgPaths = []
对于temp中的myPath:
p = myPath
(dirName,fileName)= os.pat h.split(myPath)
(fileBaseName,fileExtension)= os.path.splitext(fileName)
if(re.search(r'' - s
is there a way to condense the following loop into one line?

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg'',
u''/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg'',
u''/Users/t/web/Icons_dir/icon_sum.gif'']

# change the image path to the full sized image, if it exists
# that is, if image ends in -s.jpg, find one without the ''-s''.
temp=imgPaths[:]
imgPaths=[]
for myPath in temp:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(re.search(r''-s


'',fileBaseName,re.U)):
p2 = os.path.join(dirName,fileBaseName [0:-2])+ fileExtension
如果os.path.exists(p2):p = p2
imgPaths.append(p)

temp = []
打印imgPaths
'',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths.append(p)

temp=[]
print imgPaths




这是有意义的对我来说,因为我想说明功能性的b $ b编程,以及Python的短缺。 (如果你去维基百科

并阅读关于Python的文章,它会自豪地说Python

支持功能编程。一个巨大的他妈的谎言。(更棒的
他妈的出现在Perl的文章中。并且不可能纠正它们,因为有一群无知的白痴,通常是激进的,但不是

害羞地为人类伸张正义。))

在正常的功能编程中,以下循环例如

是一行。为什么?因为在fp中,一个专注于输入&

输出,而不是细节如何完成。所以在这里,一个简单的

读取那里的评论,那就是它。单行代码将以impPaths = f(impPaths)的形式在

中,其中f是在

点上的函数。


因此,在fp中,源代码往往采用

算法单位的段落形式,而不是填写

页面的一百万行代码。


我会的,在一行写下以下内容。但在Python中,这个

是不可能的。 (如果可能的话,可能是非常不寻常的事情和

难以理解)


....在这里我们可能想要说明它的含义是什么意思一个

编程语言支持功能编程。 FP风格,

基本上意味着一个人编写纯函数并将它们应用于

表达式。也就是说,子行程的行为纯粹是输入和输出的
。 (即所谓的没有副作用)但是如果一个以FP风格开发的
节目,就会出现某些编码模式。例如,

a lambda构造,函数排序,

函数的分配和移动,函数的应用等,以及这些语言的自然发展

开发在这个方向上的许多功能,如类型,宏,许多

专门的循环结构(递归,嵌套)和许多其他

功能,其中包含深奥的名称......等等。


所以在这里,如果我们判断Python(或Perl),我们为什么要说它支持

功能编程?仅仅因为可以定义

子程序而没有副作用?还有什么,真正的,功能性的?支持
语言功能?我们真的可以说,一个来自Python的函数编程会发现它合理地支持他的FP编码风格吗?


我想在维基百科Python文章中看到a ??也支持函数programinga ??



http://en.wikipedia.org/wiki/Python_...mming_language


(Perl语言,可以说是支持一些功能性的b $ b编程。(例如那些Schwatchzian等变换,是一个缩影

的功能编程。)但是,Perl不支持

面向对象的编程。)


Perl文章中充满了更多令人震惊的谎言。 (在过去的一年里)

我试图每隔3个月左右修补一下这篇文章,暂时是明显的技术性和非争议性项目的b $ b只是纯粹的b / b
宣传但遭到Perl fuckfaces的否认。我已经写了一个完整的页面批评,但人类的动物问题也轻易地审查了它。

如果您认为我有一点意见,请将此链接添加到Perl页面's
外部链接。
http: //xahlee.org/UnixResource_dir/perlr.html (书评和

对Perl&社区的批评)


Xah xa*@xahlee.org

a ?? http://xahlee.org/



this is of interest to me, because i wanted to illustrate functional
programing, and Python''s short-coming of it. (if you go to wikipedia
and read the article on Python, it will proudly say that Python
supports functional programing. A huge fucking lie. (more fantastic
fucking lies in the Perl article. And it is impossible to correct them,
because there is an army of ignorant morons, usually militant too, not
shy to do justice for humanity with heartiness.) )

In functional programing proper, the following loop for example would
be a single line. Why? because in fp, one is focused on the input &
output and less on the details how it is done. So here, one simply
reads the comment there and that''d be it. The one-line code would be in
the form of impPaths = f(impPaths), where f is a function made on the
spot.

So, in fp the source code tends to take the form of paragraphs of
algorithmic units, as opposed one million lines of code filling the
page.

I would have, written the following in a one-line. But in Python this
is impossible. (if possible, probably something so unusual and
unreadable)

.... Here we may want to make a note on what does it mean to say that a
programing language supports functional programing. The FP style,
basically means that one writes pure functions and apply them to
expressions. That is, subroutines whose behavior are purely that of
input and output. (i.e. so-called without side-effects) But if one
programs in FP style, certain patterns of coding emerges. For example,
a lambda construct, sequencing of functions, assigning and moving of
functions, application of functions etc, and such languages naturally
developed many features in that direction such as types, macros, many
specialized loop constructs (recursion, nesting) and many other
features that goes by abstruse names...etc.

so here, if we judge Python (or Perl), why should we say it supports
functional programing? Simply because it is possible to define
subroutines without side-effects? What else, really, functional
language features does Python support? Can we really say, that a
functional programing coming to Python will find it reasonably supports
his FP coding style?

I would like to see the a??also support functional programinga??
removed in the wikipedia Python article.
( http://en.wikipedia.org/wiki/Python_...mming_language )

(Perl the language, can arguably be said to support some functional
programing. (those Schwatchzian etc Transform for example, is a epitome
of functional programing.) However, Perl does not support
Object-Oriented programing.)

The Perl article is filled with more egregious lies. (In the past year)
I''ve tried to mend the article about every 3 months or so, tentatively
on obvious and technical and non-controversial items that''s purely
propaganda but got denied by the Perl fuckfaces. I have written a full
page of criticism but the human animal fuckers handily censored it too.
If you think i have a point, please add this link to the Perl page''s
external links.
http://xahlee.org/UnixResource_dir/perlr.html (book review and
criticism on Perl & community)

Xah
xa*@xahlee.org
a?? http://xahlee.org/


这篇关于写一个loopin一行;处理文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆