我正在从名为WeaponShack.list
的文本文件中读取文件
这些是“武器shack.list”的内容:
Grenade
Ak-47
Shotgun
Makarov
Minigun
Bat
Katana
Chainsaw
Flamethrower
没有空格。这些项目按原样复制粘贴。
我尝试删除一些换行符,但是在打印列表时,“ \ n”字符只会不断出现在某些元素上。我不明白为什么。
我尝试使用.strip('\ n'),但“ \ n”字符仍会弹出 一些元素。
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)
预期输出为:
从武器棚屋中获取武器 ['Ak-47','Makarov','Bat','Chainsaw','Grenade','Minigun','Flamethrower','Katana','Shotgun']
但是实际输出是:
从武器棚屋中获取武器 ['Ak-47 \ n','Makarov \ n','Bat \ n','Chainsaw \ n','Grenade','Minigun','Flamethrower','Katana','Shotgun']
有人知道为什么会这样吗?
编辑
我的重点是为什么出现“ \ n”字符。不是如何遍历列表。
还尝试建议: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)
产生以下输出:
从武器棚屋中获取武器 ['Grenade','Shotgun','Minigun','Katana','Flamethrower']
很奇怪,因为它没有显示所有元素。
答案 0 :(得分:2)
这是因为您同时在阅读和修改列表,从而防止遍历每个元素。 weapon.strip('\n')
要使其更整洁,可以消除遍历问题并摆脱新的空格,请尝试使用类似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对应的四行:
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次。
答案 1 :(得分:1)
@rdas谢谢您的帮助。
” 在列表上进行迭代时从列表中删除内容会扰乱列表中元素的相对顺序。由于您只迭代列表一次,因此某些元素可能会被跳过。因此\ n不会出现-只是从未从某些元素中删除。”
我删除了(ns foo.bar (:require [cljs-bach.synthesis :as b]))
(defonce audio-context (b/audio-context))
(defn playback-mp3
[url]
(let [mp3 (b/connect-> (b/sample url) ; read file using js ajax, including caching
(b/gain 0.5) ; you can chain optional effects here
b/destination) ; loudspeakers
]
(b/run-with mp3
audio-context
(b/current-time audio-context)
3.0 ; play for 3 seconds
)))
; (playback-mp3 "/music/qux.mp3")
行,并对其进行了修复!
干杯!