描述
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=' ')