重复的模式:我错过了,还是我们可以将这些添加到语言中? [英] Recurring patterns: Am I missing it, or can we get these added to thelanguage?

查看:32
本文介绍了重复的模式:我错过了,还是我们可以将这些添加到语言中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


今天我发现自己再次定义了两个函数,我使用了所有

时间:nsplit和iterable。我的这些小助手功能

在我工作的时候一直都在使用。我不得不定义它们

(但是现在我非常擅长,每个功能不到1个错字!)。

它引出了我以下问题


1.这个功能是否已经内置,我只是错过它

2.我错过了一些众所周知的好技术吗? />
3.插入问题我需要在这里问(回复)


这些是带解释的功能:


def nsplit(s,p,n):

n - = 1

l = s.split(p,n)

if len (l)< n:

l.extend([''''] *(n - len(l)))

返回l


这就像split()但返回一个完全长度的列表。这是

在使用拆包时非常有用,例如:

x,y = nsplit(''foo,bar,baz'','','',2)


def iterable(item,count_str = False):

如果不是count_str和isinstance(item,str):

返回False

试试:

iter(item)

除外:

返回False

返回True

这只是一个简单的布尔测试,判断一个对象是否可以迭代
。我希望在内置中看到这个,以镜像可调用。

可选的count_str增加了字符串处理的灵活性,因为

有时候我需要遍历一个字符串,但通常不。我经常使用它来简化我在这类

costruct中的案件处理:


def foo(bar):

bar = bar if if iterable(bar)else [bar]

for x in bar:

....


感谢您的反馈,

Erich

Hello all,

Today I found myself once again defining two functions that I use all
the time: nsplit and iterable. These little helper functions of mine
get used all the time when I work. Im sick of having to define them
(but am very good at it these days, less than 1 typo per function!).
It leads me to the following questions

1. Is this functionality already built in and im just missing it
2. Is there some well known, good technique for these that I missed?
3. Insert question I need to ask here (with a response)

These are the funtions w/ explaination:

def nsplit(s,p,n):
n -= 1
l = s.split(p, n)
if len(l) < n:
l.extend([''''] * (n - len(l)))
return l

This is like split() but returns a list of exactly lenght n. This is
very useful when using unpacking, e.g.:
x, y = nsplit(''foo,bar,baz'', '','', 2)

def iterable(item, count_str=False):
if not count_str and isinstance(item, str):
return False
try:
iter(item)
except:
return False
return True
This is just simple boolean test for whether or not an object is
iterable. I would like to see this in builtins, to mirror callable.
The optional count_str adds flexibility for string handling, since
sometimes I need to iterate over a string, but usually not. I
frequently use it to simplify my case handling in this type of
costruct:

def foo(bar):
bar = bar if iterable(bar) else [bar]
for x in bar:
....

Thanks for feeback,
Erich

推荐答案

4月15日下午1:51,Erich < sophac ... @ gmail.comwrote:
On Apr 15, 1:51 pm, Erich <sophac...@gmail.comwrote:

大家好,


今天我发现自己再次定义了两个我使用的所有功能

时间:nsplit和iterable。我的这些小助手功能

在我工作的时候一直都在使用。我不得不定义它们

(但是现在我非常擅长,每个功能不到1个错字!)。

它引出了我以下问题


1.这个功能是否已经内置,我只是错过它

2.我错过了一些众所周知的好技术吗? />
3.插入问题我需要在这里问(回复)


这些是带解释的功能:


def nsplit(s,p,n):

n - = 1

l = s.split(p,n)

if len (l)< n:

l.extend([''''] *(n - len(l)))

返回l
Hello all,

Today I found myself once again defining two functions that I use all
the time: nsplit and iterable. These little helper functions of mine
get used all the time when I work. Im sick of having to define them
(but am very good at it these days, less than 1 typo per function!).
It leads me to the following questions

1. Is this functionality already built in and im just missing it
2. Is there some well known, good technique for these that I missed?
3. Insert question I need to ask here (with a response)

These are the funtions w/ explaination:

def nsplit(s,p,n):
n -= 1
l = s.split(p, n)
if len(l) < n:
l.extend([''''] * (n - len(l)))
return l



split()方法有一个maxsplit参数,我觉得它的作用相同

。例如:


The split() method has a maxsplit parameter that I think does the same
thing. For example:


>> temp =''foo,bar,baz''
temp.split('','',1)
>>temp = ''foo,bar,baz''
temp.split('','', 1)



[''foo'',''bar ,baz'']


有关详细信息,请参阅文档:

http://docs.python.org/lib/string-methods.html

[''foo'', ''bar,baz'']

See the docs for more info:

http://docs.python.org/lib/string-methods.html


这就像split(),但返回一个完全长度为n的列表。这是

在使用拆包时非常有用,例如:

x,y = nsplit(''foo,bar,baz'','','',2)


def iterable(item,count_str = False):

如果不是count_str和isinstance(item,str):

返回False

试试:

iter(item)

除外:

返回False

返回True

这只是一个简单的布尔测试,判断一个对象是否可以迭代
。我希望在内置中看到这个,以镜像可调用。

可选的count_str增加了字符串处理的灵活性,因为

有时候我需要遍历一个字符串,但通常不。我经常使用它来简化我在这类

costruct中的案件处理:


def foo(bar):

bar = bar if if iterable(bar)else [bar]

for x in bar:

....


感谢您的反馈,

Erich
This is like split() but returns a list of exactly lenght n. This is
very useful when using unpacking, e.g.:
x, y = nsplit(''foo,bar,baz'', '','', 2)

def iterable(item, count_str=False):
if not count_str and isinstance(item, str):
return False
try:
iter(item)
except:
return False
return True
This is just simple boolean test for whether or not an object is
iterable. I would like to see this in builtins, to mirror callable.
The optional count_str adds flexibility for string handling, since
sometimes I need to iterate over a string, but usually not. I
frequently use it to simplify my case handling in this type of
costruct:

def foo(bar):
bar = bar if iterable(bar) else [bar]
for x in bar:
....

Thanks for feeback,
Erich



不确定另一个,但你可能会看看itertools。 br />

Mike

Not sure about the other one, but you might look at itertools.

Mike


4月15日下午1点51分,Erich< sophac ... @ gmail.comwrote:
On Apr 15, 1:51 pm, Erich <sophac...@gmail.comwrote:

大家好,


今天我发现自己再一次定义了我使用的两个函数

时间:nsplit和iterable。我的这些小助手功能

在我工作的时候一直都在使用。我不得不定义它们

(但是现在我非常擅长,每个功能不到1个错字!)。

它引出了我以下问题


1.这个功能是否已经内置,我只是错过它

2.我错过了一些众所周知的好技术吗? />
3.插入问题我需要在这里问(回复)


这些是带解释的功能:


def nsplit(s,p,n):

n - = 1

l = s.split(p,n)

if len (l)< n:

l.extend([''''] *(n - len(l)))

返回l


这就像split()但返回一个完全长度的列表。这是

在使用拆包时非常有用,例如:

x,y = nsplit(''foo,bar,baz'','','',2)


def iterable(item,count_str = False):

如果不是count_str和isinstance(item,str):

返回False

试试:

iter(item)

除外:

返回False

返回True

这只是一个简单的布尔测试,判断一个对象是否可以迭代
。我希望在内置中看到这个,以镜像可调用。

可选的count_str增加了字符串处理的灵活性,因为

有时候我需要遍历一个字符串,但通常不。我经常使用它来简化我在这种类型的案件处理中的操作

costruct:
Hello all,

Today I found myself once again defining two functions that I use all
the time: nsplit and iterable. These little helper functions of mine
get used all the time when I work. Im sick of having to define them
(but am very good at it these days, less than 1 typo per function!).
It leads me to the following questions

1. Is this functionality already built in and im just missing it
2. Is there some well known, good technique for these that I missed?
3. Insert question I need to ask here (with a response)

These are the funtions w/ explaination:

def nsplit(s,p,n):
n -= 1
l = s.split(p, n)
if len(l) < n:
l.extend([''''] * (n - len(l)))
return l

This is like split() but returns a list of exactly lenght n. This is
very useful when using unpacking, e.g.:
x, y = nsplit(''foo,bar,baz'', '','', 2)

def iterable(item, count_str=False):
if not count_str and isinstance(item, str):
return False
try:
iter(item)
except:
return False
return True
This is just simple boolean test for whether or not an object is
iterable. I would like to see this in builtins, to mirror callable.
The optional count_str adds flexibility for string handling, since
sometimes I need to iterate over a string, but usually not. I
frequently use it to simplify my case handling in this type of
costruct:



刚发现这个主题:nofollowhref = _blank> http://mail.python.org/pipermail/pyt...ly/394487.html


这可能会回答你的问题。

Just found this thread on the subject:

http://mail.python.org/pipermail/pyt...ly/394487.html

That might answer your question.


>

def foo(bar):

bar = bar if if iterable(bar)else [酒吧]

for x in bar:

....


感谢您的反馈,

Erich
>
def foo(bar):
bar = bar if iterable(bar) else [bar]
for x in bar:
....

Thanks for feeback,
Erich



Mike


Mike


Erich schrieb:
Erich schrieb:

这就像split()但返回一个精确长度的列表。这是

在使用拆包时非常有用,例如:

x,y = nsplit(''foo,bar,baz'','','',2)
This is like split() but returns a list of exactly lenght n. This is
very useful when using unpacking, e.g.:
x, y = nsplit(''foo,bar,baz'', '','', 2)



您可以使用拆分的第二个参数:


x,y =''foo,bar,baz''。 ('','',1)


请注意,该数字的含义为仅拆分n次。而不是

分成n部分。


干杯,

Robin

You could use the second argument of split:

x, y = ''foo,bar,baz''.split('','', 1)

Note that the number has the meaning "only split n times" as opposed to
"split into n parts".

Cheers,
Robin


这篇关于重复的模式:我错过了,还是我们可以将这些添加到语言中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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