python 常用技巧 — 列表2(list)

1. 删除字符串列表中的数字字符串 (deletes a numeric string from the string list)

2. 计算列表中true和false的个数(python list count occurrence of true and false)

3. python 列表转成字典根据列表元素出现次数( list convert to dictionary according to occurrences of the list elements)

4. 判断列表是否是嵌套列表(python check if a list is nested)

5. 替换列表中的某个值(python replace value in list)

6. 重复列表中每个元素k次(python repeat each element k times in list)

1. 删除字符串列表中的数字字符串 (deletes a numeric string from the string list)

参考链接: https://stackoverflow.com/questions/16908186/python-check-if-list-items-are-integers

1 In [20]: a=['治理', '经费', '20', '模式', '科研', '91', '管理', '政府']                                                                                                                                     
2 
3 In [21]: b=[s for s in a if not s.isdigit()]                                                                                                                                                                
4 
5 In [22]: b                                                                                                                                                                                                  
6 Out[22]: ['治理', '经费', '模式', '科研', '管理', '政府']

2. 计算列表中true和false的个数(python list count occurrence of true and false)

参考链接: https://www.geeksforgeeks.org/python-count-true-booleans-in-a-list/

 1 In [37]: a=np.random.randint(0, 10, 10)                                         
 2 
 3 In [38]: a                                                                      
 4 Out[38]: array([8, 1, 7, 5, 7, 0, 6, 8, 1, 9])
 5 
 6 In [39]: b = a > 5                                                              
 7 
 8 In [40]: b                                                                      
 9 Out[40]: 
10 array([ True, False,  True, False,  True, False,  True,  True, False,
11         True])
12 
13 In [41]: c = list(b)                                                            
14 
15 In [42]: c                                                                      
16 Out[42]: [True, False, True, False, True, False, True, True, False, True]
17 
18 In [43]: count_true = sum(c)                                                    
19 
20 In [44]: count_true                                                             
21 Out[44]: 6

 3. python 列表转成字典根据列表元素出现次数( list convert to dictionary according to occurrences of the list elements)

参考资料: https://stackoverflow.com/questions/3496518/using-a-dictionary-to-count-the-items-in-a-list

1 In [248]: a                                                                     
2 Out[248]: ['right', 'wrong_short', 'wrong_exceed', 'right', 'wrong_short', 'miss']
3 
4 In [249]: d = {x:a.count(x) for x in a}                                         
5 
6 In [250]: d                                                                     
7 Out[250]: {'right': 2, 'wrong_short': 2, 'wrong_exceed': 1, 'miss': 1}

4. 判断列表是否是嵌套列表(python check if a list is nested)

参考资料: https://blog.finxter.com/how-to-check-if-a-list-is-nested-in-python/

In [9]: a = [[1, 2], [1, 4]]

In [10]: b=[1, 2, 3]

In [11]: any(isinstance(i, list) for i in a)
Out[11]: True

In [12]: any(isinstance(i, list) for i in b)
Out[12]: False

5. 替换列表中的某个值(python replace value in list)

参考资料: https://www.statology.org/replace-values-in-list-python/

实现把列表 a 中的 1 替换成 0:

In [18]: a = [5, 6, 0, 7, 1, 1, 1]

In [19]: [0 if i==1 else i for i in a]
Out[19]: [5, 6, 0, 7, 0, 0, 0]

6. 重复列表中每个元素k次(python repeat each element k times in list)

参考资料:https://www.geeksforgeeks.org/python-repeat-each-element-k-times-in-list/

In [10]: a = list(range(1, 6))

In [11]: print(a)
[1, 2, 3, 4, 5]

In [12]: k = 2

In [13]: b = [i for i in a for j in range(k)]

In [14]: print(b)
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
原文地址:https://www.cnblogs.com/ttweixiao-IT-program/p/13496691.html