我正在寻找分解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)
任何帮助表示赞赏.谢谢你帮助一个新手.
是)我有的:
vec=[{'max': 23.425, 'date': u'2017-07-18', 'ismax': False, 'ismin': False, 'min': 14.615},
{'max': 23.83, 'date': u'2017-07-19', 'ismax': False, 'ismin': False, 'min': 14.765},
{'max': 24.07, 'date': u'2017-07-20', 'ismax': False, 'ismin': False, 'min': 14.985}]
Run Code Online (Sandbox Code Playgroud)
现在我想找到整体的最小值和最大值; 我试过这样做:
mini=min(vec[:]['min'])
maxi=max(vec[:]['max'])
Run Code Online (Sandbox Code Playgroud)
但它说:
类型错误索引必须是整数而不是str
我知道冒号代表"循环所有指数"所以我做错了什么?
My Date = 2015-07-30
2015-07-31
2015-08-03
2015-08-04
2015-08-05
2015-08-06
2015-08-07
2015-08-10
2015-08-11
2015-08-12
2015-08-13
2015-08-14
Run Code Online (Sandbox Code Playgroud)
我如何从这里拨打每个第二个日期?我尝试了这个,但这不起作用.
for i in range(0, len(Date), 2):
abc = Date[i]
Run Code Online (Sandbox Code Playgroud) 我想知道列表变量本身和后跟 [:] 的列表变量之间的区别
例如,
# When nums are List[int] and res are List,
# what is the difference between
res.append(nums[:])
# and
res.append(nums)
Run Code Online (Sandbox Code Playgroud)
我在实现递归置换函数时提出了我的问题
# When nums are List[int] and res are List,
# what is the difference between
res.append(nums[:])
# and
res.append(nums)
Run Code Online (Sandbox Code Playgroud)
提前感谢您的帮助!
['baNaNa', 7] #字符串和步长 'utGtGt'# 字符串的每个字符按步长向后移动 import ast
in_string = input()
lis = ast.literal_eval(in_string)
st = lis[0]
step = lis[1]
alphabets = 'abcdefghijklmnopqrstuvwxyz'
password = ''
for letter in st:
if letter in alphabets:
index_val = alphabets.index(letter) - (step)
password += alphabets[index_val]
print(password)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是“utgtgt”。我想要'utGtGt'。对此的帮助将不胜感激。
python ×10
python-3.x ×5
list ×4
reverse ×2
dictionary ×1
indexing ×1
loops ×1
palindrome ×1
recursion ×1
slice ×1
string ×1
syntax ×1