为什么这个for循环不适用于整个列表?

时间:2018-04-06 17:07:13

标签: python for-loop

尝试为我正在做的python课程构建我的第一个游戏。 我如何让这个for循环遍历整个列表,将每个值更改为单词'bull'? 谢谢!

输入:

a = [1,2,3,4]
b = [1,2,3,4]

for x in a:
    if x in b:
        b[x] = 'bull'

print (a)
print (b)

输出:

[1, 2, 3, 4]
[1, 'bull', 3, 'bull']

4 个答案:

答案 0 :(得分:3)

这是一个比第一次出现时更有趣的问题。

for x in a:

将迭代1,2,3,4。

if x in b:
   b[x] = 'bull'

分别检查1,2,3,4是否在b中,并将b[x]设置为bull。 由于Python是基于零的索引,所以简单的猜测就是这样:

b = [1,'bull','bull','bull']
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list assignment index out of range

相反,你会看到:

b = [1, 'bull', 3, 'bull']

是什么给出的?什么是从零开始的索引?

基于零的索引只是意味着,如果你想要某个东西的第一个元素,你可以在索引0处调用它。换句话说,如果a = [1,2,3,4],则a[1]给出2a[0]给出1。令人困惑的是,一些计算机科学家之间存在争论,认为这实际上是最好的方法。有些语言,比如Fortran和Julia,有一个基于索引的索引,所以你想要做的事情实际上就是在那里工作。

现在回到问题:让我们逐个循环。

在第一个循环x = 1上,x in b评估为Trueb[1] = 'bull'评估为:b = [1,'bull',3,4]

在第二个循环中,x = 2 x in b评估为 False !请记住b = [1,'bull',3,4],因此b中不再有2,它会跳过元素分配!

在第三个循环中,3 in bTrue,现在为b[3] = 'bull'b = [1,'bull',3,'bull']

在第四个循环中,b中没有4,所以没有任何反应。

对于您正在尝试完成的任务,您可以通过以下两种方式之一来解决这个问题:

for (i,x) in enumerate(a):
    if x in b:
        b[i] = 'bull'

在此方法中,i初始化为0,并且每个循环增加一个。因此i将跨越0,1,2,3。完美的零基索引!

另一种方法是:

for x in a:
    if x in b:
        b[x-1] = 'bull'

x为1时,x-1会为您提供0,并记住b[0] = 1

答案 1 :(得分:2)

您可以使用zip检查两个列表的相应索引处的元素是否相同。

a = [1,2,3,4]
b = [1,2,3,4]

for x, y in zip(a, b):
    if x == y:
        b[x-1] = 'bull'

print (a)  # [1, 2, 3, 4]
print (b)  # ['bull', 'bull', 'bull', 'bull'] 

注意:列表索引从0开始。因此,您需要将bull分配给x-1的索引b

最后,如果您需要 1-liner

b = ['bull' if x == y else y for x, y in zip(a, b)]

答案 2 :(得分:1)

如果x1,则b[x] = 'bull'会写入x[1],但1列表中a的位置实际上是a[0] a

您想要的是使用b值的索引来操纵for index, value in enumerate(a): if b[index] == value: b[index] = 'bull' 中的相应值:

b

如果您确实想要替换for value in a: try: index = b.index(value) except ValueError: continue b[index] = 'bull' 中的值,无论其索引如何,您都需要找到要首先操作的索引:

b = ['bull' if x in a else x for x in b]

或者,甚至更短,使用列表理解:

def patclick(self):
       root = Tk()
       root.title("INSERT DETAILS")
       c = Canvas(root, bg="#dfe3ee")
       c.grid()
       titlelabel = Label(root, text="Please Login to continue")
       titlelabel.grid(row=0, columnspan=2)
       patnamelabel = Label(root, text="Full Name")
       patnamelabel.grid(row=1, column=0, sticky=E)
       self.patnameentry = Entry(root)
       self.patnameentry.grid(row=1, column=1)
       patpwlabel = Label(root, text="Password")
       patpwlabel.grid(row=2, column=0, sticky=E)
       self.patpwentry = Entry(root, show='*')
       self.patpwentry.grid(row=2, column=1)
       patloginbutt = Button(root, text="Login")
       patloginbutt.grid(row=3, columnspan=2)
       newpatlabel = Label(root, text="New Patient?")
       newpatlabel.grid(row=4, columnspan=2)
       patsignupbutt = Button(root, text="Sign Up")
       patsignupbutt.grid(row=5, columnspan=2)

答案 3 :(得分:0)

您可以尝试以下基本代码。

a = [1,2,3,4]
b = [1,2,3,4]
c=[]
c=b
i=0
for x in a:
    print (x)
    if x in c:
        c[i] = 'bull'
        i=i+1

print (a)
print (c)