几个列表之间的共同元素比较

时间:2019-11-08 21:04:45

标签: python list

与以下问题相同:Common elements comparison between 2 lists 但我要几个而不是两个。 我希望从几个列表中将它们全部包含的所有元素转移到新列表中。

2 个答案:

答案 0 :(得分:3)

假设输出中元素的顺序无关紧要...

将每个列表转换为一组,然后对其运行let menu = [ [ 'Appetizers', ['Stuffed Mushrooms', "", '$6.95'], ['Zucchini Sticks', "Served with marinara sauce", '$6.95'] ], [ 'Soups', ['Chicken Noodle','','$4.95'], ['Minestrone','','$4.95'], ['Lentil','','$5.95'], ['Pasta Fagioli','','$5.95'], ['Cheese Tortellini','','$6.95'], ] ] //Generate Table of Contents tocUlElement = document.getElementById('toc-list'); //gets ul element. append li elements to this element tocItems = generateTOCItemsAsArray(menu); // i.e ['Appetizers', 'Entrees', 'Desserts'] for(i = 0; i < tocItems.length; i++){ liElement = document.createElement("li"); //create list item element liElement.classList.add("toc-list-item"); //give class to list item element aElement = document.createElement('a'); //create link element aElement.innerHTML = tocItems[i]; // give link text aElement.setAttribute('href', '#'); //leave href blank for now liElement.appendChild(aElement); //append link to list item tocUlElement.appendChild(liElement); //append list item to ul } //Generate Menu let menuDiv = document.getElementById('menu') // represents entire menu for(i = 0; i < menu.length; i++){ let section = document.createElement('div'); // one section of the menu (i.e Appetizers) section.classList.add('section'); for(j = 0; j < menu[i].length; j++){ if(j == 0){ // menu[i][0] is a string representing the name of the section i.e 'Appetizers' let sectionHeader = document.createElement('div'); // will contain heading to be displayed if user is on desktop and if user is on mobile sectionHeader.classList.add('section-header'); sectionHeader.classList.add(menu[i][0].toLowerCase()); // button will share this class with each entry element so that the proper ones can be shown when button is clicked sectionHeader.innerHTML = menu[i][j]; // i.e 'Appetizers', 'Entrees', etc section.appendChild(sectionHeader); // append .section header to entire section (.section) } else{ //Every element after menu[i][0] is is an array representing a menu entry in the form of [food, description, price] let entry = document.createElement('div'); // div represents a single entry, it consits of 3 divs, one for the name of the food, description and price entry.classList.add('entry'); entry.classList.add(menu[i][0].toLowerCase()); let nameOfFood = document.createElement('div'); // These three divs will be appended to the entry div let description = document.createElement('div'); let price = document.createElement('div'); nameOfFood.classList.add('name-of-food'); description.classList.add('description'); price.classList.add('price'); nameOfFood.innerHTML = menu[i][j][0]; // Remember [food, description, price] description.innerHTML = menu[i][j][1]; price.innerHTML = menu[i][j][2]; entry.appendChild(nameOfFood); // entry.appendChild(description); entry.appendChild(price); section.appendChild(entry); // Append the entry to the section } // Repeat this process for every entry remaining in the current array item, ex first iteration is at menu[0] menuDiv.appendChild(section); // Append section to the menu } // move on to next item in menu variable. Second iteration would be menu[1] } let sectionHeaders = document.getElementsByClassName('section-header'); // for(i = 0; i < sectionHeaders.length; i++){ sectionHeaders[i].addEventListener('click', function(){ // Right now this.className = 'section-h2 appetizers'(or entrees, etc). // by splitting at the " " it creates an array equal to ['section-h2', 'appetizers']. // The pop() method then removes the last element and returns it. Leaving targetClass equal to // 'appetizers'. let targetClass = this.className.split(" ").pop(); //After this I was going to try and target all divs with targetClass // and display those. Stopped doing this because I feel I am doing it wrong. } )}; /* Function definitions */ function generateTOCItemsAsArray(menu){ let tocItemsAsArray = [] for(i = 0; i < menu.length; i++){ tocItemsAsArray.push(menu[i][0]); } return tocItemsAsArray; } 。您可以使用列表理解和参数扩展在一行中做到这一点:

df_xlsx =pd.read_excel('/content/FA15-BSE.xlsm' ,sheet_name = 'Final')
df = df_xlsx[df_xlsx['Registration#'] == FA15-BSE-081]
df
if df.CSC103 >= 70:
print('hi')
set.intersection

为获得更高的效率,您可以单独调用交叉路口:

list1 = [1,2,3,4,5]
list2 = [2,3,4,5,6]
list3 = [3,4,5,6,7]

listoflists = [list1,list2,list3]

commons = list(set.intersection(*[set(l) for l in listoflists]))
print (commons)

这对我来说是更好的时机,因为不需要完全搜索长列表。这里绝对有改进的空间:

[3, 4, 5]
list1 = [1,2,3,4,5]
list2 = [2,3,4,5,6]
list3 = [3,4,5,6,7]

listoflists = [list1,list2,list3]

commons = set(listoflists[0])
if (len(listoflists)>1):
    for l in listoflists[1:]:
        commons = commons.intersection(l)

答案 1 :(得分:0)

使用散列可以通过这种方式完成。

# your code goes here


list1 = [1,2,3,4,5]
list2 = [2,3,4,5,6]
list3 = [3,4,5,6,7]


from collections import defaultdict

dic = defaultdict(int)

al_list = [list1,list2, list3]
al_list = [set(element) for element in al_list]

for sublist in al_list:
        for element in sublist:
            dic[element] +=1

len_list = len(al_list)
common_element = [k for k, v in dic.items() if v==len_list]
print(common_element)

输出

 [3, 4, 5]