All lists get overwritten in Python -


i'm trying to create list, words in function saved , line number. think code done, every time list inside same list, lists got overwritten.

this code:

#python 3.5x  import re  file = open(".\cross.txt", 'r')  def search(s):     find1 = ''     find2 = ''     find3 = ''      dic = {}     pattern = re.compile(r"([a-za-z_]*)[\s.=(]*([a-za-z_]*)[\s.=(]*([a-za-z_]*)")      line, in enumerate(s.readlines()):         result = pattern.search(i)         find1 = ''         find2 = ''         find3 = ''         find1 = result.group(1)         find2 = result.group(2)         find3 = result.group(3)         y=[line]          if find1 in dic.keys():             tmp=dic.get(find1)             tmp.append(line)             dic[find1] = tmp         else:             dic[find1] = y          if find2 in dic.keys():             tmp=dic.get(find2)             tmp.append(line)             dic[find2] = tmp         else:             dic[find2] = y          if find3 in dic.keys():             tmp=dic.get(find3)             tmp.append(line)             dic[find3] = tmp         else:             dic[find3] = y      return dic  print(search(file))  file.close() 

input:

def readin (file):  in_file = open(file,"r")  text = in_file.read()  in_file.close()  return text 

output

{'': [3, 4], 'text': [2, 4], 'file': [0, 1], 'close': [3, 4], 'read': [2, 4], 'in_file': [1, 2, 3], 'def': [0, 1], 'readin': [0, 1], 'return': [4], 'open': [1, 2, 3]} 

for example, open in line 1, gets overwritten.

edit:

i try find words inside input. first line i'm looking "def" "readin" "file". algorithm write keywords dictionary line appears (0 in case).

now checks next line -> "in_file" "open" "file". dictionary should contain def:[0], readin: [0], in_file:[1], file: [0,1], open: [1]. problem is, adds [1[] def, because if i'm looking file, [0] , changes keywords have value [0] [0,1]

minimal example:

l[x] = [0] l[y] = [0] new_list = [1] l[x].append(new_list) 

now l[y] , l[x] [0,1], l[x] should be.

consider case when loop executes first time. i.e. first line of file:

  • suppose regular expression gave 3 values a,b , c , stored them in find1='a', find2='b' , find3='c'.
  • now since dictionary empty initially, 3 else parts execute:

dic['a'] = y, dic['b'] = y, dic['c'] = y

  • now if remember memory model of python, works on referencing system. since y reference referring list [1], elements dic['a'], dic['b'] , dic['c'] along y referring same list:

dic['a'] = dic['b'] = dic['c'] = y = [1]


now consider case when loop executes second time. i.e. second line of file:

  • suppose regular expression gave 3 values a,d , e , stored them in find1='a', find2='d' , find3='e'.
  • now since 'a' present in dictionary, if part of first condition if find1 in dic.keys(): work , append 2 existing list:

dic['a'] = [1,2]

  • now since dic['b'] , dic['c'] referring same list, content of dic['b'] , dic['c'] same of dic['a'].

dic['a'] = dic['b'] = dic['c'] = [1,2]

  • now since d , e not present in dict, same story repeated key d , e:

dic['d'] = dic['e'] = y = [2]

solution:

rather writing y = [line] , assigning y dic[find1], dic[find2] , dic[find3] , assign list directly:

dic[find1] = [line] dic[find2] = [line] dic[find3] = [line] 

this way creating 3 individual lists.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -