简体   繁体   English

比较python嵌套列表中列表的第一个元素

[英]Compare 1st element of list from nest list in python

I have a list of lists like following : 我有一个列表列表,如下所示:

[[a1,a2], [b1,b2],....,  [n1]]

and I want to find whether the first elements of all these lists are equal? 我想确定所有这些列表的前几个元素是否相等?

I'd prefer to do this with a list comprehension unless there's a reason to avoid it, for readability's sake. 出于可读性考虑,除非有理由避免使用列表理解,否则我宁愿使用列表理解。

list_of_lists = [[1, 2], [1, 3], [1, 4]]
len(set([sublist[0] for sublist in list_of_lists])) == 1
# True

The solution is quite straight forward. 解决方案非常简单。

  1. Transpose your list. 转置您的列表。 Use zip to perform this task 使用zip执行此任务
  2. Index the first row of your transposed list 索引转置列表的第一行
  3. Use set to remove duplicate 使用集删除重复项
  4. Determine if no of elements is equal to 1 确定元素数是否等于1

>>> test = [[1, 2], [1, 3], [1, 4]]
>>> len(set(zip(*test)[0])) == 1
True

Note 注意

If you are using Py 3.X, instead of slicing, wrap the call to zip with next 如果您正在使用PY 3.X,而不是切片,敷调用zipnext

>>> len(set(next(zip(*test)))) == 1

How about? 怎么样?

>>> from operator import itemgetter
>>> test = [[1, 2], [1, 3], [1, 4]]
>>> len(set(map(itemgetter(0), test))) == 1
True
>>> test.append([2, 5])
>>> test
[[1, 2], [1, 3], [1, 4], [2, 5]]
>>> len(set(map(itemgetter(0), test))) == 1
False

And another way would be (Thanks, Peter DeGlopper!) 还有另一种方式(谢谢彼得·德格洛珀!)

all(sublist[0] == test[0][0] for sublist in test)

This version would short-circuit too, so it wouldn't need to check every element in every case. 这个版本也会短路,因此不需要在每种情况下都检查每个元素。

与第一个子列表的第一个元素相比,您可以创建第一个元素的列表:

False not in [len(yourList[0])>0 and len(x)>0 and x[0] == yourList[0][0] for x in yourList]

With a one liner: 用一个衬板:

>>> sample = [[1, 2], [1, 3], [1, 4]]
>>> reduce(lambda x, y: x if x == y[0] else None, sample, sample[0][0])
1
>>> sample = [[0, 2], [1, 3], [1, 4]]
>>> reduce(lambda x, y: x if x == y[0] else None, sample, sample[0][0])
None

Try this... 尝试这个...

>>> test = [[1, 2], [1, 3], [1, 4]]
>>> eval("==".join(map(lambda x: str(x[0]), test)))
True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中的2D列表中比较第一个元素并操作第二个元素 - How to compare 1st element and operate 2nd element in 2D list in python 比较列表列表中的第一个元素并写入重复值的数量 - Compare 1st element in list of lists and write number of repeated values 在列表中获取相同的第一个元素并将其分配为python中列表的第一个元素 - Taking identical 1st element of a list and assigning it as 1st element of the list in python 从列表列表中删除第一个元素,然后是第一个和第二个元素[保留] - Removing the 1st element then 1st and 2nd element from a list of lists [on hold] Python:如果列表中的第一个元素重复并且第二个元素在列表系列中最低,则删除列表中的列表 - Python: delete list in list if 1st element in list is duplicated and 2nd element is lowest in lists series 比较2D列表的第一个元素并在第二个元素上进行操作 - Compare 1st element of a 2D list and operate on 2nd element 从列表中删除空字符串,Python 中的第一个除外 - Remove empty strings from a list except 1st one in Python Python - 检查元组列表中的第一个元素是否存在于字符串中,如果确实将元组中的第二个元素添加到字符串中 - Python - check if 1st element in list of tuples exists in string, if it does add 2nd element from tuple to string 列表中的第一个元素和python中相同列表的其他元素的组合 - Making combinations of 1st element in list and other elements of the same list in python 循环遍历两个列表以查找第一个列表中的元素是否存在于第二个列表中 - loop through two lists to find if an element from 1st list exists in the 2nd list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM