从字典中弹出完全相同的值

时间:2018-07-12 14:33:18

标签: python python-3.x dictionary

我有一个包含信息的常规列表:

lis = ["Hello", "Hello1", "Test"]

然后我有一个字典,其中包含.c文件的文件路径,因为值和键是lis值的扩展名,例如:

dict1 = {"/****Hello****/":
             ["C://test/hello/one.c",
              "C://test/hello/two.c"],
         "/****Hello1****/":
             ["C://test/helloNext/one.c",
              "C://test/helloNext/two.c"],
         "/****Test****/":
             ["C://test/bye/one.c",
              "C://test/bye/two.c"]}

如果lis的值在键信息中,则想法是用lis的值替换dict1的键。

for x in lis:
    for y in dict1.copy():
        if x in y:
            dict1[x] = dict1.pop(y)

虽然很好,但现在我发现了一个大问题:

如果我这样做,那么dict1的某些信息将被覆盖,因为在此示例中,它搜索其中带有“ Hello”的键,并覆盖dict1 [0],但也将dict1 [1]覆盖为“ Hello” “ Hello1”的名称,因为它是第一个信息...

我自己无法解决问题。 我想我只是有点想念...

谢谢!

PS:这不是从键中切出特殊字符的解决方案,因为对于每个文件,它看起来都不相同。

4 个答案:

答案 0 :(得分:0)

一种方法是根据元素的长度对列表进行排序,并以相反的顺序对其进行处理:

lis = ["Hello", "Hello1", "Test"]

dict1 = {"/****Hello****/": ["C://test/hello/one.c", "C://test/hello/two.c"],
         "/****Hello1****/": ["C://test/helloNext/one.c", "C://test/helloNext/two.c"],
         "/****Test****/": ["C://test/bye/one.c", "C://test/bye/two.c"]}

for x in sorted(lis, key=len, reverse=True):
    for y in dict1:
        if x in y and y not in lis:
            dict1[x] = dict1.pop(y)
            break

print(dict1)
# {'Hello1': ['C://test/helloNext/one.c', 'C://test/helloNext/two.c'], 'Hello': ['C://test/hello/one.c', 'C://test/hello/two.c'], 'Test': ['C://test/bye/one.c', 'C://test/bye/two.c']}

答案 1 :(得分:0)

您可以按降序对第一个列表进行排序,然后创建原始字典的副本,将每个元素从第一个字典移到新字典。

**Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: Can not set javax.ejb.EJBContext field net.ps.ais.system.ejb.SystemServiceBean.ctx to org.jboss.as.ejb3.context.SessionContextImpl
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx (CMTTxInterceptor.java:187)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx (CMTTxInterceptor.java:277)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew (CMTTxInterceptor.java:344)
at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation (LifecycleCMTTxInterceptor.java:66)
at org.jboss.invocation.InterceptorContext.proceed (InterceptorContext.java:340)
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation (CurrentInvocationContextInterceptor.java:41)
at org.jboss.invocation.InterceptorContext.proceed (InterceptorContext.java:340)
at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation (ConcurrentContextInterceptor.java:45)
at org.jboss.invocation.InterceptorContext.proceed (InterceptorContext.java:340)
at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation (ContextClassLoaderInterceptor.java:64)
at org.jboss.invocation.InterceptorContext.proceed (InterceptorContext.java:340)
at org.jboss.invocation.InterceptorContext.run (InterceptorContext.java:356)
at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation (PrivilegedWithCombinerInterceptor.java:80)
at org.jboss.invocation.InterceptorContext.proceed (InterceptorContext.java:340)
at org.jboss.invocation.ChainedInterceptor.processInvocation (ChainedInterceptor.java:61)
at org.jboss.as.ee.component.BasicComponent.constructComponentInstance (BasicComponent.java:161)**

一般的问题都在我的代码以及您的示例中。它会覆盖所有先前的值。因此,我认为这些值应在列表中:

import org.jboss.as.ejb3.context.SessionContextImpl;

    @Resource
    SessionContextImpl ctx;

答案 2 :(得分:0)

**Caused by: java.lang.NullPointerException
    at net.ps.ais.system.ejb.SystemServiceBean.helloWorld(SystemServiceBean.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:254)**

替代:不使用dict2

In [1]:

llist = ["a", "aa", "ab", "c"]
dict1 = {"xax": 1, "xaax": 2, "xabx": 3, "xabcx": 4}
llist.sort(key=len, reverse=True)
dict2 = dict()
for key in dict1:
    success = False
    for replace_token in llist:
        if replace_token in key:
            dict2[replace_token] = dict1.get(key)
            success = True
            break
    if not success:
        dict2[key] = dict1.get(key)

print(dict2)
Out [1]:

{'a': 1, 'aa': 2, 'ab': 4}

输出

In [1]:

from typing import List
llist = ["a", "aa", "ab", "c"]
dict1 = {"xax": 1, "xaax": 2, "xabx": 3, "xabcx": 4}
llist.sort(key=len, reverse=True)
dict2 = dict()
for key in dict1:
    success = False
    for replace_token in llist:
        if replace_token in key:
            current = dict2.get(replace_token, [])
            new = dict1.get(key)
            if isinstance(new, List):
                current.extend(new)
            else:
                current.append(new)
            dict2[replace_token] = current
            success = True
            break
    if not success:
        dict2[key] = dict1.get(key)
print(dict2)
Out [1]:

{'a': [1], 'aa': [2], 'ab': [3, 4]}

答案 3 :(得分:0)

您可以使用re.findall

import re
lis = ["Hello","Hello1","Test"]
dict1 = {"/****Hello****/": ["C://test/hello/one.c", "C://test/hello/two.c"], "/****Hello1****/": ["C://test/helloNext/one.c", "C://test/helloNext/two.c"], "/****Test****/": ["C://test/bye/one.c", "C://test/bye/two.c"]}
_lis = sorted(lis, key=len, reverse=True)
new_dict = {re.findall('|'.join(_lis), a)[0]:b for a, b in dict1.items()}

输出:

{'Hello': ['C://test/hello/one.c', 'C://test/hello/two.c'], 'Hello1': ['C://test/helloNext/one.c', 'C://test/helloNext/two.c'], 'Test': ['C://test/bye/one.c', 'C://test/bye/two.c']}