常见问题 - 寻求优雅解决方案 [英] common problem - elegant solution sought

查看:79
本文介绍了常见问题 - 寻求优雅解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在寻找以下微小但常见问题的优雅解决方案。


我有一个清单元组(Unique_ID,Date)都是字符串。

我想用给定的Unique_ID删除元组(元素),但

我不知道相应的日期。


我的直接解决方案有点冗长,例如


L = [(a,070501 ;),(b,080115),(c,071231)]

pos = -1

found = - 1

(L,Key,日期)L:

pos + = 1

如果Key ==" b" :

找到= pos

休息


如果找到> = 0:

del L [找到]


打印L


最有可能有更优雅的解决方案。

不幸的是,索引-list-method没有为比较带来

附加功能参数。


非常感谢您的提示,

Helmut Jarausch


Lehrstuhl fuer Numerische Mathematik

RWTH - 亚琛大学

D 52056亚琛,德国

解决方案

我有一个元组列表(Unique_ID,Date),两者都是字符串。


我想删除具有给定Unique_ID的元组(元素),但

我不知道相应的日期。


我的直接解决方案有点冗长,例如


L = [(a,070501),(" ; b"," 080115"),(" c"," 071231")]



他们必须是元组吗?对我来说,如果你使用Unique_ID

,那么字典就是完美的。事实上当你想要删除一个条目时,你只需要在

字典中查找Unique_ID,它就会非常快速地查找。


如果键入my_dictionary:


但是,如果必须使用元组列表,那么您当前的方法非常好

效率低下。为什么不在if Key ==" b"中使用del:?


我无法为您当前的系统提供非常优雅的解决方案,

但是我可以告诉你一个足够大的列表,你的方式将花费很长时间,因为你将遍历整个列表

顺序找到一个条目...


Helmut Jarausch写道:


我正在寻找以下微小但常见问题的优雅解决方案。


我有一个元组列表(Unique_ID,Date),两者都是字符串。

我想用给定的元组删除元组(元素) Unique_ID,但

我不知道相应的日期。


我的直接解决方案有点冗长,例如

L = [(a,070501),(b,080115),(c,071231)]

pos = -1

found = -1

for(Ke y,日期)在L:

pos + = 1

如果Key ==" b" :

找到= pos

休息


如果找到> = 0:

del L [找到]


打印L


最有可能有更优雅的解决方案。

不幸的是,索引-list-method没有为比较采用

附加函数参数。



Python中最常见的解决方案

是生成第二个包含所有项目的列表
$ b第一个$ b除了你要删除的那个:


< code>

L = [(" a", 070501,(b,080115),(c,071231),

L2 = [(uniqid,date)for(uniqid,日期)在L中如果不是uniqid ==''b'']

< / code>


它可能看起来有点浪费,但是因为Python列表

是非常快的,因为元组本身并没有复制,只有他们的参考,结果可能是你需要的b / b



显然你给了我们一个玩具的例子,这对于证明这个问题很好。建议可能会有所不同,例如,对于

示例,您的数据集要大得多,或者元组

更复杂。


TJG


Helmut Jarausch写道:





我正在寻找以下微小但常见的
问题的优雅解决方案。


我有一个元组列表(Unique_ID,Date)哪个是字符串。

我想用给定的Unique_ID删除元组(元素),但

我不知道相应的日期。


我的直接解决方案有点冗长,例如


L = [(a,070501),(" b", 080115,(c,071231)]

pos = -1

found = -1

for(Key,Date)in L:

pos + = 1

如果Key ==" b" :

找到= pos

休息


如果找到> = 0:

del L [找到]


打印L


最有可能有更优雅的解决方案。

不幸的是,索引-list-method没有为比较带来

附加函数参数。


非常感谢你的提示,


几种解决方案:


- 使用不同的数据结构,正如其他人所建议的那样。

- 使用数据库。例如,SQLite。


- 就地替换列表如下:


L [:] = [(k,d)for k,d in L if k!=" b"]


Diez


Hi,

I''m looking for an elegant solution of the following tiny but common problem.

I have a list of tuples (Unique_ID,Date) both of which are strings.
I want to delete the tuple (element) with a given Unique_ID, but
I don''t known the corresponding Date.

My straight forward solution is a bit lengthy, e.g.

L=[("a","070501"),("b","080115"),("c","071231")]
pos=-1
found=-1
for (Key,Date) in L :
pos+= 1
if Key == "b" :
found= pos
break

if found >= 0 :
del L[found]

print L

Most probably there are much more elegant solutions.
Unfortunately, the index-list-method doesn''t take an
additional function argument for the comparisons.

Many thanks for your hints,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

解决方案

I have a list of tuples (Unique_ID,Date) both of which are strings.

I want to delete the tuple (element) with a given Unique_ID, but
I don''t known the corresponding Date.

My straight forward solution is a bit lengthy, e.g.

L=[("a","070501"),("b","080115"),("c","071231")]

Do they have to be tuples? Seems to me if you are using a Unique_ID
that a dictionary would be perfect. Indeed when you are looking to
delete an entry you can simply look for the Unique_ID in the
dictionary and it will be a very fast look up.

if key in "my_dictionary":

However, if you must use a list of tuples, your current method is very
inefficient. Why not use the del within the if Key == "b":?

I cannot provide extremely elegant solutions with your current system,
but I can tell you with a large enough list your way is going to take
a very long time since you will iterate over the whole list
sequentially to find an entry...


Helmut Jarausch wrote:

I''m looking for an elegant solution of the following tiny but common problem.

I have a list of tuples (Unique_ID,Date) both of which are strings.
I want to delete the tuple (element) with a given Unique_ID, but
I don''t known the corresponding Date.

My straight forward solution is a bit lengthy, e.g.

L=[("a","070501"),("b","080115"),("c","071231")]
pos=-1
found=-1
for (Key,Date) in L :
pos+= 1
if Key == "b" :
found= pos
break

if found >= 0 :
del L[found]

print L

Most probably there are much more elegant solutions.
Unfortunately, the index-list-method doesn''t take an
additional function argument for the comparisons.

Probably the most common solution to this in Python
is to produce a second list which has all the items
in the first except for the one(s) you wish to delete:

<code>
L=[("a","070501"),("b","080115"),("c","071231")]
L2 = [(uniqid, date) for (uniqid, date) in L if not uniqid == ''b'']
</code>

It might look a little wasteful, but since Python lists
are supremely fast and since the tuples themselves aren''t
copied, only their references, the result is probably what
you need.

Obviously you''ve given us a toy example, which is fine for
demonstrating the problem. Suggestions might vary if, for
example, your data set were much bigger or if the tuples
were more complex.

TJG


Helmut Jarausch wrote:

Hi,

I''m looking for an elegant solution of the following tiny but common
problem.

I have a list of tuples (Unique_ID,Date) both of which are strings.
I want to delete the tuple (element) with a given Unique_ID, but
I don''t known the corresponding Date.

My straight forward solution is a bit lengthy, e.g.

L=[("a","070501"),("b","080115"),("c","071231")]
pos=-1
found=-1
for (Key,Date) in L :
pos+= 1
if Key == "b" :
found= pos
break

if found >= 0 :
del L[found]

print L

Most probably there are much more elegant solutions.
Unfortunately, the index-list-method doesn''t take an
additional function argument for the comparisons.

Many thanks for your hints,

Several solutions:

- use a different datastructure, as others suggested

- use a database. SQLite for example.

- replace the list in-place like this:

L[:] = [(k, d) for k, d in L if k != "b"]

Diez


这篇关于常见问题 - 寻求优雅解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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