#!/usr/bin/python
list2 = ['Bangalore','Cochin','Trivandrum','Auckland','Seoul','Los Angeles']
list2[5] = 'Hamilton'
list2.append('Sydney')
list2.append('San Jose')
list2.append('Amsterdam')
print "Cities = ",list2[0:(len(list2) - 1)]
print "Cities = ",list2[0:(len(list2))]
Run Code Online (Sandbox Code Playgroud)
第一个print语句不打印列表中的最后一个元素.第二个print语句打印列表中的所有元素没有越界错误.从文档中我理解len()只返回列表中的元素数那么为什么最后一个索引不是len(list) - 1
我在Python 2.7中有一段非常简单的代码,我必须使用a while loop来向后遍历索引.我正在打印出它向后的打印件,但是我while loop不会在最后停止因此产生超出范围的错误,我不知道为什么.我试图解决它但失败了.
这是我的代码:
fruit = 'banana'
print len(fruit)
index = 0
while index <= len(fruit):
letter = fruit[index - 1]
print letter
index = index - 1
Run Code Online (Sandbox Code Playgroud)
我认为是怎么回事是我初始化VAR index来0,然后问蟒蛇用var工作fruit而指数小于或等于果实的大小.问题是当索引变为0时,我也尝试使用<但我编写代码的方式似乎仍然超过0,但我不确定.
代码
def main(bar):
bar = str(bar)
print bar
main(sys.argv[1:])
Run Code Online (Sandbox Code Playgroud)
版画
['bar']
Run Code Online (Sandbox Code Playgroud)
而不仅仅是
bar
Run Code Online (Sandbox Code Playgroud)
.我该怎么做才能使输入参数完全符合字符串?谢谢.(Python 2.5.2)
我怎样才能在Python中执行此操作?
strnset(string,symbol,n);
Run Code Online (Sandbox Code Playgroud)
输出应该是:
Enter String : ABCDEFGHIJ
Enter Symbol for replacement : +
How many string characters to be replaced : 4
Before strnset() : ABCDEFGHIJ
After strnset() : ++++EFGHIJ
Run Code Online (Sandbox Code Playgroud) 我有一个清单说,sample_list = [1,2,3,4,5,6,7]。
现在,当我尝试访问时:
print(sample_list[-2:2])
Run Code Online (Sandbox Code Playgroud)
或者,
print(sample_list[-3:-7])
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都会得到一个空列表。
难道它不应该按照从左到右的约定至少打印[6,7]或分别打印吗?[5,6,7]
我知道,我在语义上做了错误的事情。
(尝试以从右到左的方向打印列表?)
我想了解空列表背后的逻辑。这里发生了什么?
我正在寻找分解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 ×3
python-2.7 ×2
python-3.x ×2
c ×1
palindrome ×1
recursion ×1
reverse ×1
syntax ×1
while-loop ×1