取特定值的数组

时间:2019-02-10 20:35:19

标签: python

我有一堆数组,每个数组有7个条目。我只想查看在第7个条目上具有特定值的数组。

我有一堆看起来像这样的数组:

a = [a1,b1,c1,d1,e1,f1,1]
b = [a2,b2,c2,d2,e2,f2,1]
c = [a3,b3,c3,d3,e3,f3,2]
d = [a4,b4,c4,d4,e4,f4,2]
...
...
...
...

我只想查看最后一个条目具有特定值的数组(即:查看所有array[6] = 1这样的数组,在这种情况下为a和{{1 }},但我不确定该怎么做,我们将不胜感激!

2 个答案:

答案 0 :(得分:4)

list comprehension

a = [a1,b1,c1,d1,e1,f1,1]
b = [a2,b2,c2,d2,e2,f2,1]
c = [a3,b3,c3,d3,e3,f3,2]
d = [a4,b4,c4,d4,e4,f4,2]

list_of_lists = [a,b,c,d]
lists_with_1_at_index_6 = [l for l in list_of_lists if l[6] == 1]
print(lists_with_1_at_index_6)

输出:

# Note the real output would have the actual values of variables a1-f1 and a2-f2
[[a1,b1,c1,d1,e1,f1,1], [a2,b2,c2,d2,e2,f2,1]] 

答案 1 :(得分:2)

您可以使用函数public IEnumerabe<string> SampleData { get { // Return values based on the selection. if (SelectedData == "FirstValueICareAbout") // SelectedData assumes you have investigated how to bind to the SelectedItem of a ComboBox. { return new[] { "FirstValue", "SecondValue" }; } return Enumerable.Empty<string>(); } } private void CmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Notify the UI that SampleData has changed using INotifyPropertyChanged implementation. RaiseNotifyPropertyChanged(nameof(SampleData)); }

filter()