www.fltk.net > python list 包含元素

python list 包含元素

a 是你要找的某个元素 b是list if a in b: print 'ok'

使用python的列表生成式即可,列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。 代码如下: >>> nl = [i + 1 for i in range(1, 10)]>>> nl[2, 3, 4, 5, 6, 7, 8, 9, 10]>>>

# coding: utf-8 a = [1, 2, 3, 4, 5] b = [3, 4, 5] d = [False for c in b if c not in a] if d: print "a不包含b的所有元素" else: print "a包含b的所有元素"

# -*- coding: utf-8 -*-""":created on: 2015年9月5日:copyright: Nokia Solutions and Networks:author: Chuanqing Qin:contact: chuanqing.qin@nokia.com"""'''[(2.0,2.1),(2.1,2.0),(2.0,2.0),(4.0,4.0),(4.1,4.2),(4.2,4.1),(3.1,2.9)],现...

>>> L=[1,1,2,2,3,3,4,4] >>> len(L) 8 >>> len(set(L)) 4 第二种是计算有多少个不同元素

n = 10a = [1,2,3,4,5]b = a * nprint b#[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, # 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, # 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, # 1, 2, 3, 4, 5]c = []for i in a:for j in range(n...

我用Python3,最后一行稍微改了一下,执行了看,加上了呀 tagged_list = [[('I',0),('feel',0.2),('happy',0.8),('and',0),('excited',0.4),('today',0)],[('I',0),('feel',0),('happy',1),('and',0),('excited',1),('today',0)],[('I',0),('fee...

commonest = [1,2,2,2,1,3,4,5,1,1]print(commonest.count(1))需要把数据存储到列表中,列表的count的方法可以统计某个元素出现的次数

用*运算符 [1,2,3]*3 结果 [1, 2, 3, 1, 2, 3, 1, 2, 3]

可以啊 以下代码调试通过: class Team: # 类的定义 team_name = "" team_years = 0 def __init__(self, name, team_years): self.team_name = name self.team_years = team_years self.wlp = [] def addWLP(self, x): # 类方法 self.wlp.appe...

网站地图

All rights reserved Powered by www.fltk.net

copyright ©right 2010-2021。
www.fltk.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com