Python if row[0] in row[1] 打印行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25630127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if row[0] in row[1] print row
提问by Adan De Leon
I have a csv file that has 2 columns. I am simply trying to figure if each row[0]value is in some row[1]and if so, to print row.
我有一个有 2 列的 csv 文件。我只是想弄清楚每个row[0]值是否在某个值中row[1],如果是,则打印row.
Items in csv file:
csv 文件中的项目:
COL1, COL2
1-A, 1-A
1-B, 2-A
2-A, 1-B
2565, 2565
51Bc, 51Bc
5161, 56
811, 65
681, 11
55, 3
3, 55
Code:
代码:
import csv
doc= csv.reader(open('file.csv','rb'))
for row in doc:
if row[0] in row[1]:
print row[0]
The end result should be:
最终结果应该是:
1-A
1-B
2-A
2565
51Bc
55
3
Instead, it is giving me:
相反,它给了我:
1-A
2565
51Bc
It prints those numbers because they are right next to each other side by side but what I need it to do is get the first item in COL1 and see if it finds it in the entire COL2 list and print if it does. Not see if its beside each other and print it.
它打印这些数字,因为它们并排紧挨着,但我需要它做的是获取 COL1 中的第一个项目,看看它是否在整个 COL2 列表中找到它,如果找到则打印。不看它是否并排打印。
采纳答案by TheSoundDefense
When you say for row in doc, it's only getting one pair of elements and putting them in row. So there's no possible way row[1]can hold that entire column, at any point in time. You need to do an initial loop to get that column as a list, then loop through the csvfile again to do the comparison. Actually, you could store both columns in separate lists, and only have to open the file once.
当您说 时for row in doc,它只会获取一对元素并将它们放入row. 因此row[1],在任何时间点都无法容纳整列。您需要执行初始循环以将该列作为列表,然后csv再次循环遍历文件以进行比较。实际上,您可以将两列存储在单独的列表中,并且只需打开文件一次。
import csv
doc= csv.reader(open('file.csv','rb'))
# Build the lists.
first_col = []
second_col = set()
for row in doc:
first_col.append(row[0])
second_col.add(row[1])
# Now actually do the comparison.
for item in first_col:
if item in second_col:
print item
As per abarnert's suggestion, we're using a set()for the second column. sets are optimized for looking up values inside it, which is all we're doing with it. A listis optimized for looping through every element, which is what we do with first_col, so that makes more sense there.
根据 abarnert 的建议,我们使用 aset()作为第二列。sets 已针对查找其中的值进行了优化,这就是我们对它所做的一切。Alist针对循环遍历每个元素进行了优化,这就是我们对 所做的first_col,因此在那里更有意义。

