Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“清理马虎”;至于;使用列表列表在python中循环_Python - Fatal编程技术网

“清理马虎”;至于;使用列表列表在python中循环

“清理马虎”;至于;使用列表列表在python中循环,python,Python,我有一些代码有点草率/重复,我想在确定列表中的值是否与“主列表”匹配后生成一个新字典,返回true或false。我希望使用另一个列表中的标签将这些新的真/假列表附加到字典中。因此,每个新列表(下面的a-d)将具有相同数量的值(每个值为真/假),这些值随后将用作生成翻转图的数据帧索引。示例代码如下所示: a = [] b = [] c = [] d = [] for motif in unique_motifs: if motif in motif_lists[0]: a.

我有一些代码有点草率/重复,我想在确定列表中的值是否与“主列表”匹配后生成一个新字典,返回true或false。我希望使用另一个列表中的标签将这些新的真/假列表附加到字典中。因此,每个新列表(下面的a-d)将具有相同数量的值(每个值为真/假),这些值随后将用作生成翻转图的数据帧索引。示例代码如下所示:

a = []
b = []
c = []
d = []
for motif in unique_motifs:
    if motif in motif_lists[0]:
        a.append('True')
    else:
        a.append('False')               
for motif in unique_motifs:
    if motif in motif_lists[1]:
        b.append('True')
    else:
        b.append('False')   
for motif in unique_motifs:
    if motif in motif_lists[2]:
        c.append('True')
    else:
        c.append('False')       
for motif in unique_motifs:
    if motif in motif_lists[3]:
        d.append('True')
    else:
        d.append('False')

data_dictionary = {'motif_key': unique_motifs, \
                   args.plot_labels[0]: a, \
                   args.plot_labels[1]: b, \
                   args.plot_labels[2]: c, \
                   args.plot_labels[3]: d}
a = ['False', 'True', 'True', 'False', 'False', 'False', 'False']
b = ['True', 'False', 'False', 'False', 'False', 'False', 'False']
c = ['False', 'True', 'True', 'False', 'True', 'False', 'True']
d = ['False', 'False', 'False', 'True', 'False', 'True', 'False']
下面是
motif_list
和主列表
unique_motif
中每个列表的一些示例值:

unique_motifs = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
motif_lists[0] = ['B', 'C']
motif_lists[1] = ['A']
motif_lists[2] = ['B', 'C', 'E', 'G']
motif_lists[3] = ['D', 'F']
然后,上述代码将创建如下新列表:

a = []
b = []
c = []
d = []
for motif in unique_motifs:
    if motif in motif_lists[0]:
        a.append('True')
    else:
        a.append('False')               
for motif in unique_motifs:
    if motif in motif_lists[1]:
        b.append('True')
    else:
        b.append('False')   
for motif in unique_motifs:
    if motif in motif_lists[2]:
        c.append('True')
    else:
        c.append('False')       
for motif in unique_motifs:
    if motif in motif_lists[3]:
        d.append('True')
    else:
        d.append('False')

data_dictionary = {'motif_key': unique_motifs, \
                   args.plot_labels[0]: a, \
                   args.plot_labels[1]: b, \
                   args.plot_labels[2]: c, \
                   args.plot_labels[3]: d}
a = ['False', 'True', 'True', 'False', 'False', 'False', 'False']
b = ['True', 'False', 'False', 'False', 'False', 'False', 'False']
c = ['False', 'True', 'True', 'False', 'True', 'False', 'True']
d = ['False', 'False', 'False', 'True', 'False', 'True', 'False']
然后会被添加到字典中。每个
plot_标签
值都是一个字符串,将用作唯一标识符。我真的想压缩这个代码作为一个奖励,我希望这个字典/生成的列表数量/循环可以根据
len(motif\u列表)
进行扩展(这由用户输入提供的文件数量决定)。已经有了一个检查来确保
len(motif_列表)==len(args.plot_标签
。例如,如果
len(motif_列表)==7
,我会得到上面的列表a、b、c、d、e、f和g。我想有一种方法可以这样做:

for n, val in enumerate(motif_lists):
    globals()["list%d"%n] = []
data_dictionary = {'motif_key': unique_motifs, **{args.plot_labels[i]: bins[i] for i in range(len(args.plot_labels))}}
bins = [[] for _ in range(len(motif_lists))]

然后我会对用户输入值设置一个限制,这样就不会失控…

您可以用以下内容替换重复循环:

for bin, motif_list in zip([a, b, c, d], motif_lists):
    for motif in unique_motifs:
        if motif in motif_list:
            bin.append('True')
        else:
            bin.append('False')
如评论中所述,上述内容可进一步简化为:

for bin, motif_list in zip([a, b, c, d], motif_lists):
    for motif in unique_motifs:
        bin.append(motif in motif_list)
事实上,你可以继续这样做,直到你把上面的内容简化成一个列表,但是可读性可能会受到影响。这取决于你


对于要压缩的其他部分,可以执行以下操作:

for n, val in enumerate(motif_lists):
    globals()["list%d"%n] = []
data_dictionary = {'motif_key': unique_motifs, **{args.plot_labels[i]: bins[i] for i in range(len(args.plot_labels))}}
bins = [[] for _ in range(len(motif_lists))]
其中,
bins
是一个列表,其中包含您的列表
a
b
,等等。例如:

for n, val in enumerate(motif_lists):
    globals()["list%d"%n] = []
data_dictionary = {'motif_key': unique_motifs, **{args.plot_labels[i]: bins[i] for i in range(len(args.plot_labels))}}
bins = [[] for _ in range(len(motif_lists))]


我合并上述词典的方法仅适用于python 3.5以后的版本。有关详细信息,请参见此部分。

我必须指出,但千万不要这样做:
globals()[“列表%d”%n]=[]
,不要使用动态变量。使用容器,如
列表
目录
。无论如何,这可能更适合欢迎使用StackOverflow,并且…在这里应用。StackOverflow是针对特定编程问题的知识库,而不是设计、编码、研究或教程资源。您需要的是一个完整的程序对代码进行efactoring,这有点超出了堆栈溢出的范围,因为它涉及到几种编程技术的指导,您可以在其他地方轻松学习。@juanpa.arrivillaga只是用一个例子来满足我的需要——我不打算在代码中使用动态变量。@MargaretGruca是的,但您有点矛盾说“我最终会得到上面列出的a、b、c、d、e、f和g。”我是说你不需要动态变量。说真的,这是一个非常普遍的问题。从一个新手程序员开始,就是停止考虑“我需要这个和这个变量”到“我需要这个或那个数据结构”@juanpa.arrivillaga很公平——感谢您对代码审查的评论和建议。我认为您的zip参数是反向的。您可以进一步简化这一点;整个if语句可以被调用strbin.append(str(motif\u列表中的motif))@chepner,python真是太棒了。我会添加它,让OP来决定。在这个回答中,我一直在努力提高可读性,这样你就可以摆脱
for
循环的内部
bin.extend(str(motif\u列表中的motif)for motif\u列表中的motif)