Python 集合操作

描述

txt = '''Ifthereisonlyoneargument,itmustbeadictionarymappingUnicode |ordinals(integers)orcharacterstoUnicodeordinals,stringsorNone. |Characterkeyswillbethenconvertedtoordinals. |Iftherearetwoarguments,theymustbestringsofequallength,and |intheresultingdictionary,eachcharacterinxwillbemappedtothe |characteratthesamepositioniny.Ifthereisathirdargument,it |mustbeastring,whosecharacterswillbemappedtoNoneintheresult.''' 将以上字符串进行操作处理,编写程序完成统计: (1) 将字符串转换成列表lt;

(2) 输入随机种子x,四个正整数m1、n1、m2和n2,要求: n1 > m1,n2 > m2 且 n1,n2 小于整个列表长度;

(3) 设置随机种子,使用shuffle函数将列表顺序打乱;

(4) 切片列表,设lt1 = lt[m1:n1],lt2 = lt[m2:n2];

(5) 统计并输出lt1,lt2中分别出现了多少个不同的字符;

(6) 输出在lt1和lt2同时出现的字符;

(7) 出现在lt1但没在lt2中出现的字符。说明:为确保和输出用例一致,你需要对筛选出的字符做排序。

样例

输入

2005 5 35 70 76

输出

lt1中出现了17个不同的字符

lt2中出现了6个不同的字符

同时出现在lt1和lt2中的字符为:e g h i

出现在lt1,但没在lt2中的字符为:( I a c f m o p r s t u v

import random

txt = '''Ifthereisonlyoneargument,itmustbeadictionarymappingUnicode |ordinals(integers)orcharacterstoUnicodeordinals,stringsorNone. |Characterkeyswillbethenconvertedtoordinals. |Iftherearetwoarguments,theymustbestringsofequallength,and |intheresultingdictionary,eachcharacterinxwillbemappedtothe |characteratthesamepositioniny.Ifthereisathirdargument,it |mustbeastring,whosecharacterswillbemappedtoNoneintheresult.'''
my_list = list(txt)
x, m1, n1, m2, n2 = map(int, input().split())
random.seed(x)
random.shuffle(my_list)
a_list = my_list[m1: n1]
b_list = my_list[m2: n2]
a_set = set(a_list)
b_set = set(b_list)
a_len = len(a_set)
b_len = len(b_set)
new_set = a_set.intersection(b_set)
new_list = list(new_set)
new_list = sorted(new_list)
new_set1 = a_set.difference(b_set)
new_list1 = list(new_set1)
new_list1 = sorted(new_list1)
print(f"lt1中出现了{a_len}个不同的字符")
print(f"lt2中出现了{b_len}个不同的字符")
print("同时出现在lt1和lt2中的字符为:", end='')
for i in new_list:
    print(i, end=' ')
print()
print("出现在lt1,但没在lt2中的字符为:", end='')
for i in new_list1:
    print(i, end=' ')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值