小编mar*_*rsx的帖子

Python字典迭代

我有一本字典dict2,我想通过它删除所有包含某些ID号的条目idlist. dict2[x]是一个列表列表(参见下面的示例dict2).这是我到目前为止编写的代码,但是它不会删除所有ID(entry[1])的实例idlist.有帮助吗?

dict2 = {G1:[[A, '123456', C, D], [A, '654321', C, D], [A, '456123', C, D], [A, '321654', C, D]]}

idlist = ['123456','456123','321654']
for x in dict2.keys():
    for entry in dict2[x]:
        if entry[1] in idlist:
            dict2[x].remove(entry)
        if dict2[x] == []:
            del dict2[x]
Run Code Online (Sandbox Code Playgroud)

dict2 应该看起来像这样:

dict2 = {G1:[[A, '654321', C, D]]}
Run Code Online (Sandbox Code Playgroud)

python dictionary

21
推荐指数
2
解决办法
10万
查看次数

python中的酸洗错误?

我收到这个错误,我不知道这意味着什么.我该如何解决这个问题?

我的代码看起来像这样,我以前使用它并且它已经工作:

parentdir = os.getcwd()
dirlist = os.listdir(parentdir)

for dir in dirlist:
    if not dir == "pubs_edits": continue
    if os.path.isdir(os.path.join(parentdir, dir)):
                        os.chdir(os.path.join(parentdir, dir))
                        file_list = os.listdir(os.path.join(parentdir, dir))
                        for f in file_list:
                            in1 = open(f, 'r')
                            dict2 = pickle.load(in1)
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

    File "/home/md202/pmid_editor.py", line 18, in <module>
        dict2 = pickle.load(in1)
    File "/usr/lib/python2.5/pickle.py", line 1370, in load
        return Unpickler(file).load()
    File "/usr/lib/python2.5/pickle.py", line 858, in load
        dispatch[key](self)
KeyError: '\x00'
Run Code Online (Sandbox Code Playgroud)

python pickle

15
推荐指数
2
解决办法
2万
查看次数

python字典值排序

我有2个字典,dict1并且dict2其中包含相同的密钥,但是不同的值的密钥.我想要做的是为每个字典,从最大到最小的值排序,然后给每个值一个等级1-N,1是最大的值.从这里开始,我希望得到相同键的每个字典中值的等级差异.例如:

dict1 = {a:0.6, b:0.3, c:0.9, d:1.2, e:0.2}
dict2 = {a:1.4, b:7.7, c:9.0, d:2.5, e:2.0}

# sorting by values would look like this:
dict1 = {d:1.2, c:0.9, a:0.6, b:0.3, e:0.2}
dict2 = {c:9.0, b:7.7, d:2.5, e:2.0, a:1.4}

#ranking the values would produce this:
dict1 = {d:1, c:2, a:3, b:4, e:5}
dict2 = {c:1, b:2, d:3, e:4, a:5}

#computing the difference between ranks would be something like this:
diffs = {}
for x in dict1.keys():
    diffs[x] = …
Run Code Online (Sandbox Code Playgroud)

python sorting dictionary

9
推荐指数
2
解决办法
9902
查看次数

在列表中获得所有可能的组合

假设我有这样的事情:

L1=['cat', 'dog', 'fish', 'rabbit', 'horse', 'bird', 'frog', 'mouse'...]

for x in L1:
    input1= open('file_%s'%(x), 'r')
    file1= pickle.load(input1)
    for x in L1:
        input2= open('file_%s'%(x), 'r')
        file2= pickle.load(input2)
Run Code Online (Sandbox Code Playgroud)

我希望获得文件的每个组合而不重复已经完成的组合(一旦完成cat_dog,就不要再执行dog_cat).有没有办法可以做到这一点?我的真实列表按字母顺序排列,如果这有任何区别的话.

python combinations list

7
推荐指数
3
解决办法
6986
查看次数

删除没有值的字典条目 - Python

如果我有一个字典,并且我想删除其中值为空列表的条目,[]我将如何进行此操作?

我试过了:

for x in dict2.keys():
    if dict2[x] == []:
        dict2.keys().remove(x)
Run Code Online (Sandbox Code Playgroud)

但那没用.

python dictionary

7
推荐指数
2
解决办法
1万
查看次数

查找python中日期之间的时差量

假设我有两个看起来像这样的列表:

L1=['Smith, John, 2008,  12,  10,  Male', 'Bates, John,  2006,  1,  Male', 'Johnson, John,  2009,  1,  28,  Male', 'James,  John,  2008,  3,  Male']

