【发布时间】:2015-11-28 18:50:57
【问题描述】:
我不了解 Python(C 人)中的基本概念,可以使用 a) 答案 b) 解释
def do_the_deed(srctxt, upperlower)
# srctxt = "XGID=-b----E-C---eE---c-e----B-:0:0:1:21:0:0:1:0:10"
alpha_list = srctxt[5:31]
# map chars to ascii
# [45],[66],[45],[45]....
ord_list = [map(ord, x) for x in alpha_list]
count = 0
# what I want to do but can not!!!
??? for y = int(x) in ord_list ???
if y <> 45 # acs('-') == 45
if upperlower = 'UPPER'
if ((y>= 97) and (y<= 112)): # uppercase 15 valid
count += y - 96
if upperlower = 'LOWER'
if ((y>=65) and (y<=80)): # lower case 15 valid
count += y - 64
return count
我认为有一个整洁的方法可以做到这一点
xval = [int(x) for x in ord_list[0]]
给我第一个值。
我可以明确地遍历从 0 到 26 的范围,但这似乎是错误的想法。我一直在谷歌搜索,但我不知道要搜索的正确术语。 Iterator、Enumerate、Cast...C 类型的术语没有让我得到正确的答案。
谢谢, 罗伯特
【问题讨论】:
-
试试
for y in [x[0] for x in ord_list] -
我不确定您在这里要做什么,或者您的要求究竟是什么。您的第一个列表理解已经很奇怪,因为它转换为单值整数列表的列表。你为什么要这样做呢?为什么不干脆做
ord_list = [ord(x) for x in alpha_list],然后就可以直接遍历ord_list了? -
你是指一个值
xval = int(ord_list[0])还是列出xval =[int(x) for x in ord_list](没有[0]) -
不要使用
<>进行比较,它很神秘且已弃用。请改用!=。 -
ord_list是整数列表,因此您不需要使用int()
标签: python list foreach casting integer