计算.csv文件的列中重复项的实例

时间:2015-10-18 20:56:54

标签: python

import re, csv
import os,shutil
import io,json, collections
from collections import Counter, defaultdict,deque

sn=0 #1st column
p_f=1 #2nd column


reader = csv.reader(open("C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv", "r"), delimiter='\t')


f= csv.writer(open("C:/Users/gurbir.sahota/Documents/python_csv_file_program/final.csv", "w"))


g=open("C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv",'r') 
with open("C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv", 'r') as infh:
    data = csv.reader(infh)
    next(data)  # skip header

    seen = defaultdict(set)

    counts = Counter(
    row[sn]    
    for row in data
    if row[sn] and row[p_f] not in seen[row[sn]] and not seen[row[sn]].add(row[sn])   
    )


print(counts.most_common())
#want to count instances of the number 2 in [('VFGRP15040030', 2), ('VFGRP15370118', 2), ('VFGRP15150113', 2)]


x=len(list(csv.reader(open('C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv'))))
print('# of rows including header=');print(x)
count_pass = sum(1 for row in csv.reader(open('C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv')) if row[1] =='pass')
print('# of passes=');print(count_pass)

count_fail = sum(1 for row in csv.reader(open('C:/Users/gurbir.sahota/Documents/python_csv_file_program/remove_duplicates.csv')) if row[1] =='fail')
print('# of fails=');print(count_fail)

#count_retest = ??




g.close
#f.close

1 个答案:

答案 0 :(得分:0)

package cardapp;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed = 0;



       public int Discards(int pressedNumber){

              return pressed;

             }



       public void Randomizer (){

           int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number

           //------------------Decide what hand to randomize --------------------------//
           if (pressed==1){
                  LeftSuit= Suits[RandomSuitNumber];
                  }

              if (pressed==2){
                 MiddleSuit=Suits[RandomSuitNumber];
                  }

              if (pressed==3){
                  RightSuit=Suits[RandomSuitNumber];
                    }
          //----------------20% chance of new random set------------------------------------//
            int ProabilityRandomNum = randomInt.nextInt(5);//this will create a random number for probability array
            RandomShuffle= probability[ProabilityRandomNum];//this will pick a random letter in proability array


          //------------If proability array equals R then change all of the suits----------//  
            if (RandomShuffle.equals("R")){
                JOptionPane.showMessageDialog(null, "Randomized Hand!");
                int leftNumber = randomInt.nextInt(4);
                int middleNumber = randomInt.nextInt(4);
                int rightNumber = randomInt.nextInt(4);
                LeftSuit= Suits[leftNumber];
                MiddleSuit= Suits[middleNumber];
                RightSuit= Suits[rightNumber];}

            ThreeSuits = (LeftSuit + MiddleSuit + RightSuit); 
       }


       public String ListOfCards (){
              return ThreeSuits;
             }




         public void GameOver(){
                  if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit &&    


    RightSuit== LeftSuit){
                      JOptionPane.showMessageDialog(null, "WINNER!!");
                     }
             }
    }