pythonlist包含
Ⅰ 如何在python中使列表只包含不同的元素
list去重
List=[1,2,3,1,4,2]
New=list(set(List))
结果
New=[1,2,3,4]
可以网络一下,还有其他方法
Ⅱ python List里可以包含tupletuple里可以包含list
可以,但不建议这样使用。
元组是不可变变量,包含的list无法删除,只能做元素增加,删除元素,还有个别方法也无法使用。
Ⅲ python 在列表中查找包含所以某个字符串的项,并保存到一个新的列表
# 文件不很大的话:
def findstrinfile(filename, lookup):
return lookup in open(filename,'rt').read()
# 对付大文件:
def findstrinlargefile(filename, lookup):
with open(filename, 'rt') as handle:
for ln in handle:
if lookup in ln:
return True
else:
return False
Ⅳ python的list中包含tuple,如何修改tuple中的元素
tuple是不变的。应当是生成新的tuple项添加了待输出的列表中。
根据规则,分别得到两个List[Tuple],然后合并即可
A = [...]
B = [...]
def b_in_a(b: tuple) ->bool:
....""""判断b子项是否能与A中某项对应"""
....for a in A:
........if a[0]==b[0] and a[1].startswith(b[1]):
............return True
....return False
def gen_item(a: tuple) -> tuple:
...."""生成输出项"""
....for b in B:
........if b_in_a:
............return b + (1,)
....return a[:2] + (0,)
def get_excess() -> list:
...."""得到B中不能与A对应的所有项"""
....return list(map(lambda _: _ + (0,),filter(lambda _: !b_in_a(_),B)))
lst_out = list(map(gen_item, A)) + get_excess()
print(lst_out)
Ⅳ python中的list中多个有包含tuple的list,如何将每个list中的tuple的对应元素相加
我用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),('feel',0),('happy',1),('and',0),('excited',1),('today',0)]]
d={}
forword_listintagged_list:
for(word,score)inword_list:
ifwordind:
d[word]=d[word]+int(score)
else:
d[word]=d.setdefault(word,0)+int(score)
print(*map(tuple,d.items()))
执行结果
('and', 0) ('today', 0) ('I', 0) ('feel', 0) ('excited', 2) ('happy', 2)
第三个就是
Ⅵ python语言中如何直接定义包含若干元素的list
大概是这样:
list=[[Picture('x11'),Picture('x12')...],
[Picture('x21'),Picture('x22')...],
...
]
Ⅶ python中如何判断list中是否包含某个元素
index方法 表示在list中查找元素的位置。没有查找到元素会报错。
count方法 表示在list中查找元素的个数。没有为0
Ⅷ python包含不同长度的list的一维数组用0填充统一长度
matrix=[[1],
[1,2],
[1,2,3],
[1,2,3,4],
[1,2,3,4,5],
[3,4,5],
[2,3,4,5],
]
#现在需要将矩阵中所有的列表长度对齐到最长的列表的长度5,末尾全部用0填充
max_len=max((len(l)forlinmatrix))
new_matrix=list(map(lambdal:l+[0]*(max_len-len(l)),matrix))
print(new_matrix)
Ⅸ python中list表示什么
list是python语言中的基本数据类型列表,使用[]表示;列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表,如下:
ak = [1, '67',true,[23,45,67]]
Ⅹ python怎么判断list是否包含某个元素
a 是你要找的某个元素
b是list
if a in b:
print 'ok'