我的Python代码仅选择列表内容的一半吗? [英] My Python code is only selecting half of a list's contents?

查看:66
本文介绍了我的Python代码仅选择列表内容的一半吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python还是很陌生,我正在浏览一些在网上找到的示例项目,但此刻我一直在使用回文检查器.

I'm very new to Python, and I'm going through some example projects I found online but I'm stuck on my palindrome checker at the moment.

现在,我的代码将一个单词作为输入,将其分成两半,将每个部分保存到单独的变量中,制作两个变量列表,然后应该从那里反转第二个列表,因此我可以将其与首先,但是从我收集的试图修复它的角度来看,它只是将选择的一半追加到新列表中.

Right now, my code takes a word as an input, splits it in half, saves each part into separate variables, makes both of the variables lists, and from there it SHOULD reverse the second list so I can compare it to the first, but from what I've gathered trying to fix it, it's only appending half of the selection to the new list.

例如,如果我输入"racecar",它将很好地分为"race"和"ecar",但是当我反转"ecar"时,它只会给我"['c','e']".(此外,如果我切换变量以反转前一半,则会得到相同的错误)

For example, if I enter "racecar", it'll split it into "race" and "ecar" just fine, but then when I go to reverse "ecar" it only gives me back "['c', 'e']". (Also, if I switch the variables around to reverse the first half, I get the same error)

我已经尝试解决了很长时间,但是我没有取得任何进展,因此非常感谢您的帮助!

I've been trying to figure it out for quite a while now and I'm not making any progress so some help would be very much appreciated!

Ninja Edit:如果有一种更简单的方法(我确定有),我很想知道,但是我仍然想弄清楚在已有的代码中我做错了什么我可以尝试向它学习

Ninja If there's an easier way to do this (which I'm sure there is) I'd love to know, but I still want to figure out what I've done wrong in the code I already have so I can try to learn from it

到目前为止,这是我的代码:

Here's my code so far:

print "Please enter a word you want to check is a palindrome"
input = raw_input('> ')

#Gets lengths of input
full_length = len(input)
split_length = len(input) / 2

#If word has an even length split like this
if full_length % 2 == 0:
    first_half = input[0: split_length]
    second_half = input[split_length:full_length]

#If word does not have even length split like this
else:
    first_half = input[0:split_length+1]
    second_half = input[split_length:full_length]

#Make both halves lists
first_half_list = list(first_half)
print first_half_list
second_half_list = list(second_half)
print second_half_list

# Reverse second half
rev_second_half = []


for x in second_half_list:
    current_letter = second_half_list[0]
    second_half_list.remove(second_half_list[0])
    rev_second_half.insert(0, current_letter)

print rev_second_half

"""
#Check to see if both lists are identical

#If they are identical
print "This word is a palindrome!"

#If they are not identical
print "This word is not a palindrome."
"""

这是我输入"racecar"时得到的输出:

And this is the output I get when I enter 'racecar':

racecar
['r','a','c','e']
['e','c','a','r']
['c', 'e']

推荐答案

正在进行很多不必要的工作.无需转换为列表;口译员可以为您管理所有这一切.无需手动反转字符串;使用切片.无需手动声明字符串中第一个和最后一个字符的索引;口译员知道他们在哪里.这是代码的固定版本;您可以在IDE One上观看演示:

There's a lot of unnecessary work going on. No need to convert to lists; the interpreter can manage this all for you. No need to manually reverse a string; use slicing. No need to manually declare the indices of the first and last characters in your string; the interpreter knows where they are. Here's a fixed version of the code; you can view a demo at IDE One:

input = 'racecar'

#Gets lengths of input
full_length = len(input)
split_length = len(input) / 2

#If word has an even length split like this
if full_length % 2 == 0:
    first_half = input[:split_length]
    second_half = input[split_length:]

#If word does not have even length split like this
else:
    first_half = input[:split_length+1]
    second_half = input[split_length:]

print first_half
print second_half

rev_second_half = second_half[::-1]

print rev_second_half

种族
ecar
种族

race
ecar
race

通过使用带有负迭代步骤的切片来注意下半部分的反转方式吗?您只需对源字符串执行一次操作,然后将结果与原始字符串进行比较.现在,您可以使用一种方法来检查字符串是否是回文式: input == input [::-1]

Notice the way that the second half is getting reversed, by using a slice with a negative iteration step? You can just do that once, to your source string, and compare the result to the original. Now you have a one line method to check if a string is a palindrome: input == input[::-1]

有关切片语法的更多信息(您可能想了解此问题). input [::-1] input [0:len(input):-1] 完全相同.冒号将三个参数分开,它们是 start:end:step .前两个创建一个范围,其中包括 start 及其之间和 end 之间的所有内容,但不包括 end 本身.不指定 start end 会使解释器假定您的意思分别是"use 0"和"use len".不指定 step 会导致假设为1.负的步进表示从 end 开始并向后退 step 的大小".

A bit more on slicing syntax (you might like to check out this question). input[::-1] is exactly the same as input[0:len(input):-1]. The colons separate the three arguments, which are start : end : step. The first two create a range which includes start and everything between it and end, but not end itself. Not specifying start or end causes the interpreter to assume you mean "use 0" and "use len", respectively. Not specifying step causes an assumption of 1. Using a negative step means "start at end and go backwards by magnitude of step".

如果要省略参数并使用切片指定范围,则需要包括冒号,以便解释器可以知道省略了哪些参数.例如, input [-1] 将返回input的最后一个元素,因为没有冒号表示您正在指定索引,而negative则表示从末尾开始",因此 print如果您输入的是"racecar",则input [:-1] 将产生"raceca".

If you want to omit arguments and specify a range with a slice, you need to include the colons, so the interpreter can tell which arguments are omitted. For example, input[-1] will return the last element of input, because no colons means you're specifying an index, and negative means "go backwards from the end", so print input[:-1] would yield "raceca" if your input was "racecar".

关于代码出了什么问题,问题出在您的反向循环中.

As for what was going wrong with your code, the problem is in your reversing loop.

for x in second_half_list:
    current_letter = second_half_list[0]
    second_half_list.remove(second_half_list[0])
    rev_second_half.insert(0, current_letter)

您要从要遍历的列表中删除项目.不要那样做,这是引发问题的好方法.这就是为什么在这种情况下您只能获得一半的原因.还有不必要的复制,尽管这不会导致错误的结果.最后,您根本没有使用迭代变量,这肯定是循环代码存在某种问题的迹象.在这里,如果您修复了列表变异,但继续使用 second_half_list [0] ,则该字母会重复 len(second_half_list)次.如果您确实需要真正反转列表,可以改成这样:

You're removing items from the list you're iterating through. Don't do that, it's a great way to cause problems; it's why you're only getting half the list in this case. There's also needless copying going on, though that won't cause incorrect results. Finally, you're not using your iterated variable at all, which is a sure sign of some sort of problem with your loop code. Here, if you fixed the list mutation but continued using second_half_list[0], you'd get that letter repeated len(second_half_list) times. If you really need to actually reverse a list, you can do it like this instead:

for x in second_half_list:
    rev_second_half.insert(0, x)

但是,只有在迭代过程中需要某种副作用时,才应实际迭代列表.对于python中的纯反转,您需要这样做,它将表现得更好:

But you should only actually iterate the list if you need some sort of side effects during the iteration. For a pure reversal in python, you want this, which will perform better:

rev_second_half = [reversed(second_half_list)]

这篇关于我的Python代码仅选择列表内容的一半吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