0

Published by MBA Skool Team, Last Updated: January 02, 2021 What is Psychographic Segmentation? Psychographic segmentation is the market segmentation strategy in which the total market is divided on the basis of psychology, beliefs, personality, characteristics, lifestyle, attitudes, reasons etc. Psychographic segmentation helps identify people based on the way they think & the kind of life they want to live in terms of lifestyle, status etc. This segmentation method focuses on the psychology of the customer, which can be focused upon by the company for their marketing activities. Psychographic Segmentation is rooted more in the field of psychology than conventional segmentation variables of age, income etc.

  • Create a list using `text.split("\n ")` and check [here](https://stackoverflow.com/questions/3496518/using-a-dictionary-to-count-the-items-in-a-list/9604768) for how to create a counter – RJ Adriaansen Feb 05 '21 at 21:58
  • `x = {word:str(i+1) for i,word in enumerate(text.split('\n '))}` should do the trick. Also added the extra space between `\n` and the next word. This also converts the numbers into strings as per your need starting with 1. Note here that if you have `pie` showing up again in the `text`, then you will lose information as this is a dictionary – Joe Ferndz Feb 05 '21 at 21:59

4 Answers4

1

You can try this

text="pie\n hotdogs\n noodles\n cheese\n..."
rows=text.split("\n")
result = dict((j,i+1) for i,j in enumerate(rows))
print(result)

UPD: If you want to save all repeats, you can't use dict. But you can use a list of tuples like this

text="pie\n hotdogs\n noodles\n cheese\n... \n pie"
rows=text.split("\n")
result = [(j, i+1) for i,j in enumerate(rows)]
print(result)
Arseniy
  • 680
  • 5
  • 10
  • how would I be able to count all duplicate values this just counts the last line that the text was on. for example text='''pie\n hotdogs\n noodles\n cheese\n pie\n...''' dic={ "hotdogs": "2", "noodles": "3","cheese":4, "pie": "5",...} but i need dict = {"pie": "1", "hotdogs": "2", "noodles": "3","cheese":4,"pie":"5"...} – Parker Bryant Feb 05 '21 at 22:09
  • You can't have two elements with the same key in a dict. Whell, then you need list of tuples. I update my answer. If this solves your problem, please mark it as an answer – Arseniy Feb 06 '21 at 06:54
1
file1 = open('nameoffile.txt', 'r')
dictionary={}

for index, line in enumerate(file1, start=1):
    dictionary[line.strip()] = index

file1.close()

print(dictionary)
ble
  • 287
  • 1
  • 5
  • how would I be able to count all duplicate values this just counts the last line that the text was on. for example text='''pie\n hotdogs\n noodles\n cheese\n pie\n...''' this will display dic={ "hotdogs": "2", "noodles": "3","cheese":4, "pie": "5",...} but i need dict = {"pie": "1", "hotdogs": "2", "noodles": "3","cheese":4,"pie":"5"...} – Parker Bryant Feb 05 '21 at 22:14
  • you can't have more than one same key in dictionary. if you want, you can make number of lines as keys and words as values. not sure what you need but you can't do that with dictionary in python – ble Feb 05 '21 at 22:25
1

I guess something like this would fit your purpose:

# read from file 
input_path = "input.txt"
with open(input_path, "r") as file:
    text = file.read()

your_dict = {}
for line_idx, line_text in enumerate(text.split('\n')):
    your_dict[line_text] = line_idx+1

# your_dict is what you want

However, note that duplicate lines will only report the latest occurring index. If you want to store all occurrences you should use a list of indices for each line of text:

from collections import defaultdict

your_dict = defaultdict(list)
for line_idx, line_text in enumerate(text.split('\n')):
    your_dict[line_text].append(line_idx+1)

# now each line will give you a list of indices in ascending order
Rocco Fortuna
  • 300
  • 1
  • 11
1
dict = {}
with open("example.txt") as text:
    i = 0
    for line in text:
        keys = line.split("\\n")
        for key in keys:
            dict[key.strip()] = i
            i+=1 

(1) The above code will convert the text document to a dictionary.

(2) if you have actual new lines then you could use the below:

dict = {}
with open("example.txt") as text:
    i = 0
    for line in text:
        dict[line.strip()] = i
        i+=1 
Wis124
  • 27
  • 4