English 中文(简体)
粉碎:确定长1 v. string
原标题:Python: Identifying string list with length of 1 vs. string

In my code I have the following:

if all(requiredField in submittedFields for requiredField in requiredFields):
    # all required fields in submittedFields exist
else:
    # error handling

目的是核对在<代码>上填报的外地的示意图清单是否都出现在<代码>提交的外地上。

在“条码”中,该作品是一份长篇大体的插图;但是,如果你有类似的话,则会处以罚款。

requiredFields = ( single element )

那么,选择权会超越每一种特性,而不是扼杀本身。

因此,我的问题是,除了处理这种问题外,还有更多的老化手段。

try: 
    requiredFields.sort()
    # requiredFields is a list of strings
except AttributeError:
    # requiredFields is a string list whose length == 1
最佳回答

使用假装会提高效率:

submitted_fields = set([ length ,  width ,  color ,  single element ])
required_fields = set([ width ,  length ])
if submitted_fields >= required_fields:
    # all required fields in submittedFields exist
else:
    # error handling

几个优化使这一点迅速:

  • The hash table implementation of sets ensures a high likelihood of a match before doing a character by character equality test.
  • If two strings are identical (the same object in memory), the identity check will bypass a character-by-character equality check.

注 这与你原来的问题一样,是tu。 Steven Rumbalski对此作了很好的阐述。 当然,如果你使用成套设备,就成为一个不问题。

得到你的实地验证的好兆头:-

问题回答

括号里的扼杀不是直观——它是一种扼杀。 a. 做一个项目,需要一个拖车:

>>> ( single element ) # this is not a tuple, it s a string
 single element 
>>> ( single element ,) # note the trailing comma
( single element ,)

欲了解更多信息,请参看Wiki on TupleyntaxStack Overflow question Zar tuple comma syntax Rule

首先,如果你想写上<代码>需要的领域=(单一要素)——如果你想写上<代码>需要填写的外地=(单一要素:)。

但是,假设你不掌握你的意见,检验的最初步方法是:

if isinstance(requiredFields, basestring):
    # requiredFields is a string
else:
    # requiredFields is iterable

为什么不要让你做这样的事情:

required = [ name ,  password ]
submitted = [ name ,  email ]

for field in required:
    if field not in submitted:
        print field +   is required 

这是因为定义

requiredFields = ( single element )

实际等于

requiredFields =  single element 

编制单一要素清单确实如此。

requiredFields = [ single element ]

a 仅作一个部分,确实如此。

requiredFields = ( single element ,)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签