L2=['Smith,  Joy, 2008,  12,  10,  Female', 'Smith,  Kevin,  2008,  12,  10,  Male', 'Smith,  Matt,  2008,  12,  10,  Male', 'Smith,  Carol,  2000,  12,  11,  Female', 'Smith,  Sue,  2000,  12,  11,  Female', 'Johnson,  Alex,  2008,  3,  Male', 'Johnson,  Emma,  2008,  3,  Female', 'James,  Peter,  2008,  3,  Male', 'James,  Chelsea,  2008,  3,  Female'] 
Run Code Online (Sandbox Code Playgroud)

我想用它做的是比较一个家庭中每个人(相同的姓氏)与他们每个家庭中的'John'的日期.日期从包括年,月和日,到年和月,再到年.我想找到约翰的约会和他的每个家庭成员之间的差异到我能得到的最具体的一点(如果一个约会全部有3个部分而另一个只有月份和年份,那么只能找到几个月和几年的时差).这是我到目前为止所尝试的,它没有用,因为它没有使用正确的名称和日期(它只给了每个约翰一个兄弟姐妹),它计算日期之间的时间方式令人困惑和错误:

for line in L1:
    type=line.split(',')
    if …
Run Code Online (Sandbox Code Playgroud)

python time list

2
推荐指数
1
解决办法
2015
查看次数

Python布尔帮助!

我有这样的代码:

if (X or Y) == ("Cat" or "Dog" or "Fish" or "Bird"):
    print X, Y
Run Code Online (Sandbox Code Playgroud)

只有这样才有用X == "Cat".有谁知道我的错误吗?

python boolean-operations

2
推荐指数
1
解决办法
206
查看次数

python:在另一个列表中的一个列表中搜索字符串,然后将整个列表条目附加到新列表

例:

L1=['cat', 'dog', 'fish', 'bird', 'rabbit', 'horse']

L2=[('cat', 'a', 'b', 'c'), ('cat', 'c', 'd', 'e'), ('cat', 'e', 'f', 'g'), ('fish', 'x', 'y', 'z'), ('dog', 'w', 'x', 'y'), ('dog', 'z', 'y', 'x'), ('horse', '1', '2', '3'), ('monkey', 'a', 'b', 'c'), ('kitten', 'h', 'i', 'j'), ('bird', '4', '5', '6')]
Run Code Online (Sandbox Code Playgroud)

我试图在L2中搜索L1中的字符串,这样如果L1中的字符串出现在L2的任何部分中,则L2中的整个条目"('cat, a, b, c')"将附加到新列表中.我还认为,从L1中删除没有任何字符串部分的条目可能会有效.我试过了:

def searcher(L1, L2):
    common = []
    for x in L1:
        if re.search(x, L2):
            common.append(L2)

    return common
Run Code Online (Sandbox Code Playgroud)

但那没用.我使用的实际列表要长得多,所以一个高效的代码真的可以帮助我.

谢谢!

python

1
推荐指数
1
解决办法
529
查看次数

使用 %s 更改列表的名称

有没有办法使用 更改列表的名称%s?下面是一个例子:

dict1[key]=value

for x in dict1.keys():
    %s %(x)profile=[]
    if dict1[x]=1:
        %s %(x)profile.append('yes')
Run Code Online (Sandbox Code Playgroud)

此代码没有工作,但我正在寻找的东西,这将使我“N”列表,每一个xdict1.keys()

python dictionary list

0
推荐指数
1
解决办法
4211
查看次数

for循环问题

对于循环问题:

in1 = open('file_1', 'r')
in2 = open('file_2', 'r')
outf = open('out_file', 'w')


for line in in1:
    s = line.split('\t')
    A = s[1][:-1]
    B = s[0]
    counter = 0
    for line in in2:
        ss = line.split('\t')
        if A == ss[0] or A == ss[1]:
            counter += 1
    outf.write('%s\t%s\t%s\n'%(A,B,counter))
Run Code Online (Sandbox Code Playgroud)

问题是它只是通过for line in in2:第一次line in in1.我似乎无法弄清楚为什么.

python for-loop

0
推荐指数
1
解决办法
177
查看次数

排除有多个x的列表

我有一堆看起来像这样的列表:

my_list = [x, y, z, x, z, w]
Run Code Online (Sandbox Code Playgroud)

我想排除有多个'x'的列表,但是允许一个'x',以及重复其他字母.

我不太清楚从哪里开始......

python list

0
推荐指数
1
解决办法
47
查看次数