相关疑难解决方法(0)

编写与reverse()函数类似的Python代码

我正在寻找分解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 reverse list python-3.x

0
推荐指数
1
解决办法
3372
查看次数

Python回文程序不起作用

我在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 recursion palindrome

0
推荐指数
2
解决办法
679
查看次数

这个语法是什么意思:paths = [line [2:] for

我偶尔使用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"根本没有帮助......所以,可能是愚蠢的问题:这是什么意思?

python

0
推荐指数
1
解决办法
73
查看次数

如何在Python中拆分字符串列表

说我有一个像这样的字符串列表:

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)

python list python-3.x

0
推荐指数
1
解决办法
81
查看次数

Python:<str和int之间不支持

我正在通过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 syntax

0
推荐指数
1
解决办法
1万
查看次数

如何在python中反向工作

我有一个包含一些元素的数组.

如何arr[::-1]对整个阵列进行排序?

那背后的逻辑是什么?

python reverse

0
推荐指数
1
解决办法
108
查看次数

Pythonic方法查找字典数组的最大值和最小值

是)我有的:

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

我知道冒号代表"循环所有指数"所以我做错了什么?

python dictionary list slice

0
推荐指数
1
解决办法
142
查看次数

如何调用python中日期列表中的每个第n个元素?

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)

python python-3.x

0
推荐指数
1
解决办法
816
查看次数

Python 中的 arr 和 arr[:] 有什么区别?

我想知道列表变量本身和后跟 [:] 的列表变量之间的区别

例如,

# 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)

提前感谢您的帮助!

python list python-3.x

0
推荐指数
1
解决办法
387
查看次数

按某个步骤移动字符串的所有字母

  • 输入:['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 string indexing loops python-3.x

0
推荐指数
1
解决办法
396
查看次数