Example of removing null characters from Python list

  • 2021-07-24 11:18:24
  • OfStack

As shown below:


# x = ['c b a',"e d f"]
# y = []
# for i in x:
# for ii in i:
# # print(ii)
# if ii == ' ':
# pass
# else:
# y.append(ii)


# print(y)

# python clears null characters from list


# list1 = ['122','2333','3444',' ','422',' ',' ','54',' ']
#  No. 1 1 This method will lead to the final 1 A ' ' Has not been removed ['122', '2333', '3444', '422', '54', ' ']
# for x in list1:
# if x == ' ':
# list1.remove(' ')
# print(list1)


#  No. 1 2 Methods: ['122', '2333', '3444', '422', '54']
# for x in list1:
# if ' ' in list1:
# list1.remove(' ')
# print(list1)


#  No. 1 3 One way: traverse the number of spaces and then delete them one by one 
# for x in range(list1.count(' ')):
# list1.remove(' ')
# print(list1)


#  No. 1 4 One way: I used it while And for1 Sample 
# while ' ' in list1:
# list1.remove(' ')
# print(list1)

# Remove spaces in the middle of a string


#  No. 1 1 One way: use replace, But this method is stupid, if there is a 1 What about ten thousand spaces? Do you want to type them all 
# a = 'hello world'
# b = a.replace(' ','')
# print(b)


#  No. 1 2 Methods: 
# a = 'hello world'
# a = list(a)
# for x in a:
# if ' ' in a:
# a.remove(' ')
# bb = ''.join(a)
# print(bb)




# a = 'hello wor ld'
# # aa = a.split()
# # print(aa)
# # print(''.join(aa))
# print(''.join(a.split()))




# list1 = ['122','2333','3444',' ','422',' ',' ','54',' ']
# for x in list1:
# for i,j in enumerate(list1):
# print(i,j)
# if x == ' ':
# list1.remove(' ')
# print(list1)
# print('***************************************')
# print(list1)

# Remove duplicate elements from the list


#  Method 1 :   Be shy about the list, compare it from beginning to end, and delete duplicate elements, otherwise the pointer moves to the right 1 Bit 
# Method 1 The logic is complex, temporary variables save values consume memory, return results destroy the original list order, and the efficiency is the worst 
# def deleteDuplicatedElement(l):
# l.sort()
# length = len(l)
# firstItem = l[0]
# for x in range(1,length-1):
# # if x < length - 2:
# # if l[x] == l[x+1]:
# # l.remove(l[x])
# # return l
# currentItem = l[x]
# if firstItem == currentItem:
# l.remove(currentItem)
# else:
# firstItem = currentItem
# return l


# print(deleteDuplicatedElement(['d','d','1','2','1','4']))




# def deleteDuplicatedElement(l):
# l.sort()
# length = len(l)
# lastItem = l[length-1]
# for x in range(length-2,-1,-1):
# currentItem = l[x]
# if lastItem == currentItem:
# l.remove(currentItem)
# else:
# lastItem = currentItem
# return l


# print(deleteDuplicatedElement(['python','r','r','g','g','g','t','y','g','n']))




#  Method 2 : Setting 1 The temporary list saves the results, traverses the original list from scratch, and appends if there is no current element in the temporary list: 
# Method 2 That calls directly append The method modifies the list in place, the logic is clear, and the efficiency is second 
# def deleteDuplicatedElement(l):
# ll = []
# for x in l:
# if x in ll:
# continue
# else:
# ll.append(x)
# return ll


# print(deleteDuplicatedElement(['python','r','r','g','g','g','t','y','g','n']))




#  Method 3 : Use Python Collection elements in the 1 Sexual characteristics, convert the list into a collection, and then convert it into a list output 
# Method 3 , extremely concise, using python The native method is the most efficient, but the original order of the list is disrupted 
# def deleteDuplicatedElement(l):
# return sorted(list(set(l)),key=l.index)


# print(deleteDuplicatedElement(['python','r','r','g','g','g','t','y','g','n']))


Related articles: