可能重复:
在Python中迭代时从列表中删除项目
我正在尝试从python中的列表中删除一个项目:
x = ["ok", "jj", "uy", "poooo", "fren"]
for item in x:
if len(item) != 2:
print "length of %s is: %s" %(item, len(item))
x.remove(item)
Run Code Online (Sandbox Code Playgroud)
但它不会删除"fren"项目.有任何想法吗?
Python文档说:
re.MULTILINE:指定时,模式字符'^'匹配字符串的开头和每行的开头(紧跟在每个换行符之后)...默认情况下,'^'仅匹配字符串的开头...
那么当我得到以下意外结果时会发生什么?
>>> import re
>>> s = """// The quick brown fox.
... // Jumped over the lazy dog."""
>>> re.sub('^//', '', s, re.MULTILINE)
' The quick brown fox.\n// Jumped over the lazy dog.'
Run Code Online (Sandbox Code Playgroud) 我需要获取元素的ID,但值是动态的,只有它的开头始终是相同的.
下面是一段代码.
<form class="form-poll" id="poll-1225962377536" action="/cs/Satellite">
Run Code Online (Sandbox Code Playgroud)
ID始终以'poll-'开头,然后数字是动态的.
如何使用JavaScript而不是jQuery获取ID?
我需要从Python 2.7中的对象列表中删除前n个元素.有没有使用循环的简单方法?
您可能知道,实现__getitem__方法会使类可迭代:
class IterableDemo:
def __getitem__(self, index):
if index > 3:
raise IndexError
return index
demo = IterableDemo()
print(demo[2]) # 2
print(list(demo)) # [0, 1, 2, 3]
print(hasattr(demo, '__iter__')) # False
Run Code Online (Sandbox Code Playgroud)
但是,对于正则表达式匹配对象,这不适用:
>>> import re
>>> match = re.match('(ab)c', 'abc')
>>> match[0]
'abc'
>>> match[1]
'ab'
>>> list(match)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '_sre.SRE_Match' object is not iterable
Run Code Online (Sandbox Code Playgroud)
值得注意的是,该__iter__方法中没有抛出此异常,因为该方法甚至没有实现:
>>> hasattr(match, '__iter__')
False
Run Code Online (Sandbox Code Playgroud)
那么,如何在__getitem__不使类可迭代的情况下实现呢?
我正在运行这个简单的代码:
import threading, time
class reqthread(threading.Thread):
def run(self):
for i in range(0, 10):
time.sleep(1)
print('.')
try:
thread = reqthread()
thread.start()
except (KeyboardInterrupt, SystemExit):
print('\n! Received keyboard interrupt, quitting threads.\n')
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会打印出来
$ python prova.py
.
.
^C.
.
.
.
.
.
.
.
Exception KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
Run Code Online (Sandbox Code Playgroud)
实际上python线程忽略我的Ctrl+ C键盘中断而不打印Received Keyboard Interrupt.为什么?这段代码有什么问题?
我有这段自称的代码:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"a"或"b",一切都很好.输出是:
Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)
但是,如果我输入其他内容然后输入"a"或"b",我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)
我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.
有一个清单:
a = [("ax", 1), ("ec", 3), ("bk", 5)]
Run Code Online (Sandbox Code Playgroud)
另一个清单:
b = ["ec", "ax", "bk"]
Run Code Online (Sandbox Code Playgroud)
我想按照以下方式排序a:
sort_it(a, b)
a = [("ec", 3), ("ax", 1), ("bk", 5)]
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
我知道我们enumerate用于迭代列表,但我在字典中尝试过它并没有给出错误.
码:
enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
for i, j in enumerate(enumm):
print(i, j)
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
0 0
1 1
2 2
3 4
4 5
5 6
6 7
Run Code Online (Sandbox Code Playgroud)
有人可以解释输出吗?
我从用户输入的GUI文本框中读取一个字符串,并通过pandoc处理它.该字符串包含带有反斜杠字符的math的latex指令.我想将字符串作为原始字符串发送到pandoc进行处理.但是像"\ theta"这样的东西变成了一个标签和"heta".
如何将包含反斜杠字符的字符串文字转换为原始字符串...?
编辑:
谢谢develerx,飞羊和unutbu.但是这些解决方案似乎都没有帮助我.原因是还有其他的backslashed-characters在python中没有任何影响但在latex中有意义.
例如'\ lambda'.建议生产所有方法
\\lambda
Run Code Online (Sandbox Code Playgroud)
在乳胶加工中没有经过 - 它应该保持为\ lambda.
另一个编辑:
如果我能得到这项工作,我想我应该通过.@Mark:这三种方法都给出了我不想要的答案.
a='\nu + \lambda + \theta';
b=a.replace(r"\\",r"\\\\");
c='%r' %a;
d=a.encode('string_escape');
print a
u + \lambda + heta
print b
u + \lambda + heta
print c
'\nu + \\lambda + \theta'
print d
\nu + \\lambda + \theta
Run Code Online (Sandbox Code Playgroud) python ×9
list ×3
python-3.x ×2
dictionary ×1
enumerate ×1
events ×1
exception ×1
function ×1
iterable ×1
javascript ×1
performance ×1
python-2.7 ×1
recursion ×1
regex ×1
return ×1
sorting ×1
string ×1
text ×1