如何确定部分列表和包含所有部分列表元素的更大列表的顺序相同?

时间:2019-03-01 21:09:18

标签: python-2.7

例如[a,a,c,e]和[a,b,c,d,e]。这就是我所说的相同顺序。列表之一是否仅是部分列表都没关系。明确地说,我对排序任何东西都不感兴趣。

编辑:显然我应该提到我的意思是列表具有一些不同的值。例如[a,a,c,e]和[a,b,c,d,e]的顺序相同。仅仅删除重复项并削减更大的列表是行不通的。

3 个答案:

答案 0 :(得分:0)

您可以通过首先从部分列表a中获取唯一元素(按a的顺序,然后从完整列表b中获取那些元素(的顺序为: b)。然后,您可以将两者进行比较,看看它们是否相同。

>>> a = [1, 1, 4, 3]
>>> a = sorted(set(a), key=a.index)  # Remove duplicate elements, but keep the original order
>>> a
[1, 4, 3]
>>> b = [1, 2, 4, 3, 5]
>>> b = [x for x in sorted(set(b), key=b.index) if x in a]  # Get unique elements of a from b in order of b, and filter them out to get only elements present in a
>>> a == b
True

>>> a = [4, 3, 2, 1]
>>> a = sorted(set(a), key=a.index)  # Remove duplicate elements, but keep the original order
>>> b = [1, 2, 4, 3, 5]
>>> b = [x for x in sorted(set(b), key=b.index) if x in a]  # Get unique elements of a from b in order of b, and filter them out to get only elements present in a
>>> a == b
False

答案 1 :(得分:0)

我们可以使用itertools.groupby将相同的连续项目压缩为该值的单个项目。然后,我们可以public class AddReviewActivity extends AppCompatActivity { Spinner companyNameSpinner; ArrayList<String> companies = new ArrayList<>(); DatabaseReference databaseReference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_review); databaseReference = FirebaseDatabase.getInstance().getReference("Company"); companyNameSpinner = (Spinner) findViewById(R.id.companyNameSpinner); DatabaseReference mref = databaseReference.child("name"); FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this, String.class, android.R.layout.simple_spinner_item, mref) { @Override protected void populateView(View v, String model, int position) { ((TextView)findViewById(android.R.id.text1)).setText(model); } }; companyNameSpinner.setAdapter(firebaseListAdapter); } 将压缩后的值与另一个列表一起查看它们是否相同

zip

答案 2 :(得分:0)

您可以首先获取b的唯一集合,然后将其截断为a的大小(反之亦然)

a = [1,2,3,4]
b = [1,1,2,2,3,4,4,5]

a == list(set(b))[:len(a)]