内置数据结构

2、
print ({ 1 , 2 , 3 , 4 }-{ 3 , 4 , 5 , 6 })
>>
{1, 2}
6、zip( )函数  :将可迭代对象组合
将可迭代的对象作为参数,将对象中 对应的元素 打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表
ziped = list ( zip ([ 1 , 2 ],[ 3 , 4 ]))
print (ziped)
>>
[(1, 3), (2, 4)]
7、鸡蛋
for i in range(3,200):
    if i%2==1:
        j=63*i
        if j%40==1 and j<=10000:
            print(j)
>>
441
2961
5481
8001

8、邮票
#3种邮票面值
a = 6
b = 7
c = 8
#票的张数
t = 50
#放置排列组合数据集
s = []
#生成组合
for i in range (t+ 1 ):
    for j in range (t + 1 ):
        for k in range (t + 1 ):
            s1 = a*i
            s2 = a*i+b*j
            s3 = a*i+b*j+c*k
            s.append(s1)
            s.append(s2)
            s.append(s3)
#排序
s.sort()
news = []
for i in s:
    if i not in news:
        news.append(i)
print ( "组合生成的最大数%s" % news[- 1 ])
#提取不在列表中的数字集合
r = []
for i in range ( 6 *t):
    if i not in s:
        r.append(i)
print ( "组合不能生成的数字%s" %r)
print ( "不能生成的最大数字为%s" %r[- 1 ])
>>
组合生成的最大数1050
组合不能生成的数字[1, 2, 3, 4, 5, 9, 10, 11, 17]
不能生成的最大数字为17
9、word count
#输入字符串
lst = list ( input ( '请输入一行字符,可以是任意字符:' ))

#建立分类,存储字符
iLetter = []
iSpace = []
iNumber = []
iOther = []
#分类、储存
for i in range ( len (lst)):
    if ord (lst[i]) in range ( 65 , 91 ) or ord (lst[i]) in range ( 97 , 123 ):
        iLetter.append(lst[i])
    elif ord (lst[i]) == 32 :
        iSpace.append(lst[i])
    elif ord (lst[i]) in range ( 48 , 58 ):
        iNumber.append(lst[i])
    else :
        iOther.append(lst[i])
print ( '中英文字母个数:%s' % len (iLetter))
print ( '空格个数:%s' % len (iSpace))
print ( '数字个数:%s' % len (iNumber))
print ( '其他字符个数:%s' % len (iOther))
>>
请输入一行字符,可以是任意字符:JIZHIXUEYUAN is 666 *^*@#$%^&*(!
中英文字母个数:14
空格个数:3
数字个数:3
其他字符个数:12
10、碎片平均长度
#10、碎片平均长度
#输入
num = input ( '请输入一行字符:' )
result = []
size = len (num) - 1
count = 1
#统计碎片、计次
for i in range (size):
    if num[i] == num[i+ 1 ]:
        count += 1
    else :
        result.append(count)
        count = 1
        result.append(count)
print ( '所有碎片的平均长度=%0.2f' %( sum (result)/ len (result)))
>>
请输入一行字符:JIZHIXUEYUAN666
所有碎片的平均长度=1.15

11、 字符串字符统计、位置输出
#11、字符串字符统计、位置输出
s = input ( '请输入一段字符串:' )
letter_count_dict = {}
for i in s:
    #判断是否在字典中出现过
    if i in letter_count_dict:
        letter_count_dict[i] += 1
    #没有出现过
    else :
        qletter_count_dict[i] = 1
print ( 'letter_count_dict:%s' %letter_count_dict)

#所有出现次数列出来,max函数去最大值
max_letter_occurrence = max (letter_count_dict.values())
print ( 'max_letter_occurrence:%s' %max_letter_occurrence)
#存储可能的1个或多个最大可能
max_occurrence_letters = []
for k,v in letter_count_dict.items():
    #值=出现最大次数时
    if v ==max_letter_occurrence:
        #满足最大的存于列表中
        max_occurrence_letters.append(k)

#遍历、查询输出位置信息
for i in max_occurrence_letters:
    #定义存储位置的列表
    max_occurrence_letter_positions = []
    #从i位置开始找
    start_position = 0
    #从启始字符串位置找到结束位置
    while 1 :
        #该位置有
        if s.find(i,start_position)!=- 1 :
            max_occurrence_letter_positions.append(s.find(i,start_position))
            #位置+1继续查找
            start_position = s.find(i,start_position)+ 1
            #当已查不到目标字母,说明所有字母都找到了
        else :
            print ( '%s positions:%s' %(i,max_occurrence_letter_positions))
            break
>>
请输入一段字符串:JIZHIXUEYUAN666
letter_count_dict:{'J': 1, 'I': 2, 'Z': 1, 'H': 1, 'X': 1, 'U': 2, 'E': 1, 'Y': 1, 'A': 1, 'N': 1, '6': 3}
max_letter_occurrence:3
6 positions:[12, 13, 14]

猜你喜欢

转载自blog.csdn.net/sdhotn/article/details/80323315