In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to modify the list elements in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Code implementation
Stus = ['Sun WuKong', 'Zhu Bajie', 'Sha Wujing', 'Tang monk', 'spider essence', 'white bone essence'] print ('pre-modified stus:',stus) # modify elements stus [0] =' sunwukong'stus [2] = '' # delete elements del stus [1] print ('modified stus:',stus) stus1 = [' Sun WuKong', 'Zhu Bajie', 'Sha Wujing', 'Tang monk' by del 'Spider essence', 'White Bone essence'] print ('pre-modified stus1:',stus1) # modify the list stus1 [0:2] = [' Bull Devil King', 'Red Boy'] # replace the old element print ('modified stus1-1 stus1-1 print) stus1 [0:2] = [' Bull Devil King', 'Red Boy', 'Erlang God'] print ('modified stus1-2 stus1' Stus1) stus1 [0:0] = ['Bull Devil King'] # insert the element print into the position with index 0 ('modified stus1-3 stus1-3 print) # when the step size is set The number of elements in the sequence must be the same as the number of elements in the slice stus1 [:: 2] = ['Bull Devil King 1' Magic 'Red Boy 1' 'Erlang Shen' Erlang Shen''Baigujing 1'] print (' modified stus1-4 stus1') # delete the element del stus1 [0:2] by slicing ('modified stus1-5 charge Stus1) del stus1 [: 2] print (' modified stus1-6 stus1' Stus1) stus1 [1:3] = [] print ('modified stus1-7 print)
Code run output:
Stus before revision: ['Sun WuKong', 'Zhu Bajie', 'Sha Wujing', 'Tang monk', 'spider essence', 'white bone essence']
Modified stus: ['sunwukong',' ', 'Tang monk', 'spider essence', 'white bone essence']
Stus1 before revision: ['Sun WuKong', 'Zhu Bajie', 'Sha Wujing', 'Tang monk', 'spider essence', 'white bone essence']
Modified stus1-1: ['Bull Devil King', 'Red Boy', 'Sha Wujing', 'Tang Monk', 'Spider essence', 'White Bone essence']
Revised stus1-2: ['Bull Devil King', 'Red Boy', 'Erlang God', 'Sha Wujing', 'Tang Monk', 'Spider essence', 'White Bone essence']
Revised stus1-3: ['Bull Devil King', 'Bull Devil King', 'Red Boy', 'Erlang God', 'Sha Wujing', 'Tang Monk', 'Spider Spirit', 'White Bone essence']
Revised stus1-4: ['Bull Devil King 1', 'Bull Devil King', 'Red Boy 1', 'Erlang God', 'Erlang Shenyi', 'Tang Monk', 'White Bone essence 1', 'White Bone essence']
Revised stus1-5: ['Red Boy 1', 'Erlang Shen', 'Erlang Shen 1', 'Tang Monk', 'White Bone essence 1', 'White Bone essence']
Revised stus1-6: ['Erlang God', 'Tang Monk', 'White Bone essence']
Modified stus1-7: ['Erlang God']
After reading the above, do you have any further understanding of how to modify list elements in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.