简体   繁体   中英

Find exact string in tuple

I am using python to access results from another program. The program has a specific module in order to so. Unfortunately I do not understand the tuple format ("stuff") that comes out as a result.

I am familiar with looking up keys/values in dictionaries, but not how this would work. I am new to tuples and I am using 2.7.5 - any help would be great!

>>> import OrcFxAPI
>>> model = model.OrcFxAPI('C:\#17.sim')
>>> stuff = model.objects
>>> print stuff
(<General Data: 'General'>, <Environment Data: 'Environment'>, <Line Contact Data: 'Line Contact Data'>,  <Vessel: 'vesselA'>, <Vessel: 'vesselB'>, <Line: 'A'>, <Line: 'B'>, <Line: 'C'>, <Line: 'D'>, <Line: 'E'>, <Line: 'F'>)

>>> print type(stuff)
<type 'tuple'>

>>> print map(type, stuff)
[<class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexVesselObject'>, <class 'OrcFxAPI.OrcaFlexVesselObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>]

I want to lookup instances of Line, and output 'A','B','C','D','E','F':

>>> for thing in stuff:      
        if isinstance(thing, Line) and thing.name == 'C':          
            line_c = thing 
            break
        else: 
            raise ValueError('No Line C in stuff!')  

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'Line' is not defined

It's hard to tell from your vague question, but I think what you want is something like this:

for thing in stuff:
    if isinstance(thing, OrcFxAPI.OrcaFlexLineObject):
        print thing.name,
print

Or, equivalently:

lines = (thing for thing in stuff 
         if isinstance(thing, OrcFxAPI.OrcaFlexLineObject))
names = (line.name for line in lines)
print ' '.join(names)

If you're looking for the Line object that has a specific name, that's just as easy:

for thing in stuff:
    if isinstance(thing, OrcFxAPI.OrcaFlexLineObject) and thing.name == 'C':
        line_c = thing
        break
else:
    raise ValueError('No Line C in stuff!')

… or:

line_c = next(thing for thing in stuff 
              if isinstance(thing, OrcFxAPI.OrcaFlexLineObject) and thing.name == 'C')

But in general, you don't want to do this like of "type switching" code. It would be better to store this information in some way that kept all the lines separate from all the other things. Maybe this:

{'general': <General Data: 'General'>, 
 'environment': <Environment Data: 'Environment'>, 
 'line contact': <Line Contact Data: 'Line Contact Data'>, 
 'code checks': <Code Checks: 'Code Checks'>, 
 'shear7': <SHEAR7 Data: 'SHEAR7 Data'>, 
 'vessels': (<Vessel: 'vesselA'>, <Vessel: 'vesselB'>), 
 'lines': (<Line: 'A'>, <Line: 'B'>, <Line: 'C'>, <Line: 'D'>, <Line: 'E'>, <Line: 'F'>)
}

And then it would be a matter of just using a dict lookup (which you already know how to do) and iterating over a sequence (which you already know how to do):

for line in stuff['lines']:
    print line.name,
print

Or, maybe even better, use a class instead of a dict, so the object has a lines attribute that you can use like this:

for line in stuff.lines:
    print line.name,
print

OrcFxAPI objects of type OrcaFlexObject and those with that base eg OrcaFlexLineObject have attributes called typeName , Name and type (the upper/lower cases of those is correct and a proper PITA). So, if I understand your question you can do this:

>>> import OrcFxAPI
>>> model = model.OrcFxAPI('C:\#17.sim')
>>> lines = filter(lambda o: o.typeName=="Line",model.objects)
>>> for line in lines:
        print line.Name

A
B
C
D
E
F    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM