我有一个字符串,好吧,实际上有几个.字符串很简单:
string.a.is.this
Run Code Online (Sandbox Code Playgroud)
要么
string.a.im
Run Code Online (Sandbox Code Playgroud)
以那种方式.
而我想做的是让那些叮咬成为:
this.is.a.string
Run Code Online (Sandbox Code Playgroud)
和
im.a.string
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
new_string = string.split('.')
new_string = (new_string[3] + '.' + new_string[2] + '.' + new_string[1] + '.' + new_string[0])
Run Code Online (Sandbox Code Playgroud)
哪个适用于制作:
string.a.is.this
Run Code Online (Sandbox Code Playgroud)
成
this.is.a.string
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试的话,会给我一个"超出范围"的错误:
string.a.im
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
new_string = (new_string[2] + '.' + new_string[1] + '.' + new_string[0])
Run Code Online (Sandbox Code Playgroud)
这工作正常:
string.a.im
Run Code Online (Sandbox Code Playgroud)
成
im.a.string
Run Code Online (Sandbox Code Playgroud)
但显然不适用于:
string.a.is.this
Run Code Online (Sandbox Code Playgroud)
因为没有为4个指数设置.我试图弄清楚如何使额外的索引可选,或任何其他工作,或更好的方法.谢谢.
我正在尝试使用切片表示法在列表中设置多个元素.例如,我想分别设置ato 100和的第2和第3个元素200:
a[1:2] = [100, 200]
Run Code Online (Sandbox Code Playgroud)
但这是设置第二个元素100并插入200而不是替换第三个元素.这在Python中是否可行(如果重要的话,使用Python 2.7)?我可以很容易地迭代,但我想要一个更优雅的Pythonic解决方案(即使它可能被认为不太可读).
>>> a=[0]*4
>>> a
[0, 0, 0, 0]
>>> a[1:2] = [100,200]
>>> a
[0, 100, 200, 0, 0]
>>>
Run Code Online (Sandbox Code Playgroud) 看看从我的正则表达式匹配返回的跨度,我注意到它们总是返回超过实际匹配的一个;例如,在正则表达式 HOWTO的示例中
>>> print(p.match('::: message'))
None
>>> m = p.search('::: message'); print(m)
<_sre.SRE_Match object at 0x...>
>>> m.group()
'message'
>>> m.span()
(4, 11)
Run Code Online (Sandbox Code Playgroud)
示例中生成的跨度(4, 11)与实际位置相对应(4, 10)。这给我带来了一些麻烦,因为左右边界具有不同的含义,我需要比较跨度的相对位置。
这样做有充分的理由吗?或者我可以继续根据自己的喜好修改跨度,从正确的边界中减去一个?
我有一个文本字符串.我需要解码文本字符串,即用相应的字符串替换字符串中的每个字符,然后阅读消息.我该如何实现这一目标?
例如:
g fmnc wms bgblr rpylqjyrc gr zw fylb.rfyrq ufyr amknsrcpq ypc dmp.bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle.sqgle qrpgle.kyicrpylq()gq pcamkkclbcb.lmu ynnjw ml rfc spj.
被编码.我需要用c代替a,用d代替b等.我如何在Python中执行此操作?
我更换时出现问题.当我替换所有,我替换被替换的字符,这不是我想要的.我想一次替换一个字符并解码消息.
sentence = "Hello"
print sentence
print sentence[:]
Run Code Online (Sandbox Code Playgroud)
两者输出相同的东西,即 Hello
那么,何时以及为何使用/不使用[:]?
谢谢!:)
我正在寻找分解reverse()函数并将其写在代码中以供练习。我最终弄清楚了如何做到这一点(向后逐步浏览原始列表并附加到新的“反向”列表),但想知道为什么这不起作用。
def reverse(list):
newlist = []
index = 0
while index < len(list):
newlist[index] = list[(len(list)) - 1 - index]
index = index + 1
return newlist
list = [1, 2, 3, 4, 5]
print(reverse(list))
Run Code Online (Sandbox Code Playgroud) 我在python中写了一个简单的程序,检查句子是否是回文.但我无法弄清楚为什么它不起作用.结果总是错误的.有谁知道什么是错的?
def isPalindrome(word):
# Removes all spaces, and lowercase the word.
word = word.strip().lower()
word = word.replace(" ", "")
# If the length of the word is less than 1, means its a palindrome
if (len(word) <= 1):
return True
# Compares the first and the last character of the word.
# If it is the same, calls the function again with the same word,
# without its first and last characters. If its not the same, its
# not …Run Code Online (Sandbox Code Playgroud) 我偶尔使用python; 所以我理解了基本概念; 但今天我遇到了一段代码...我根本不明白:
我一直在寻找一种通过python"查找"的有效方法; 这个问题显示了这个答案:
paths = [line[2:] for line in subprocess.check_output("find . -iname '*.txt'", shell=True).splitlines()]
Run Code Online (Sandbox Code Playgroud)
是的,它对我有用; 并且与os.walk相比要快得多; 所以我打算用它.但我不得不承认:我不明白它在做什么; 特别是'line [2:]'部分...... wtf?!
我试着用google/so来找到答案; 好吧,搜索"python line"根本没有帮助......所以,可能是愚蠢的问题:这是什么意思?
说我有一个像这样的字符串列表:
list = ["Jan 1", "John Smith", "Jan 2", "Bobby Johnson"]
Run Code Online (Sandbox Code Playgroud)
如何将它们分成两个单独的列表?我的老师提到了关于索引的一些内容,但没有很好地解释它
li1 = ["Jan 1", "John Smith"]
li2 = ["Jan 2", "Bobby Johnson"]
Run Code Online (Sandbox Code Playgroud) 我正在通过Automate the Boring Stuff学习Python,而且我遇到了一些我不太了解的东西.
我正在尝试创建一个简单的for循环,以这种格式打印列表的元素:W, X, Y, and Z.
我的代码如下所示:
spam = ['apples', 'bananas', 'tofu', 'cats']
def printSpam(item):
for i in item:
if i < len(item)-1:
print (','.join(str(item[i])))
else:
print ("and ".join(str(item[len(item)-1])))
return
printSpam(spam)
Run Code Online (Sandbox Code Playgroud)
我得到这个错误作为回应:
Traceback (most recent call last):
File "CH4_ListFunction.py", line 11, in <module>
printSpam(spam)
File "CH4_ListFunction.py", line 5, in printSpam
if i < len(item)-1:
TypeError: '<' not supported between instances of 'str' and 'int'
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.谢谢你帮助一个新手.
python ×10
list ×2
python-3.x ×2
syntax ×2
palindrome ×1
recursion ×1
regex ×1
reverse ×1
slice ×1
string ×1