菜鸟小艾 2022-06-18 21:06 采纳率: 75%
浏览 124
已结题

Python:利用集合构造出相同省份的学生名单。

现有一组同学绮梦,冷伊一,香凝,梓轩,零语,圣博;他们的生源地
分别为山东、山西、湖南、内蒙古,山东,山西.
(1)利用集合分别记录学生名单和生源地;
(2)利用集合构造出相同省份的学生名单。

我编写的代码:
student_name = ['绮梦','冷伊一','香凝','梓轩','零语','圣博']
student_name1 = set (student_name)
print (student_name1)
birthplace = ['山东','山西','湖南','内蒙古','山东','山西']
birthplace1 = set (birthplace)
print (birthplace1)
dictionary = dict(zip(student_name,birthplace))
print (dictionary)
for student_name,birthplace in dictionary.items():
dict.setdefault(birthplace,set()).add(student_name)
student = [ len(values) > 1]
printf(student)
需要达成的运行结果:

img


我当前运行的结果:

img

  • 写回答

2条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-06-18 21:43
    关注
    student_name = ['绮梦','冷伊一','香凝','梓轩','零语','圣博']
    student_name1 = set (student_name)
    print (student_name1)
    birthplace = ['山东','山西','湖南','内蒙古','山东','山西']
    birthplace1 = set (birthplace)
    print (birthplace1)
    dictionary = dict(zip(student_name,birthplace))
    print (dictionary)
    a = {}
    for student_name,birthplace in dictionary.items():
        a.setdefault(birthplace,set()).add(student_name)
    print(a)
    student = [a[i] for i in a if len(a[i]) > 1]
    print(student)
    
    {'圣博', '绮梦', '梓轩', '零语', '香凝', '冷伊一'}
    {'湖南', '山西', '内蒙古', '山东'}
    {'绮梦': '山东', '冷伊一': '山西', '香凝': '湖南', '梓轩': '内蒙古', '零语': '山东', '圣博': '山西'}
    {'山东': {'绮梦', '零语'}, '山西': {'圣博', '冷伊一'}, '湖南': {'香凝'}, '内蒙古': {'梓轩'}}
    [{'绮梦', '零语'}, {'圣博', '冷伊一'}]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月27日
  • 已采纳回答 6月19日
  • 创建了问题 6月18日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。