-1

I have a file with lines which are separated by white spaces.

I have written the program below in Perl and it works. Now I must rewrite it in Python which is not my language, but I have solved it more or less.

I currently struggle with this expression in Perl which I can't convert it to Python.

$hash{$prefix}++;

I have found some solutions but I'm not sufficiently experienced with Python to solve this. All the solution looks complicated to me compared to the Perl one.

These Stack Overflow questions seem to be relevant.

Python variables as keys to dict

Python: How to pass key to a dictionary from the variable of a function?

Perl

#!perl -w

use strict;
use warnings FATAL => 'all';

our $line = "";
our @line = "";
our $prefix = "";
our %hash;
our $key;

while ( $line = <STDIN> ) {
  # NAMESPACE                NAME                                         READY     STATUS             RESTARTS   AGE       IP               NODE
  next if $line =~ /^NAMESPACE/;

  #aleks-event-test         redis-1-m06k0                                1/1       Running            0          1d        172.26.0.138     The_Server_name
  @line = split ' ', $line;
  $line[1] =~ /(.*)-\d+-\w+$/ ;
  $prefix = $1;
  #print "$prefix $line[7]\n";
  print "$prefix $line[7]\n";
  $hash{$prefix}++;
}

foreach $key ( keys %hash ) {
  if ( $hash{$key} / 2 ){
    print "$key : $hash{$key} mod 2 \n"
  }
  else {
    print "$key : $hash{$key} not mod 2 \n"
  }
}

Python

#!python

import sys
import re

myhash = {}

for line in sys.stdin:
    """
    Diese Projekte werden ignoriert
    """
    if re.match('^NAMESPACE|logging|default',line):
        continue
    linesplited = line.split()
    prefix = re.split('(.*)(-\d+)?-\w+$',linesplited[1])
    #print  linesplited[1]
    print prefix[1]
    myhash[prefix[1]] += 1
Borodin
  • 126,100
  • 9
  • 70
  • 144
Aleksandar
  • 2,442
  • 3
  • 15
  • 24
  • 1
    Your conditional `if ( $hash{$key} / 2 )` is odd. That will be *false* only if `$hash{$key}` is zero. Did you mean `if ( $hash{$key} % 2 )` perhaps? – Borodin Sep 06 '17 at 17:57
  • Good catch. It was '%' I just tested another idea :-) – Aleksandar Sep 06 '17 at 18:56

1 Answers1

5

Your problem is using this line:

myhash = {}
# ... code ...
myhash[prefix[1]] += 1

You likely are getting a KeyError. This is because you start off with an empty dictionary (or hash), and if you attempt to reference a key that doesn't exist yet, Python will raise an exception.

A simple solution that will let your script work is to use a defaultdict, which will auto-initialize any key-value pair you attempt to access.

#!python

import sys
import re
from collections import defaultdict

# Since you're keeping counts, we'll initialize this so that the values
# of the dictionary are `int` and will default to 0
myhash = defaultdict(int)

for line in sys.stdin:
    """
    Diese Projekte werden ignoriert
    """
    if re.match('^NAMESPACE|logging|default',line):
        continue
    linesplited = line.split()
    prefix = re.split('(.*)(-\d+)?-\w+$',linesplited[1])
    #print  linesplited[1]
    print prefix[1]
    myhash[prefix[1]] += 1
wkl
  • 77,184
  • 16
  • 165
  • 176
  • Instead of `defaultdict(int)` it may be more obvious to specify the default value as `defaultdict(lambda: 0)`, but that is a matter of taste. – amon Sep 06 '17 at 15:49
  • thanks birryree & amon for the fast answer. Sorry for dublication, sometimes is the right question the challenge – Aleksandar Sep 06 '17 at 16:42