bloggerads

2017年7月31日 星期一

Python : list

In Python, We have list and and dictionary, similarly as the container list and map in C++, follows are the list example.

● Init a list in even numbers from 0 to 6


>>>list = [i for i in range(8) if i%2==0]
>>>list
[0, 2, 4, 6]

● Append data:2 to the tail

>>>list.append(2)

>>>list
[0, 2, 4, 6, 2]

● Insert data:3 to index: 2



>>>list.insert(2, 3)
>>>list
[0, 2, 3, 4, 6, 2]

● Remove data:3 

>>>list.remove(3)

>>>list
[0, 2, 4, 6, 2]


● Remove index:2 

>>>del list[2]

>>>list
[0, 2, 6, 2]

● Find the count of a data:2



>>>list.count(2)
2

● Search data:6, return True/False

>>>6 in list

True

● Find length of a list


>>>len(list)
4



● Delete all contents in list



>>>del list[:]

>>>list

[]

【【【  Advance  】

● Collect sub string from a string

>>>s='123,234,456'

>>>b=s.split(',')

>>>b

['123', '234', '456']


● Combine list of string into a string


>>>c=','.join(b)
>>>b
'123,234,456'

沒有留言:

張貼留言