用Python方式对列表的所有元素执行拆分

时间:2018-08-31 21:40:06

标签: python python-3.x list split

我想以一个列表结尾,该列表包含列表的所有项目上拆分的第一个元素。那是。 input_list = [‘A1 - Some Text’, ‘A2 - Other Text’]的结果应为output_list = [‘A1’, ‘A2’]。最Pythonic(聪明)的方法是什么?不需要多个列表变量的加分点。

更新:

我的初次尝试正在更新:

input_list = [‘A1 - Some Text’, ‘A2 - Other Text’]
output_list = []

for element in input_list:
    output_list.append(element.split(' - ')[0]))

1 个答案:

答案 0 :(得分:0)

如果import java.util.*; import java.lang.*; public class Collections { public HashMap<String, HashMap<String, Integer>> createMap(String beta, String alpha, int m, HashMap<String, Integer> innerStructure, HashMap<String, HashMap<String, Integer>> containingStructure) { while(m>0) { innerStructure.put(beta, m); m--; } containingStructure.put(alpha, innerStructure); return containingStructure; } public void reset(HashMap<String, HashMap<String, Integer>> map, int x) { HashMap<String, Integer> betaMap = new HashMap<String, Integer>(); for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) { System.out.println("Key before is:" + entry.getKey()); if(entry.getValue() instanceof Map) { for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) { if(mapEntry.getValue() == x) { entry.getValue().remove(x); map.put(entry.getKey(), betaMap); } } } } } public void print(HashMap<String, HashMap<String, Integer>> map) { for(Map.Entry<String,HashMap<String,Integer>> entry: map.entrySet()) { System.out.println("Key is:" + entry.getKey()); if(entry.getValue() instanceof Map) { for(Map.Entry<String, Integer> mapEntry: entry.getValue().entrySet()) { System.out.println(mapEntry.getKey()); } } } } public static void main(String[] args) { Collections collections = new Collections(); HashMap<String, Integer> innerStructure = new HashMap<>(); HashMap<String, HashMap<String, Integer>> containingStructure = new HashMap<>(); containingStructure = collections.createMap("B1", "A1", 4, innerStructure, containingStructure); collections.reset(containingStructure, 2); collections.print(containingStructure); } } 包含您的列表,我认为类似:l应该是一个好的开始。