python 怎么去除相邻连续的数字
用列表解析,比如:
b=[1001,1002,1003,1004,1005]
[x for x in b if b.index(x)%2 ==0] 就是你要的结果,实际就是取列表中奇数位的值
请把下面代码转换为python语言的代码,功能为把数字用汉字表示(如1001表示为一千零一)
#coding:gbk
import math
units=["", "十", "", "千", "万", "十", "百", "千", "亿", "十", "百", "千"]
nums=["","一", "二", "三", "四", "五","六", "七", "八", "九"]
def translate(num):
num=str(num)
res=''
for p in xrange(len(num)-1,-1,-1):
r=int(int(num)/math.pow(10,p))
print r,p
res =nums[r] units[p]
for (i,j) in [('零十','零'), ('零百','零'), ('零千','零')]:
res=res.replace(i,j)
while res.find('')!=-1:
res=res.replace('零零','零')
for (i,j) in [('零万','万'),('零亿','亿')]:
res=res.replace(i,j)
res=res.replace('亿万','亿')
if res.startswith('一十'):
res=res[2:]
if res.endswith('零'):
res=res[:-2]
return res
print(translate('1000000010'))
好丑的代码……
如何利用python语言中list和range结合起来使用
使用举例:
nums = [] #定义一个list
for i in range(1, 1001): //使用range的值循环加到nums中
nums.append(i)
python怎么比较字典中的元素
i={1:"192.168.0.1:1000",2:"192.168.0.1:1000",3:"192.168.0.1:1000",4:"192.168.0.1:1001",5:"192.168.0.1:1001",6:"192.168.0.1:1002",7:"192.168.0.1:1003"}
for f in range(1,8):
a=i[f]
for w in range(2,8):
b=i[w]
if a==b and f!=w:
print f,w
的作一典
版权声明:本站所有文章皆为原创,欢迎转载或转发,请保留网站地址和作者信息。