问题描述
我有清单:
['Happy Family','2122540624','213 E Broadway # A,New York,NY 10002,United States']
我想在第一项中添加一个',',
['Happy Family,',United States']
任何朋友都知道该怎么做?
解决方法
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
List<String> firstList = new ArrayList<String>(Arrays.asList("Cake","pizza","pasta","Chocolate"));
List<String> secondList = new ArrayList<String>(Arrays.asList("Chocolate","fruits","pancake","Cake"));
// Add firstList and secondList to a Set to get the list of unique elements
Set<String> thirdList = new LinkedHashSet<>();
thirdList.addAll(firstList);
thirdList.addAll(secondList);
// Create fourthList and fifthList out of thirdList
List<String> fourthList = new ArrayList<>(thirdList);
List<String> fifthList = new ArrayList<>(thirdList);
// Remove all elements of firstList from fourthList
fourthList.removeAll(firstList);
// Remove all elements of secondList from fifthList
fifthList.removeAll(secondList);
// Create a new list by joining fourthList and fifthList
List<String> sixthList = new ArrayList<>();
sixthList.addAll(fifthList);
sixthList.addAll(fourthList);
System.out.println("Elements not common in both the lists: " + sixthList);
}
}
结果:
Elements not common in both the lists: [pizza,pasta,fruits,pancake]
您可以尝试
,您只需要按索引获取项目并添加一个逗号,如下所示:
a = ['Happy Family','2122540624','213 E Broadway # A,New York,NY 10002,United States']
a[0] = f'{a[0]},' # f-string above python 3.6 otherwise a[0] = a[0] + ','
print(a) # Outputs ['Happy Family,',United States']