【问题标题】:how can i print onto the screen which numbers are the same in each list如何在屏幕上打印每个列表中相同的数字
【发布时间】:2017-04-08 15:46:39
【问题描述】:
List1 = []

称为 List1 随机导入

for n in range(1000):
    y =  random.randrange(0,100000)
    List1.append(y)

    List2 = []

for n1 in range(1000):
    y1 =  random.randrange(0,100000)
    List2.append(y1)

第一个列表,我想在#中看到这些数字

【问题讨论】:

  • 使用Counter...
  • 非常感谢 Willem Van Onsem 编辑问题,使其看起来更干净、更易于阅读
  • 我将如何使用计数器你能给我一个例子吗
  • here
  • 不,我问错了问题,我的意思不是计算出现的次数和项目,而是计算出现的项目

标签: list python-3.x random


【解决方案1】:

您可以使用setscounters

List2 中的数字不在 List1 中:

set_of_different_numbers = set(List2) - set(List1)

List2中数字的出现次数:

from collections import Counter
occurence_count = Counter(List2)

List2 中未出现在 List1 中的数字的出现次数:

occurence_in_disjunction = Counter([l for l in List2 if l in set_of_different_numbers])

【讨论】:

  • 然后我会打印什么来获取屏幕上两个列表中的数字 ted ?
  • “获取两个列表中的数字”是什么意思?您只需打印occurence_in_disjunction 即可查看List2 中不在List1 中的数字出现的次数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多