当前位置: 首页>>代码示例>>Python>>正文


Python ChainMap.values方法代码示例

本文整理汇总了Python中collections.ChainMap.values方法的典型用法代码示例。如果您正苦于以下问题:Python ChainMap.values方法的具体用法?Python ChainMap.values怎么用?Python ChainMap.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在collections.ChainMap的用法示例。


在下文中一共展示了ChainMap.values方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: expand_rows

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
    def expand_rows(self, apply_extended=True):
        """Generate CIB rows by expanding all CIBs pointing to current CIB """
        paths = self.resolve_graph()

        # for storing expanded rows
        rows = []

        for path in paths:
            expanded_properties = (self.cib[uid].expand() for uid in path)
            for pas in itertools.product(*expanded_properties):
                chain = ChainMap(*pas)

                # For debugging purposes, add the path list to the chain.
                # Store as string to preserve path order (NEAT properties are not ordered).
                dbg_path = '<<'.join(uid for uid in path)

                # insert at position 0 to override any existing entries
                # chain.maps.insert(0, PropertyArray(NEATProperty(('cib_uids', dbg_path))))

                # convert back to normal PropertyArrays
                row = PropertyArray(*(p for p in chain.values()))
                row.meta['cib_uids'] = dbg_path
                rows.append(row)

        if not apply_extended:
            return rows

        if not self.cib.extenders:
            # no extender CIB nodes loaded
            return rows

        # TODO optimize
        extended_rows = rows.copy()
        for entry in rows:
            # TODO take priorities into account
            # iterate extender cib_nodes
            for uid, xs in self.cib.extenders.items():
                for pa in xs.expand():
                    if xs.match_entry(entry):
                        entry_copy = copy.deepcopy(entry)
                        chain = ChainMap(pa, entry_copy)
                        new_pa = PropertyArray(*(p for p in chain.values()))
                        try:
                            del new_pa['uid']
                        except KeyError:
                            pass
                        extended_rows.append(new_pa)

        return extended_rows
开发者ID:NEAT-project,项目名称:neat,代码行数:51,代码来源:cib.py

示例2: combine_map

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
def combine_map():
    a = {'x': 1, 'z': 3 }
    b = {'y': 2, 'z': 4 }
    c = ChainMap(a,b)
    print(c['x']) # Outputs 1 (from a)
    print(c['y']) # Outputs 2 (from b)
    print(c['z']) # Outputs 3 (from a)

    print(len(c))
    print(list(c.keys()))
    print(list(c.values()))

    c['z'] = 10
    c['w'] = 40
    del c['x']
    print(a)
    # del c['y']

    values = ChainMap()
    values['x'] = 1
    # Add a new mapping
    values = values.new_child()
    values['x'] = 2
    # Add a new mapping
    values = values.new_child()
    values['x'] = 3
    print(values)
    print(values['x'])
    # Discard last mapping
    values = values.parents
    print(values['x'])
    # Discard last mapping
    values = values.parents
    print(values['x'])
    print(values)
开发者ID:Barathrum-Liu,项目名称:python3-cookbook,代码行数:37,代码来源:p20_combine_map.py

示例3: ch1_20

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
def ch1_20():
    '''
    1.20 将多个映射合并为单个映射
    使用collections ChainMap解决
    '''

    print("\nch1_20:")

    from collections import ChainMap

    a={'x':1,'z':3}
    b={'y':2,'z':4}

    c = ChainMap(a,b) #使用原始的字典
    print(c['x'],c['y'],c['z'])

    print(list(c.keys()),list(c.values()))

    #用新建字典代替
    merged = dict(b)
    merged.update(a)
    print(merged['x'],merged['y'],merged['z'])
开发者ID:xxg1413,项目名称:python-cookbook,代码行数:24,代码来源:Chapter1.py

示例4: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
# @Author: joshua
# @Date:   2015-05-23 20:06:31
# @Last Modified by:   Joshua Liu
# @Last Modified time: 2015-05-24 19:24:22

from collections import ChainMap

a = {'x': 1, 'z': 3}
b = {'y': 1, 'z': 4}
c = ChainMap(a, b)
print(c)

# 事实上ChainMap并没有真正把多个映射合成一个,而是通过逻辑重新定义了映射的常用操作
print(len(c))
print(list(c.keys()))
print(list(c.values()))

# 如果有重复的键值,将从第一个映射取值,所以c['z'] == a['z'],而不是等于b['z']
# 对合成映射c的操作只影响第一个映射a

c['z'] = 10
c['w'] = 40
del c['x']
print(a)

try:
    # 因为
    del c['y']
except Exception as e:
    print(e)
开发者ID:fooyou,项目名称:Exercise,代码行数:32,代码来源:20_Combine_maps.py

示例5: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
from collections import ChainMap

a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

c = ChainMap(a, b)

print('< x >', c['x'])
print('< y > ', c['y'])
print('< z >: ', c['z'])

print('< Length >', len(c))
print('< Keys >', list(c.keys()))
print('< Values >', list(c.values()))

c['z'] = 10
print('New A', a)
开发者ID:guidiego,项目名称:python-cookbook-examples,代码行数:19,代码来源:problem20.py

示例6: print

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
  Python 3.3 or greater is needed. Python 3.2 does not have ChainMap
"""

"""
Discussion:
-----------

  A Chainmap takes multiple mappings and makes them logically appear
as one. However, the mappings are not literally merged together. Instead,
a ChainMap simply keeps a list of the underlying mappings and redefines common
dictionary operations to scan the list. Most operations will work. For example:
"""

print("\n\nlen(c): ", len(c))
print("\n\nlist(c.keys()): ", list(c.keys()))
print("\n\nlist(c.values()): ", list(c.values()))

"""
  If there are duplicate keys, the values from the first mapping get
used. Thus, the entry c['z'] in the example would always refer to the
value in dictionary a, not the value in dictionary b.

  Operations that mutuate the mapping always affect the first mapping
listed.
"""

"""
  As an alternative to ChainMap, you might consider merging dictionaries
together using the update() method. For example:
"""
开发者ID:crazcalm,项目名称:Python_cookbook_favorites,代码行数:32,代码来源:OnePointTwenty.py

示例7: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
# Przykład ilustrujący łączenie słowników w obiekt typu ChainMap

a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }

# (a) Prosty przykład ilustrujący łączenie
from collections import ChainMap
c = ChainMap(a,b)
print(c['x'])      # Zwraca 1  (z a)
print(c['y'])      # Zwraca 2  (z b)
print(c['z'])      # Zwraca 3  (z a)

# Wyświetlanie typowych atrybutów
print('len(c):', len(c))
print('c.keys():', list(c.keys()))
print('c.values():', list(c.values()))

# Modyfikowanie wartości
c['z'] = 10
c['w'] = 40
del c['x']
print("a:", a)


# Tworzenie kilku poziomów odwzorowań (przypomina działanie zasięgu)
values = ChainMap()
values['x'] = 1

# Dodawanie nowego odwzorowania
values = values.new_child()
values['x'] = 2
开发者ID:anpadoma,项目名称:python_receptury3,代码行数:33,代码来源:example.py

示例8: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
__author__ = 'Hernan Y.Ke'
from collections import ChainMap

a = {
    'A': 100,
    'B': 10}
b={
    'B': 20,
    'D': 50
}

c = ChainMap(a,b)   # Check first map first Do c['B']=10
print(list(c.keys()))  # ABD
print(list(c.values()))    #100 10 50
#del c['D']  #can not delete key in first map

#others.  new_child merge, update

val = ChainMap()
val['x']=1
val = val.new_child()
val['x']=2
val = val.new_child()
val['x']=3
print(val)
print(val['x'])

val=val.parents
print(val['x'])
val=val.parents
print(val['x'])
开发者ID:rengokantai,项目名称:orpycok3ed,代码行数:33,代码来源:L1_20_combinemap_ChainMap.py

示例9: scan

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]

#.........这里部分代码省略.........
        self.AddToList(elm)

    def AddDependent(self,name,block,code,func=None,variables=None,value=None):
        variable_list=[]
        try:# replace parameter names in variables with parameter objects,
            # then collect them into variable_list
            for var in variables:
                if type(var) is str:
                    var=self.variable_list[var]
                variable_list.append(var)
        except KeyError:
            Error('variable %s do not exist'%var)
        except TypeError:
            pass
        dpd=dependent_scalar(name,block,code,func,variable_list,value)
        self.AddToList(dpd)

    def AddFollower(self,name,block,code,target):
        if type(target) is str:
            try:
                target=self.variable_list[target]
            except KeyError:
                Error('variable %s do not exist'%target)
        while isinstance(target,(follower)):
            target=target.target
        flw=follower(name,block,code,target)
        self.AddToList(flw)
    

    def AddMatrix(self,name,block):
        pass

    def Sample(self,**keys):
        for p in self.free_parameter_list.values():
            p.Generate(**keys)
        return copy.deepcopy(self)
    #========================
    
    def AddMcmcMatrix(self,name,block,shape,free_element={},minimum=None,maximum=None,pace='normal',step_width=None,element_list={}):
        block=block.upper()
        # set one matrix
        mtx=matrix(name,block,shape,element_list={})
        
        if type(free_element) is dict:
            mtx.element_list.update(free_element)
        elif type(free_element) is list or type(free_element) is tuple:
            if all( type(coords) is tuple for coords in free_element ):
                mtx.element_list.update(dict.fromkeys(free_element))

        mtx.minimum=minimum
        mtx.maximum=maximum
        mtx.pace=pace
        if step_width:
            mtx.step_width=step_width
        else:
            try:
                mtx.step_width=(mtx.maximum-mtx.minimum)/100.
            except TypeError:
                Caution(
                    "  Both maximum and minimum of '%s' should be given with pace='%s'"%(name,pace[0])
                +   " if step width is not specified. Later, step width will be 1% of its initial value")

        # add matrix into lists
        if name in self.variable_list.keys():
            Caution("parameter '%s' overridden"%name)
        self.variable_list[name]=mtx
开发者ID:vooum,项目名称:ScanCommander,代码行数:70,代码来源:scan.py

示例10: default

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
def default():
    c = ChainMap(a, b)
    print(len(c), list(c.keys()), list(c.values()))
    print(c['x'], c['y'], c['z'])
开发者ID:Chiva-Zhao,项目名称:pproject,代码行数:6,代码来源:combindict.py

示例11: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
a = {'x' : 1, 'z' : 3}
b = {'y' : 2, 'z' : 4}

'''
现在假设你必须在两个字典中执行查找操作 (比如先从 a 中找,如果找不到再在 b
中找)。
'''
from collections import ChainMap
c = ChainMap(a, b)
print(c['x']) # 1
print(c['y']) # 2
print(c['z']) # 3
print(len(c)) # 3

print(list(c.keys())) # ['x', 'z', 'y']
print(list(c.values())) # [2, 1, 3]

'''
如果出现重复键,那么第一次出现的映射值会被返回。因此,例子程序中的 c['z']
总是会返回字典 a 中对应的值,而不是 b 中对应的值。
'''

'''
对于字典的更新或删除操作总是影响的是列表中第一个字典
'''
c['z'] = 10
c['w'] = 40
del c['x']
print(a) # {'w': 40, 'z': 10}
'''
del c['y']
开发者ID:bodii,项目名称:test-code,代码行数:33,代码来源:20.py

示例12: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
# for the existence of keys.

from collections import ChainMap

a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }

c = ChainMap(a,b) # If there are duplicate keys, the values from the first mapping get used.


print (c['x'])
print (c['y'])
print (c['z'])
# a ChainMap simply keeps a list of the underlying mappings and 
# redefines common dictionary operations to scan the list
print (c, list(c.keys()), list(c.values()), len(c))

# Operations that mutate the mapping always affect the first mapping listed
c['z'] = 10
c['w'] = 40
print (c, a)

a['w'] = 30
print (c, a)

# KeyError: "Key not found in the first mapping: 'y'"
# del c['y']

# ------------------------------------------------------------------------

values = ChainMap()
开发者ID:fluency03,项目名称:Python-Cookbook,代码行数:33,代码来源:1.20.multi_to_single_mapping.py

示例13: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

from collections import ChainMap
c = ChainMap(a, b)

print(c['x'])
print(c['y'])
print(c['z'])
print(c)
print(len(c))
print(c.keys())
print(c.values())

//output
1
2
3
ChainMap({'z': 3, 'x': 1}, {'z': 4, 'y': 2})
3
KeysView(ChainMap({'z': 3, 'x': 1}, {'z': 4, 'y': 2}))
ValuesView(ChainMap({'z': 3, 'x': 1}, {'z': 4, 'y': 2}))

c['z'] = 10
c['w'] = 40

In [28]: c
Out[28]: ChainMap({'z': 10, 'x': 1, 'w': 40}, {'z': 4, 'y': 2})
In [29]: del c['x']
In [30]: a
Out[30]: {'w': 40, 'z': 10}
开发者ID:dataAlgorithms,项目名称:data,代码行数:33,代码来源:dsAlgo_combineMultiMapToSingleMap.py

示例14: ChainMap

# 需要导入模块: from collections import ChainMap [as 别名]
# 或者: from collections.ChainMap import values [as 别名]
# use ChainMap
# find in the order of params
from collections import ChainMap

a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}
c = ChainMap(a, b)
print(c['x'])  # Outputs 1 (from a)
print(c['y'])  # Outputs 2 (from b)
print(c['z'])  # Outputs 3 (from a)

print(len(c))  # 3
list(c.keys())  # ['x', 'y', 'z']
list(c.values())  # [1, 2, 3]
# show first found values
# always change the first dict
c['z'] = 10
c['w'] = 40
del c['x']
print(a)  # {'w': 40, 'z': 10}
# you cant del c['y'] which is in b KeyError

values = ChainMap()
values['x'] = 1
# Add a new mapping
values = values.new_child()
values['x'] = 2
# Add a new mapping
values = values.new_child()
values['x'] = 3
print(values)  # ChainMap({'x': 3}, {'x': 2}, {'x': 1})
开发者ID:rishinkaku,项目名称:PythonCookBook3,代码行数:33,代码来源:ds1_20_union_dicts_mappings.py


注:本文中的collections.ChainMap.values方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。