我希望找到以ing结尾的单词并打印出来,我当前的代码打印出而不是单词.
#match all words ending in ing
import re
expression = input("please enter an expression: ")
print(re.findall(r'\b\w+(ing\b)', expression))
Run Code Online (Sandbox Code Playgroud)
所以如果我们输入一个表达式: sharing all the information you are hearing
我希望['sharing', 'hearing']打印出来,而不是['ing', 'ing']打印出来
有没有快速解决方法?
想象我们有一个 file = open("filetext.txt", 'r')
split()方法和readlines()方法之间有什么区别似乎两条线都分开并将它作为字符串放在列表中.是什么让他们与众不同?
for line in file:
values = line.split() #break each line into a list
file.readlines() #return a list of strings each represent a single line in the file
Run Code Online (Sandbox Code Playgroud) 我是python的新手。具有讽刺意味的是,来自C ++的背景知识使我难以理解这种语言的简单性,更不用说IDLE的工作原理了。
无论如何,我需要编写一个简单的函数,该函数接受一个列表并返回一个新列表,其中的元素被随机排列
这就是我到目前为止
import random
def shuffle():
aList = []
i = 0
for i in range(0, 5):
elements = input(": ")
aList.append(elements)
shuffleList = random.shuffle(aList)
return shuffleList
shuffle()
Run Code Online (Sandbox Code Playgroud)
在我输入了元素(在这种情况下为数字)之后,什么也没有输出。因此,由于某种原因shuffleList并未在其中显示。有任何想法吗 ?
Run Code Online (Sandbox Code Playgroud)>>> : 1 : 2 : 3 : 4 : 5 >>>
我的Sentence类中有contains方法,用于检查单词是否在句子中(在我的情况下是字符串)
我想在我的检查functionTesting,如果hello存在hello world,我得到这个错误,而不是:
AttributeError: 'Sentence' object has no attribute 'contains'
Run Code Online (Sandbox Code Playgroud)
这是我的代码
class Sentence:
def __init__(self, string):
self._string = string
def getSentence(self):
return self._string
def getWords(self):
return self._string.split()
def getLength(self):
return len(self._string)
def getNumWords(self):
return len(self._string.split())
def capitalize(self):
self._string = self._string.upper()
def punctation(self):
self._string = self._string + ", "
def __str__(self):
return self._string
def __getitem__(self, k):
return k
def __len__(self):
return self._String
def __getslice__(self, start, end):
return self[max(0, i):max(0, j):]
def __add__(self, other):
self._string = …Run Code Online (Sandbox Code Playgroud) 我有一个class Sentence神奇的方法功能__getslice__
我不明白如何调用该函数?
我正在尝试切片.所以,例如,如果字符串是"HELLO WORLD, james",并且我在[0:1]切片,我希望得到"HELLO"
我得到了这个错误: 'method' object is not subscriptable
这是我的代码:
class Sentence:
def __init__(self, string):
self._string = string
def getSentence(self):
return self._string
def getWords(self):
return self._string.split()
def getLength(self):
return len(self._string)
def getNumWords(self):
return len(self._string.split())
def capitalize(self):
self._string = self._string.upper()
def punctation(self):
self._string = self._string + ", "
def __str__(self):
return self._string
def __getitem__(self, k):
return self._string[k]
def __len__(self):
return self._String
def __getslice__(self, start, end):
return self[max(0, start):max(0, end):]
def __add__(self, other): …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个列表理解,使用List1创建长度为4的单词列表.
List1 = ['jacob','batman','mozarella']
wordList = [words for i in range(1)]
print(wordList)
Run Code Online (Sandbox Code Playgroud)
这打印出wordList但是长度大于4的单词我正在寻找打印出来的程序:
['jaco','batm','moza']
Run Code Online (Sandbox Code Playgroud)
它们是List1中的相同单词,但长度为4
我尝试过这个并没有用
wordList = [[len(4)] words for i in range(1)]
Run Code Online (Sandbox Code Playgroud)
有什么想法吗 ?
我想在 python 中添加两个分数
如果输入 1/4 + 1/4,我期待 1/2 结果
我用__add__加法方法构建了一个分数类
from fractions import gcd
class fraction:
def __init__(self, numerator, denominator):
self.num = numerator
self.deno = denominator
def __add__(self, other):
self.sumOfn = self.num + other.num
self.sumOfd = gcd(self.deno,other.deno)
return(self.sumOfn, self.sumOfd)
print(fraction(1,4)+fraction(1,4))
Run Code Online (Sandbox Code Playgroud)
但是我得到 2,4 作为输出,实际上是 1/2,只是没有简化。我怎么能解决这个问题?
我试图在长度为4的正则表达式中找到单词
我正在尝试这个,但我得到一个空列表:
#words that have length of 4
s = input("please enter an expression: ")
print(re.findall(r'/^[a-zA-Z]{4}$/',s))
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
我的意见是: here we are having fun these days
我的预期输出: ['here', 'days']
我的输出: []
我有一个编写正则表达式的程序,以匹配最后一个字母以's'结尾的所有单词.然而,我遇到的问题是,它只匹配第一个单词,然后停止.所以,如果我输入表达式:"詹姆斯对我们很好"
我预计
matchObj.group() : 'james'
matchObj.group(1): 'is'
matchObj.group(2): 'us'
Run Code Online (Sandbox Code Playgroud)
但是我只得到 matchObj.group() : 'james'
我相信问题可以追溯到匹配函数的工作方式,是否有解决方法?这是我的代码
import re
matchObj = re.match(r'\w+s', expression, re.M|re.I)
print("matchObj.group() : ", matchObj.group())
print("matchObj.group(1) : ", matchObj.group(1))
print("matchObj.group(2) : ", matchObj.group(2))
Run Code Online (Sandbox Code Playgroud) 我想检查列表是否可以通过执行最多一个交换操作进行排序,如果有,我想返回true.否则,我想回来false.
例如,输入
A = [1,2,3,4,5]
应该导致,true因为它已经排序.
A = [1,2,5,4,3]
应该导致true因为有一个交换,涉及5和3,列表被排序.
A = [1,2,5,3,4]
应该导致false因为它需要多个交换来排序.
我的代码如下.如果A已经排序,则返回true ,但undefined如果未排序但是可以使用一个交换进行排序,则返回true .
有人可以帮我找到问题吗?
function solution(A) {
var count=0;
for(var k=0; k<A.length; k++) //check if sorted
{
if(A[k] <= A[k+1])
count++;
else
break;
}
if (count === A.length - 1) //if already sorted return true
{
return true;
}
for(var i=0; i<A.length; i++) //if not sorted lets check
{
var temp=0;
for(var …Run Code Online (Sandbox Code Playgroud) 我正在尝试打印作为字典中存储的键的行星名称,但是我什么都没有,只是空间
这是我的代码:
class planets:
def __init__(self, aDict):
self.aDict = aDict
def __str__(self):
for key in self.aDict.keys():
print(key)
aDict = {"Sun": 1000, "Mercury": 10, "Earth": 60, "Mars": 50, "jupiter": 100}
p = planets(aDict)
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,有两个函数,外部函数和内部 IIFE 函数。
下面代码的输出是:
outer func: this.foo = bar
outer func: self.foo = bar
inner func: this.foo = undefined
inner func: self.foo = bar
Run Code Online (Sandbox Code Playgroud)
我不明白为什么在内部 IIFE 函数中,this不能用于访问 foo 变量而self仍然可以。我知道这var self是内部函数的全局变量,仍然可以访问。但是呢this?
var myObject = {
foo: "bar",
func: function() {
var self = this;
console.log("outer func: this.foo = " + this.foo);
console.log("outer func: self.foo = " + self.foo);
(function() {
console.log("inner func: this.foo = " + this.foo);
console.log("inner func: self.foo = " + …Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×7
regex ×3
javascript ×2
algorithm ×1
dictionary ×1
list ×1
readfile ×1
sorting ×1
swap ×1