第6题代码
# -*- coding:utf-8 -*-
# 4 6 7 2 1 1 4 4 2 8 3
# 4 6 7 2 1 8 3
from collections import Counter
numCounter = []
newList = []
##输入内容
inputSeries = input('输入一列数,以空格为分割: ')
A = [ int(x) for x in inputSeries.split(' ') ]
#将列表中出现频率大于1的数字添加到临时列表中
for k,v in dict( Counter(A) ).items() :
if v > 1:
numCounter.append(k)
#生成最后结果
for i in A:
if i not in numCounter:
newList.append(i)
else:
if i not in newList:
newList.append(i)
print(newList)
print( ' '.join( [ str(i) for i in newList ] ) )
第7题代码
# -*- coding:utf-8 -*-
import re
def num_to_char(num):
##将文本数字转换为阿拉伯数字
num_dict = {'1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', '0': '零', }
index_dict = {1: '', 2: '十', 3: '百', 4: '千', 5: '万', 6: '十', 7: '百', 8: '千', 9: '亿'}
nums = list(str(num))
if nums == ['0']:
return '零'
nums_index = [x for x in range(1, len(nums) + 1)][-1::-1]
new_str = ''
for index, item in enumerate(nums):
new_str = "".join((new_str, num_dict[item], index_dict[nums_index[index]]))
new_str = re.sub("零[十百千零]*", "零", new_str)
new_str = re.sub("零万", "万", new_str)
new_str = re.sub("亿万", "亿零", new_str)
new_str = re.sub("零零", "零", new_str)
new_str = re.sub("零\\b", "", new_str)
return new_str
# num = int(input(' 请输入0-100之间的一个整数: '))
# print('该数的中文表示是: ',num_to_char(num))
nums = input(' 请输入0-100之间的一列整数,以英文","为分割符: ')
newStr = ''
temp = [ int(i) for i in nums.split(',') ]
for i in temp:
newStr += num_to_char(i) + ','
print('该列数的中文表示是:',newStr.rsplit(',',1)[0])