例如...
s = 'The fox jumped over a big brown log.'
k = FUNCTION(s,4)
k should be... "The fox jumped over"
Run Code Online (Sandbox Code Playgroud)
我可以编写自己的函数,在空格上拆分,剪切列表然后加入列表.但那是太多的工作.
其他人都知道更简单的方法吗?
我遇到了一个简单的问题.我有一本英语单词词典,还有一个要检查的示例文本.我必须检查示例中的每个单词与字典,我正在使用的代码是错误的.
for word in checkList: # iterates through every word in the sample
if word not in refDict: # checks if word is not in the dictionary
print word # just to see if it's recognizing misspelled words
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,当它通过循环时它打印出每个单词,而不仅仅是拼写错误的单词.有人可以解释这个并提供解决方案吗?非常感谢!
假设有人决定(是的,这很可怕)以下列方式创建句柄输入:用户在导入你的类后在python控制台上键入命令,该命令实际上是一个类名,类名的__str__
函数实际上是一个函数有副作用(例如命令是"north",函数会更改一些全局变量,然后返回描述当前位置的文本).显然这是一个愚蠢的事情,但你会怎么做(如果可能的话)?
请注意,基本问题是如何在__str__
不创建类实例的情况下为类定义方法,否则它会很简单(但仍然同样疯狂:
class ff:
def __str__(self):
#do fun side effects
return "fun text string"
ginst = ff()
>>ginst
Run Code Online (Sandbox Code Playgroud) 我已经提取了一些网址列表,并希望操纵此列表.以下是提取列表示例:
http://help.naver.com/service/svc_index.jsp?selected_nodeId=NODE0000000235
http://www.naver.com/rules/service.html
http://news.naver.com/main/principle.nhn
http://www.naver.com/rules/privacy.html
http://www.naver.com/rules/disclaimer.html
http://help.naver.com/claim_main.asp
http://news.naver.com/main/ombudsman/guidecenter.nhn?mid=omb
http://www.nhncorp.com/
http://www.nhncorp.com/
Run Code Online (Sandbox Code Playgroud)
我想只提取以' http://www.naver.com ' 开头的网址,所以最后我想要的列表如下
http://www.naver.com/rules/privacy.html
http://www.naver.com/rules/disclaimer.html
http://www.naver.com/rules/service.html
Run Code Online (Sandbox Code Playgroud)
我怎样才能提取我想要的东西?
我有一个简单的情况,我有一个用户提供的周数X,我需要找出那周的星期一的日期(例如12月12日).我怎么做到这一点?我知道一年又一周.
如果我有一个列表,并且我想创建一个只包含原始列表的偶数值的列表,我该怎么做?
我最初有:
list1 = [1,2,3,4,5]
list2 = []
for v in list1:
if v % 2 == 0:
list2 += v
print list2
Run Code Online (Sandbox Code Playgroud) 我是一本字典(字典字典)
old_dict = {
'1':{'A':1, 'check': 0, 'AA':2, 'AAA':3 , 'status':0},
'2':{'A':11,'check': 0, 'AA':22, 'AAA':33 ,'status':1},
'3':{'A':111,'check': 0, 'AA':222, 'AAA':333 ,'status':0},
'4':{'A':1111,'check': 1, 'AA':2222, 'AAA':3333 ,'status':0},
}
Run Code Online (Sandbox Code Playgroud)
我想在['check']!= 0之前和['status']之前得到一本新词典!= 0所以将会是
new_dict = {
'1':{'A':1, 'check': 0, 'AA':2, 'AAA':3 , 'status':0},
'3':{'A':111,'check': 0, 'AA':222, 'AAA':333 ,'status':0},
}
Run Code Online (Sandbox Code Playgroud)
如果是列表,我确实喜欢这个
ouputdata = [d for d in data if d[1] == ' 0' and d[6]==' 0']
Run Code Online (Sandbox Code Playgroud)
我试过了
for field in old_dict.values():
if field['check'] !=0 and field['status'] != 0
line = field['A'] + field['AA']+field['AAA']
#write …
Run Code Online (Sandbox Code Playgroud) 我有一些形式的代码:
for i in range(nIterations):
y = f(y)
Run Code Online (Sandbox Code Playgroud)
f
其他地方定义的函数在哪里.希望该代码的想法是,它运行之后y
将 f
适用于它的nIterations
时间.
在python中有一种方法可以将它写在一行中吗?
python ×7
php ×3
date ×1
deprecated ×1
dictionary ×1
list ×1
regex ×1
string ×1
week-number ×1