本文介绍了Python为什么要在列表元素中添加换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从名为 WeaponShack.list

这些是武器shack.list"的内容:

These are the contents of 'weapon shack.list':

Grenade
Ak-47
Shotgun
Makarov
Minigun
Bat
Katana
Chainsaw
Flamethrower

没有空格.这些项目按原样复制粘贴.

There are no whitespaces. These items are copy-pasted as is.

我尝试删除一些换行符,但是在打印列表时,"\ n"字符只会不断出现在某些元素上.我不明白为什么.

I try to strip down some newline characters but the "\n" character just keeps showing up at some elements when I print the list. I don't understand why.

我尝试使用.strip('\ n'),但"\ n"字符仍会弹出一些元素.

I tried using .strip('\n') but the "\n" characters still pop up at the end ofsome elements.

print("Taking weapons from Weapon shack")
weapons = []
with open("WeaponShack.list",'r') as ws:
    weapons = ws.readlines()
    for weapon in weapons:
        weapons.remove(weapon)
        weapon = weapon.strip('\n')
        weapons.append(weapon)
ws.close()
print(weapons)

预期输出为:

但是实际输出是:

有人知道为什么会这样吗?

Does anyone know why this is happening?

编辑我的重点是为什么出现"\ n"字符.不是如何遍历列表.还尝试建议:添加新列表:

EDITMy focus is why the "\n" character appears. Not how to traverse lists.Also trying the suggestion: adding a new list:

print("Taking weapons from Weapon shack")
weapons = []
newWeaponList = []
with open("WeaponShack.list",'r') as ws:
    weapons = ws.readlines()
    for weapon in weapons:
        weapons.remove(weapon)
        weapon = weapon.strip('\n')
        newWeaponList.append(weapon)
ws.close()
print(newWeaponList)

产生以下输出:

很奇怪,因为它没有显示所有元素.

Weird because it doesn't show all the elements.

推荐答案

这是因为您同时在阅读和修改列表,以防止遍历每个元素. weapon.strip('\ n')

It is because you are both reading and modifying the list preventing traversal of each element. The weapons with '\n' at the end are never reached by weapon.strip('\n')

要使其更整洁,可以消除遍历问题并摆脱新的空格,请尝试类似

To make it cleaner remove the traversal problem and get rid of the new spaces, try something like this answer:

print("Taking weapons from Weapon shack")
with open("weapons.list") as ws:
    readWeapons = ws.readlines()
    for weapon in readWeapons:
        weapon = weapon.rstrip('\n')
        weapons.append(weapon)
print(weapons)

要回答您的问题,这是因为python中的for循环是如何工作的.我不知道确切的细节,但是看起来for循环实际上是实时访问数组,而不是一次读取整个数组.从test.list中检查此读数,其中包含与A-D对应的四行:

To answer your clarification, it's because of how the for loop in python works. I am not privy to the exact details, but it appears that the for loop actually accesses the array in real time rather than read the entire array at once. Check out this reading from test.list which contains four lines corresponding to A-D:

print("Taking item from test\n")
with open("test.list",'r') as ws:
    elements = ws.readlines()
    print(elements)
    print()
    for element in elements:
        print('Current element: ' + element)
        elements.remove(element)
        print('Element pre-strip: ' + str(elements))
        element = element.strip('\n')
        print('Stripped element: ' + element)
        elements.append(element)
        print('Element list post-strip: ' + str(elements))

输出:

Taking item from test

['A\n', 'B\n', 'C\n', 'D\n']

Current element: A

Element pre-strip: ['B\n', 'C\n', 'D\n']
Stripped element: A
Element list post-strip: ['B\n', 'C\n', 'D\n', 'A']
Current element: C

Element pre-strip: ['B\n', 'D\n', 'A']
Stripped element: C
Element list post-strip: ['B\n', 'D\n', 'A', 'C']
Current element: A
Element pre-strip: ['B\n', 'D\n', 'C']
Stripped element: A
Element list post-strip: ['B\n', 'D\n', 'C', 'A']
Current element: A
Element pre-strip: ['B\n', 'D\n', 'C']
Stripped element: A
Element list post-strip: ['B\n', 'D\n', 'C', 'A']

由于添加而不是添加,因此所有其他元素都将被跳过,因为for循环从数组的索引a移至数组的索引a + 1.这将导致B和D(索引1和3)被跳过,而A被读取3次.

Because of of the appending rather than prepending, every other element is being skipped as the for loop moves from index a of the array to index a + 1 of the array. This causes, B and D (indices 1 and 3) to be skipped and A to be read 3 times.

这篇关于Python为什么要在列表元素中添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:13
查看更多