GVKun编程网logo

如何在列表理解Python中构建两个for循环(如何在列表理解python中构建两个for循环结构)

21

在本文中,我们将详细介绍如何在列表理解Python中构建两个for循环的各个方面,并为您提供关于如何在列表理解python中构建两个for循环结构的相关解答,同时,我们也将为您带来关于python-如

在本文中,我们将详细介绍如何在列表理解Python中构建两个for循环的各个方面,并为您提供关于如何在列表理解python中构建两个for循环结构的相关解答,同时,我们也将为您带来关于python - 如何在for循环中一次访问两个元素而不会在python中重复?、python – 在列表理解中尝试/除外、python – 如何组合两个for循环、python 如何在一个for循环中遍历两个列表的有用知识。

本文目录一览:

如何在列表理解Python中构建两个for循环(如何在列表理解python中构建两个for循环结构)

如何在列表理解Python中构建两个for循环(如何在列表理解python中构建两个for循环结构)

我有两个清单如下

tags = [u''man'', u''you'', u''are'', u''awesome'']entries = [[u''man'', u''thats''],[ u''right'',u''awesome'']]

我想提取物项从entries当他们在tags

result = []for tag in tags:    for entry in entries:        if tag in entry:            result.extend(entry)

如何将两个循环写为单行列表理解?

答案1

小编典典

应该这样做:

[entry for tag in tags for entry in entries if tag in entry]

python - 如何在for循环中一次访问两个元素而不会在python中重复?

python - 如何在for循环中一次访问两个元素而不会在python中重复?

先检查 crosstab,然后检查 dot

s = pd.crosstab(df.Celebrity,df.Username)
s = s.dot(s.T)
out = s.mask(np.triu(np.ones(s.shape)).astype(bool)).stack()
Out[301]: 
Celebrity  Celebrity
B          A            1.0
C          A            1.0
           B            0.0
D          A            2.0
           B            0.0
           C            1.0
dtype: float64
,

首先,函数 num_of_fans_overlap 不应返回 print()

def num_of_fans_overlap(cel1,cel2,data):
    l = [cel1,cel2]
    Res = len(data.loc[data['Usernames'].map(data.groupby('Usernames').agg(set)['Celebrity'].eq(set(l)))])/2
    return int(Res)

其次,如果变量 celebritiesCelebrity 列上的唯一值列表。


from itertools import combinations
celebrities = list(data.Celebrity.unique())
for (cel1,cel2) in combinations(celebrities,2):
   fans_overlap = num_of_fans_overlap(cel1,data)
   print((cel1,fans_overlap))
,

最简单的方法:

celebs = ["A","B","C","D"]
for i in range(len(celebs)):
    for j in range(i+1,len(celebs)):
        celeb_pairs = (celebs[i],celebs[j])
        run_your_function_here(*celeb_pairs,other,parameters)

您也可以使用 itertools.combinations 函数优雅地执行此操作:

import itertools

celebs = ["A","D"]
for celeb1,celeb2 in itertools.combinations(celebs,2):
    run_your_function_here

python – 在列表理解中尝试/除外

python – 在列表理解中尝试/除外

是否可以将以下内容转换为列表解析?

cleaned_list = []
for item in dirtry_list:
    try:
        item.video
    except Video.DoesNotExist:
        pass
    else:
        cleaned_list.append(item)

解决方法

因为根据您的其他问题,您使用的是Django,只需查看 actual field即可.

cleaned_list = [item for item in dirty_list if item.video_id is not None]

python – 如何组合两个for循环

python – 如何组合两个for循环

可以说我正在尝试打印0到25和75到100.
现在我有:

for x in range(0,26):
    print(x)
for x in range(75,101):
    print(x)

有没有办法将这些组合成一个for循环导致:

print(x)

有点像:

for x in range(0,26) and range(75,101):
    print(x)

解决方法

你需要 itertools.chain()

Make an iterator that returns elements from the first iterable until it is exhausted,then proceeds to the next iterable,until all of the iterables are exhausted.

from itertools import chain

for x in chain(range(0,26),range(75,101)):
    print(x)

适用于Python 2和3.

python 如何在一个for循环中遍历两个列表

python 如何在一个for循环中遍历两个列表

是我在看《笨方法学python》过程中发现有一行代码看不懂——“ for sentence in snippet, phrase:”,所以研究了半天,感觉挺有收获的。所以就放在博客上分享给大家了。

直入主题:

为了不耽误大家时间,如果知道以下为两段代码为什么输出不一样的话我觉得您肯定知道我下面要说的是什么了,您就不必花时间再读下去了。

1和2两段代码的区别是print在for循环中,另外一个是不在循环中。输出的结果却截然不同,如果想要弄懂如何遍历两列表,花些时间把下面的几行代码弄懂。我要说的您看起来就会十分简单了。

1.

# for循环两个列表的过程
list1 = [''1'', ''1'']
list2 = [''A'',''B'']

for x in list1, list2:
    reslut = x[:]
    print reslut    # type=list

2.

# for循环两个列表的过程
list1 = [''1'', ''1'']
list2 = [''A'',''B'']

for x in list1, list2:
    reslut = x[:]
print reslut    # type=list

输出的结果分别为

1.

[''1'', ''1'']
[''A'', ''B'']

2.

[''A'', ''B'']

 

好了,您看到这里,我就默认您不会嫌弃我罗嗦了。首先我是个写博客的新手,真是连句通顺能顺畅表达我意思的话,我都要思索再三才能写出来,让我写一篇篇幅足够站住园子首页的文章。我真是捉襟见肘啊,但是我开博客就是为了锻炼自己的文字表达能力的,就算是一点一滴的积累终归有一天我会成长的。不积跬步无以至千里!

扯得有些远了,下面说说python中如何在一个for循环中遍历两个列表

#coding:utf-8
#################
# for循环两个列表的过程
list1 = [''1'', ''1'']
list2 = [''A'',''B'']

for x in list1, list2:
    reslut = x[:]
    print reslut    # type=list

print "---------"

print list1, list2

print "#############"
# 简单版
dir = {''A'': ''a'',
       ''B'': ''b''}
       
xq = dir.keys()
yw = dir.values()

# 这段代码是遍历两个列表之后赋值给新的两个列表
for i in xq, yw:
    answer, question = i[:]    # 这里可以加代码对元素操作之后再赋值给新的列表
    print answer, ",", question  # 你可以把这句放在for循环外面在看下结果

print "---------"
# 复杂版
for j in xq:
    answer_ = j[:]
    print answer_, ",",
    
print "\n"

for k in yw:
    question_ = k[:]
    print question_, ",",
    

运行结果如下:

[''1'', ''1'']
[''A'', ''B'']
---------
[''1'', ''1''] [''A'', ''B'']
#############
A , B
a , b
---------
A , B , 

a , b ,

我贴完代码都不知道要说些什么了,我只能说多动手勤思考,如果一次没想明白的话,不要放弃,过两天再来看看。没准你就豁然开朗了。

以上为我的拙见,本人才疏学浅如有错误欢迎指正。

 

本文分享 CNBlog - 削微寒的程序员之路。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

今天关于如何在列表理解Python中构建两个for循环如何在列表理解python中构建两个for循环结构的分享就到这里,希望大家有所收获,若想了解更多关于python - 如何在for循环中一次访问两个元素而不会在python中重复?、python – 在列表理解中尝试/除外、python – 如何组合两个for循环、python 如何在一个for循环中遍历两个列表等相关知识,可以在本站进行查询。

本文标签: