change the pointer value or to change list1
1, python the same data type string and list directly by "+" is connected
"+" Expands the list only the length, not every element addition.
lst1 = ['a','c']
lst2 = ['d','g']
lst = lst1 + lst2
s1 = "adc"
s2 = "agf"
s3 = s1 + s2
Reverse list
Def rev(lst):
lt = []
for i in lst:
lt = [i] + lt
return lt
Compared with java
. 1, java list can not directly use the "+" operator, java objects to be achieved by a method, such as:
String a[] = { "A", "E", "I" };
String b[] = { "O", "U" };
List list = new ArrayList(Arrays.asList(a));
list.addAll(Arrays.asList(b));
Object[] c = list.toArray();
System.out.println(Arrays.toString(c));
[A, E, I, O, U]
More Reference:
https://www.runoob.com/java/arrays_merge.html
https://www.cjavapy.com/article/290/
2, list of characteristics
Note the difference between the index (index) and slice (slice) of
lst = ['s','f','i']
ls1 = lst[0]
ls2 = lst[0:1]
type(lst1) --->str,进行索引操作
type(lst2) --->list,进行切片操作
Although lst1 lst2 values and the same, but different types of data
3, while receiving the list and string data types, the first and second switching elements
def swap(ls):
return ls[1:2]+ls[0:1]+ls[2:]
4, when the data type for the application data transfer type
myList = list("Isaac")
def swapL(xs) :
return [xs[-1]]+xs[1:-1]+[xs[0]]
def swapLx(lst):
lst = swapL(lst)
swapLx(myList)
myList
Results: [ 'the I', 'S', 'A', 'A', 'C']
Explanation:
5, when a specific change in the parameter value only when
myList = list("Isaac")
def swapL(xs) :
return [xs[-1]]+xs[1:-1]+[xs[0]]
def swapLx(lst):
lst[:] = swapL(lst)
swapLx(myList)
myList
Results:
[ 'C', 'S', 'A', 'A', 'the I']
Explanation:
6, summed up
- direct assignment list data type is to change its pointer to the new variable, does not change the stored data of the original
- list [:] is assigned data directly to change its value, the stored value is changed directly
7. More tips
def swapx(lst):
lst[-1],lst[0] = lst[0],lst[-1]
swapx(myList)
myList
Results:
[ 'c', 's', 'a', 'a', 'I']