由于我对python很陌生,我甚至不确定如何说出我的问题。我想要完成的基本概念是能够在2D数组中搜索某些内容并检索正确的值以及与该值相关联的值(对不起我的错误解释)
e.g。
array=[[1,a,b],[2,x,d],[3,c,f]]
如果用户想要找到2
,我希望程序检索[2,x,d]
,如果可能,将其放入普通(1D)数组中。同样,如果用户搜索3
,程序应检索[3,c,f]
。
提前谢谢(如果可能的话,我想要一个不涉及numpy的解决方案)
答案 0 :(得分:1)
也许是这样的?
def search(arr2d, value):
for row in arr2d:
if row[0] == value:
return row
答案 1 :(得分:1)
您可以执行简单的export default class Example extends Component {
constructor() {
super();
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds,
isLoading: true
};
}
componentDidMount() {
this.fetchData()
}
fetchData() {
Api.getPosts().then((resp) => {
console.log(resp);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(resp.posts),
isLoading: false
})
});
}
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
style={styles.postList}
/>
</View>
);
}
renderRow(post) {
return (
<PostListItem
id={post.id}
coverImage={post.cover_image}
title={post.title}
lockedStatus={post.status}
time={post.time} />
);
}
}
export default class ListItemExample extends Component {
constructor() {
super();
}
render() {
return (
<TouchableHighlight onPress={this.clicked} >
<View style={styles.postItem}>
</View>
</TouchableHighlight>
);
}
clicked() {
console.log("clicked props");
console.log(this.props);
Actions.openPost();
}
}
循环,并使用内置的for
语句:
in
答案 2 :(得分:1)
尝试类似:
def find(value, array):
for l in array:
if l[0]==value:
return l
或者如果您想了解更多信息:
array[list(zip(*array))[0].index(value)]
答案 3 :(得分:1)
如果符合您的问题,您可以使用字典:
>>> dict={1:['a','b'],2:['x','d'],3:['c','f']}
>>> dict[2]
['x', 'd']
比在每个列表中线性搜索正确的索引更有效。