Find Python Matching Numeric Locations

Asked 2 years ago, Updated 2 years ago, 13 views

Hi, everyone. I'm a beginner at Python If the stored data matches a number that is the same value as the number of times you enter it, I'd like to print out all the matching numbers

Stored data (data.txt)

1012137
8
1012136
4
1012135
3
1012134
0
1012133
4
1012132
8
1012131
4 .
..

For example, if you enter the range number 1012137 and 1012136, the values are 8 and 4.

I'd like to know the turn number of all the data whose values match in the order of 8 and 4 (e.g. 1012132, 1012131)

f1 = open('data.txt', mode='r', encoding='utf-8')
lines = f1.readlines()
start = int (input ("Enter low query count: ")))
last = int (input ("Enter high query count: ")))

# Include in array as many sizes as you enter
lst = []
for i in range(start, last + 1):
    lst.append(str(i) + '\n')    
    lst.reverse()

# Find the same element between two lists
list_group = list(set(lst).intersection(lines))
list_group.sort()
print(list_group)

# Find the same element index in lines (per turn)
index_num = []
for i in list_group:
    for d in lines:    
        if d == i:    
            index_num.append(lines.index(d))
index_num.sort()

# Find an index to contain the next number by index
index_odd_oven = []
Duplicate_list = [] # Find duplicate
for i in index_num:
    index_odd_oven.append(i + 1)    
    Duplicate_list.append(i)


# Duplicate Find (Error)
a = []
for i in Duplicate_list:
    a.append(lines[i])  


word_cnt = dict()
for word in a:
    if word not in word_cnt.keys():    
        word_cnt[word] = 1    
    else:    
        word_cnt[word] += 1

for key, value in word_cnt.items():
    if value > 1:    
        print("error========error=======error===========")    
        print(key, value)

value = []
for i in index_odd_oven:       # [0, 2]
    value.append(lines[i])      #  ['8\n', '4\n']    

f1.close()

I want to compare the values 8 and 4 with the whole data and find all the numbers that match the values How do I code it?

I'm sorry and thank you for the beginner's question.

python

2022-09-20 21:32

1 Answers

First, organize your data and logic again, think about it, and then implement it.

The data consists of the key, which is the value, and the value, which is the number of the turn.

The criterion you are looking for is 'value' rather than the number of times, and key is 'value' and value is the number of times.

If the value is an array, it will be easy to find the turn number when you know the key value. In that case, the status of the key-value pair is as follows.

Value: round numbers

1 : [1012121, 1012123, 1012111]

2 : [1012124, 1012128, 1012116, 1012105]

3 : [...]

...

There are many ways to organize as above, but the simplest way, for statement and dictionary, is as follows.

Key in dictionary does not allow duplication.

dict = {}
lines = list(map(int, f1.readlines()) # Change all values of the line to int type.
print(lines)
For i in range (0, len(lines)-1, 2): # key value pair, increments by 2.
    If lines[i + 1] in dict: # Add the corresponding number to the list if the dictionary already has a key
        dict[lines[i + 1]].append(lines[i])
    else: # Generate list if dictionary does not contain a key
        dict[lines[i + 1]] = [lines[i]];
print(dict)

I want to compare the values of 8 and 4 with the total data and find all the number of turns that match that value, so how do I code it?It was a long?

After organizing as above, you can combine dict[4] and dict[8] to get the desired result.

resultList = []
For v in value: # value is the same as the list of values obtained. (int list)
    resultList += dict[v]
print(resultList)

# It does not work if the value is ['8\n', '4\n'], because all of them have been changed to int type at the beginning. ex) value = [8, 4]

It's good to divide the code by explaining the steps and functions when you ask questions, but there are too many unrelated codes to the question. It is recommended not to insert irrelevant codes as much as possible. If it's long, no one reads it.

It would be nice if you could fill out the input/output, doctor code, and explanation.

Additionally, the last value is a variable name in the list that is severely inappropriate.

감사합니다.


2022-09-20 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.