如何在Python中执行以下操作?
row = [unicode(x.strip()) for x in row if x is not None else '']
Run Code Online (Sandbox Code Playgroud)
实质上:
我正在做一些字符串解析,如果字符是字母则要返回1,如果字符是数字则返回2,如果字符是其他字符则返回.
通常我会使用for循环并像这样附加到它
string = 'hello123...'
values = []
for char in string:
if char.isalpha():
values.append(1)
elif char.isdigit():
values.append(2)
Run Code Online (Sandbox Code Playgroud)
返回
[1, 1, 1, 1, 1, 2, 2, 2]
Run Code Online (Sandbox Code Playgroud)
正如所料.根据/sf/answers/2117182581/使用列表推导可以快得多.所以我尝试过:
values = [1 if char.isalpha else 2 if char.isdigit for char in string]
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个语法错误,因为'else'是预期的.
File "C:/Users/test3.py", line 12
values = [1 if char.isalpha else 2 if char.isdigit for char in string]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
如果角色不是字母数字,我希望它不添加任何内容.我的代码出了什么问题?
我将不得不执行这个功能数十亿次,所以如果有更好的方法可以做到这一点,那么效率的提高是值得欢迎的.
给定一个字典流,每个字典都具有唯一的数字 ID,map
基于不线性增加的阈值(<48、<103、 <123...)?我没有广泛研究itertools
或其他有用的迭代库,我想不出比使用elif
s更好的分组方式。
使用 if/elif/else 的示例:
dicts = [{'id':30},{'id':60},{'id':90},{'id':120},{'id':150}]
groups = [[] for _ in range(5)]
for a_dict in dicts:
ID = a_dict['id']
if ID < 50: groups[0].append(ID)
elif ID < 100: groups[1].append(ID)
elif ID < 150: groups[2].append(ID)
elif ID < 200: groups[3].append(ID)
else: groups[4].append(ID)
Run Code Online (Sandbox Code Playgroud)
输出:
>>> print(groups)
[[30], [60, 90], [120], [150], []]
Run Code Online (Sandbox Code Playgroud) 从以下列表理解开始:
new_list = [re.sub(r'replace_a', 'with__b', i)
if not re.findall(r'find_pattern', i, re.M) else
re.sub(r'replace_c', r'with__d', i)
for i in old_list]
Run Code Online (Sandbox Code Playgroud)
现在我想添加一个if条件,else
然后使用替换另一个模式re.sub()
.我尝试过:
new_list = [re.sub(r'replace_a', 'with_b', i)
if not re.findall(r'find_pattern', i, re.M) else
(re.sub(r'replace_c', r'with_d', i)
if foo == re.match("pattern", foo))
for i in old_list]
Run Code Online (Sandbox Code Playgroud)
试图通过列表推导找出正确的方法,但没有运气.
此代码应在O(n)线性时间内找到列表的模式.我想把它变成一个列表理解,因为我在教自己Python,并且我正在努力提高我的列表理解能力.
这些都是提供信息,但没有真正回答我的问题:
我遇到的问题是嵌套if和try/except.我确信这是一个简单的问题,所以初级Python程序员可能很快得到答案.
def mode(L):
# your code here
d = {}; mode = 0; freq = 0
for j in L:
try:
d[j] += 1
if d[j] > freq:
mode = j; freq = d[j]
except(KeyError): d[j] = 1
return mode
Run Code Online (Sandbox Code Playgroud)
请注意,L
参数是这样的int列表:
L = [3,4,1,20,102,3,5,67,39,10,1,4,34,1,6,107,99]
Run Code Online (Sandbox Code Playgroud)
我想的是:
[try (d[j] += 1) if d[j] > freq (mode = j; freq = d[j]) except(KeyError): d[j] = 1 for j in L]
Run Code Online (Sandbox Code Playgroud)
但我没有足够的胶带来修复语法错误的程度.
python list-comprehension try-catch conditional-statements python-2.7
x = [1, 3, 2, 5, 7]
Run Code Online (Sandbox Code Playgroud)
从列表的第一个值开始:
如果下一个值更大,则打印 "\nthe value x is greater than y"
如果下一个值相等,则打印 "\nthe value x is equal to y"
如果下一个值较小,则打印 "\nthe value x is smaller than y"
如何将其转换为精确的Python代码?我实际上正在使用pandas数据框,我只是通过使用列表作为示例来简化它.
x = [1, 3, 2, 5, 7]
Run Code Online (Sandbox Code Playgroud)
根据上面给出的,输出应该是这样的:
the value 3 is greater than 1
the value 2 is smaller than 3
the value 5 is greater than 2
the value 7 is greater than 5
Run Code Online (Sandbox Code Playgroud)