[英]How can I remove element from list?
嘗試從字符串中刪除不是數字的字符。 不幸的是,由於某種原因,我不能這樣做。 我也試過“del file[i][j]”,不幸的是沒有。
代碼:
file_open = open("napisy.txt","r")
wyniki = open("wyniki4.txt","w")
file = file_open.read().split()
file1 = file
print(file)
cyfry = ['0','1','2','3','4','5','6','7','8','9']
cyfry_zad3 = ''
licznik_cyfr = 0
tab = []
for i in range(0,len(file1)):
for j in range(0,len(file1[i])):
if file1[i][j] not in cyfry:
file1.remove(file1[i][j])
#del file1[i][j]
print(file1)
結果
讓一切變得更簡單怎么樣?
bad_characters = ['0','1','2','3','4','5','6','7','8','9']
file_text = file.read()
file_text = ''.join(filter(lambda c: c not in bad_characters, file_text))
適合新手的版本:
bad_characters = ['0','1','2','3','4','5','6','7','8','9']
file_text = file.read()
good_text = ""
for character in file_text:
if character not in bad_characters:
good_text += character
print(good_text)
你可以用這個
file_open = open("napisy.txt","r")
file = file_open.read()
file_open.close()
for char in list(file):
if char not in "1234567890":
file = file.replace(char, "")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.