I want to put letters and numbers in the list as a set.

Asked 2 years ago, Updated 2 years ago, 24 views

date
>a
12345
12345
1234

>b
12345
1234

>c
12345
12

x = [ ]
A = [ ]

with open("date") asf:
    for line inf:
     new_line = line
     len_count = len(new_line)

     if new_line.startswith(">"):
         print(new_line)
         A="
         x.append(new.line)

     else:
         A+=new_line

     if new_len == 0:
         print(len(A))
         x.append(len(A))
         A="

After recognizing > like this, print
If not, store the numbers in A.
Print the number of characters stored in A when an empty line is recognized. Apart from that, I would like to add [(a, 14), (b, 9), (c, 7)] to x.
The goal is to finally find the characters that take the maximum value

The ideal outcome is

a
14

B
9

c
7

max a —14
minc —7

Yes, I would like to find max/min in the x list.

python

2022-09-30 19:51

2 Answers

You may want to make some changes to your previous answers.

import re

with open("date") asf:
  assoc = {}
  for pin f.read().split("\n\n"):
    if not re.match ('^>', p): continue
    arr = p.split("\n")
    assoc[arr[0][1:]]=sum(map(len,arr[1:]))

  print(assoc)
  sa=sorted (assoc.items(), key=lambdax:x[1])
  print('max{}:{}'.format(sa[-1][0], sa[-1][1]))
  print('min{}:{}'.format(sa[0][0], sa[0][1]))

# {'a':14, 'b':9, 'c':7}
# max a —14
# minc —7


2022-09-30 19:51

I think it's easier to achieve your ultimate goal if you set x to list type than dictionary type.

The code in the question does not seem to resolve the fact that the line contains new line characters or that the variable A is not used properly used.
Why don't you cut the cord by referring to the code below and find out where it doesn't work?

Create #date
s="">a
12345
12345
1234

>b
12345
1234

>c
12345
12""" 

with open("date", "w") as f:
    f.write(s)

# output a dictionary value corresponding to the key
defprint_dic(key):
    print(key)
    print(x[key])
    print()

x = {}# dictionary type
A = [ ]

with open("date") asf:
    for line in f.read().splitlines():
        count=len(line)
        ifline.startswith(">"):
            # Set the key and reset variable A if it starts with ">"
            key=line[1:].trip()
            x[key] = 0
            A = [ ]
        elif line==":
            # Outputs the current key value if empty
            print_dic(key)
        else:
            # A only adds items
            x[key]+=count
            A.append(line)

print_dic(key)

# maximum acquisition
max=max(x.items(), key=lambdax:x[1])
print("max{}:{}".format(max[0], max[1]))

# minimum acquisition
min=min(x.items(), key=lambdax:x[1])
print("min{}:{}".format(min[0], min[1]))


2022-09-30 19:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.