Demon 发表于 2017-12-28 21:05

如何把list中有关9的字掉


请问如何把一个list 中有关9的字掉如:A = [‘249’,’9598’,’256.9’]
拿掉后A = [‘24’,’58’,’256’]

pemako 发表于 2017-12-29 15:42

b = []
for x in a:
        if '.' in x:
                _t = re.sub('\.', '', x)
                b.append(re.sub('9','',_t))
        else:
                b.append(re.sub('9','', x))

print(b)
>>>['24', '58', '256']

pemako 发表于 2017-12-29 15:42

b = []
for x in a:
        if '.' in x:
                _t = re.sub('\.', '', x)
                b.append(re.sub('9','',_t))
        else:
                b.append(re.sub('9','', x))

print(b)
>>>['24', '58', '256']

pemako 发表于 2017-12-29 15:42

b = []
for x in a:
        if '.' in x:
                _t = re.sub('\.', '', x)
                b.append(re.sub('9','',_t))
        else:
                b.append(re.sub('9','', x))

print(b)
>>>['24', '58', '256']

pemako 发表于 2017-12-29 15:43

b = []
for x in a:
        if '.' in x:
                _t = re.sub('\.', '', x)
                b.append(re.sub('9','',_t))
        else:
                b.append(re.sub('9','', x))

print(b)
>>>['24', '58', '256']

Demon 发表于 2017-12-29 19:06

pemako 谢谢

Demon 发表于 2017-12-30 18:31

续上面的问题
如果依次加起来
如4+2 =6
   5+8 = 13
   2+5+6 = 13
A = [ ‘6’,’13’,’13]


我用map(int,b)
得到
invalid literal for int() with base 10: ''

但用int(map(float,b))
却说could not convert string to float:

所以无法用依次轮询方式去做加减



List A 原先是string 做成数字加减,有法办到吗?

Perl_Er 发表于 2017-12-30 20:50

回复 1# Demon

list = [ re.sub(r'9','', s) for s in A ]

Perl_Er 发表于 2017-12-30 20:51

回复 1# Demon

list = [ re.sub(r'9','', s) for s in A ]

Demon 发表于 2017-12-30 23:01

Perl_Er 谢谢,但续我上面的问题
输出是['24', '58', '256']

如果再作处理依次加起来
如4+2 =6
   5+8 = 13
   2+5+6 = 13
A = [ ‘6’,’13’,’13]

因为list是string,要如何做加减?
页: [1] 2 3
查看完整版本: 如何把list中有关9的字掉