आदेश को बनाए रखते हुए नेस्टेड सूचियों के बीच छेड़छाड़ करना चाहूंगा।पायथन: नेस्टेड सूचियों का चौराहे जहां आदेश
taxa = [['E_pyrifoliae_Ep1_96', 'Bacteria', 'Proteobacteria', 'Gammaproteobacteria', 'Enterobacteriales', 'Enterobacteriaceae', 'Erwinia'],
['E_amylovora_CFBP1430', 'Bacteria', 'Proteobacteria', 'Gammaproteobacteria', 'Enterobacteriales', 'Enterobacteriaceae', 'Erwinia'],
['E_amylovora_ATCC49946', 'Bacteria', 'Proteobacteria', 'Gammaproteobacteria', 'Enterobacteriales', 'Enterobacteriaceae', 'Erwinia']]
चौराहे मेरे पास है खोजने के लिए:
set.intersection(*map(set, taxa))
या
set(taxa[0]).intersection(*taxa)
लेकिन मूल आदेश नहीं रखा गया है।
set(['Erwinia', 'Gammaproteobacteria', 'Enterobacteriaceae', 'Enterobacteriales', 'Proteobacteria', 'Bacteria'])
मूल रूप से, मैं क्या करने की जरूरत है नेस्टेड सूची (वे taxanomic वर्गीकरण हैं) के बीच पिछले आम तत्व लगता है। तो मुझे सभी चौराहे खोजने की ज़रूरत नहीं है, केवल आखिरी एक या उन सभी को जब मैं अंतिम प्रविष्टि पर कॉल कर सकता हूं।
intersection_lst[-1]
इस उदाहरण में मैं आउटपुट 'इरविनिया' होना चाहता हूं।
आपकी मदद के लिए धन्यवाद।
[t for t in taxa[0] if all(t in l for l in taxa)]
# ['Bacteria', 'Proteobacteria', 'Gammaproteobacteria', 'Enterobacteriales', 'Enterobacteriaceae', 'Erwinia']
सूचियों बड़े हैं, तो यह करने के लिए और अधिक कुशल हो जाएगा:
अजगर के किस संस्करण के साथ काम कर रहे हैं:
यहाँ भावी पीढ़ी के लिए है कि समारोह है? –
संस्करण पायथन 2.7.3 – Binnie