【发布时间】:2020-04-29 22:06:07
【问题描述】:
我有两个网站的列表,我想数“。”点和“-”连字符 它重复的次数,我想把它分配给空的 for 循环中每次迭代的列表。当我执行下面的代码时 计算点和连字符,但对于每次迭代,它的计数都是以前的 值并增加计数,如 [1,2,3,6,12...] 但我想存储 每个迭代值在列表中分开,如 [1,3,2,5....] 怎么办?
tdots=[]
tphen = []
dots = 0
phen = 0
named_url = ['www.facebook.com','yahoo.com/index-true']
for i in named_url:
for j in i:
if '.' in str(j):
dots = dots + 1
Dots = []
Dots.append(dots)
else:
print('No more "."')
tdots.append(sum(Dots))
for i in named_url:
for j in i:
if '-' in str(j):
phen = phen + 1
Phen = []
Phen.append(phen)
else:
print('No more "-"')
tphen.append(sum(Phen))
print(tdots,'Dots')
print(tphen,'Hyphen')
【问题讨论】:
标签: python-3.x for-loop