Python Forum
Sorting by average ascii value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting by average ascii value
#7
(Sep-07-2021, 08:19 PM)deanhystad Wrote: Surprised you don't understand ordinal since ord() is a function that returns the ordinal value of a unicode character.

From your example it appears you are using ord() to get the average ascii value. ord() returns an integer. How would you compute the average of a list of integers?

Why are you using "word" when you pass "word" as an argument to sorting_by_ascii_average(lst). You should be using "lst". I know they are the same, but it is poor programming to pass an argument to a function and not use it. All your sort functions should take an argument which is the list to sort, and they should refer to that list using the function argument name. Use of global values should be kept to a minimum and global variables should not be used as a way to pass parameters to a function..

How do you provide a key function to sort? There are lots of examples. Are you wondering how you would call the function?
ef bubble_sort(items, key):
    items = items.copy()  # Do not alter origional items list
    for i in range(len(items)-1, -1, -1):
        for j in range(i):
            if key(items[j]) > key(items[j+1]):
                items[j], items[j+1] = items[j+1], items[j]
    return items

words = input('Enter words: ').split()

print('Words:', words)
print('Sorted by length:', bubble_sort(words, key=len))
print('Sorted lexically (ignoring case):', bubble_sort(words, key=str.lower))
Output:
Words: ['Now', 'is', 'the', 'time', 'for', 'all', 'good', 'programmers', 'to', 'learn', 'Python'] Sorted by length: ['is', 'to', 'Now', 'the', 'for', 'all', 'time', 'good', 'learn', 'Python', 'programmers'] Sorted lexically (ignoring case): ['all', 'for', 'good', 'is', 'learn', 'Now', 'programmers', 'Python', 'the', 'time', 'to']

Oh about ordinal, I didnt know its the whole word for ord ( first time for me, I just knew that ord gets ascii ).

About : From your example it appears you are using ord() to get the average ascii value. ord() returns an integer. How would you compute the average of a list of integers?

answer: I know how to find the average of the whole list, but the problem is, I need to sort my list with strings based on ascii average.
I mean, if a list contains: ["hey", "hello"], I need to find the ascii of h,e,y in separate and ascii of h,e,l,l,o in separate and then sum the h,e,y and the other one, and then divide by the number of letter in the string.
But that is my problem, I cant manage to separate the strings in the list for it... I can only find the ord in the all letter in list, like that: h,e,y,h,e,l,l,o - without separating ( like the comment I did above ).

About: Word - yea I know, it kind of poor programing, I did it by accident at the beginning and forgot to change it. Ill change it now.

About: words = input('Enter words: ').split() ( in code )
didnt understand it, what it does? I didnt learn the split function, saw it before, tried to learn it, couldn't understand a thing... what is the point of the words =?

Ill try to change my function now so it will do like yours, thanks :) was really good idea, although its really out of my league to do something like that.. to think of something like that, its like top programming... using the key function, thinking of it and such... ( I am a complete beginner ).

About: the second comment:
No idea... How does the average of int in list will help me? I dont know ("modify the function to receive a string and generate a list of integers where each integer is the ordinal value of one of the letters in the string)...
Is it possible to do it as a beginner? seriously asking...
forgot to mention, but also the question said that once I write stop at the list, It will stop writing strings and not add the stop ( wrote it at the question, you probably missed it, but it doenst matter, with the words, I will just stick to the function I did ).
But ill be happy to get answers to the other questions I wrote.

EDIT:
There is something I just found online by searching the key function.
The key itself is some sort of sorting system, its not allowed to me.
I need to sort without using sort, sorted, reverse or any other system like that, only by using some sort functions - like bubble or selection like I did.
If a key is not a sort system, I will be happy to use it ( I will be happy if you can clarify that to me ).


Thanks.
I changed the code to be a little more understandable:

def words_to_list():
    growing_list = []  # Creating an empty list.
    while True:  # Continue till string: "stop".
        enter_word = input("Please enter your word: ")
        growing_list.append(enter_word)  # Adding the words to the list in each iteration.
        if enter_word == "stop":
            growing_list.remove("stop")
            break  # Finish function.
    return growing_list


words = words_to_list()
print(f"List not sorted: {words}")


def lexicography_list(lst):
    for i in range(len(lst) -1, -1, -1):
        for j in range(i):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]
    return lst


lex_list = lexicography_list(words)
print(f"List sorted by alphabet: {lex_list}")


def sorting_by_length(lst):
    for i in range(len(lst) - 1, -1, -1):
        for j in range(i):
            if len(lst[j]) > len(lst[j + 1]):
                lst[j], lst[j + 1] = lst[j + 1], lst[j]
    return lst


length_list = sorting_by_length(words)
print(f"List sorted by length: {length_list}")
EDIT:
Just saw something about the ascii thingy: https://stackoverflow.com/questions/1806...-in-python
I saw there chr, no idea what it is, but it seems like it converts the ascii back to strings, I will try it maybe... see If I can work it out...

______________________________________________________________________________________________________________________________________________________________________________
LAST EDIT:

I tried something as you said above.
1. Getting the strings on the list.
2. Turning them to intergers ( by ord )
3. Having a list full of numbers ( which are int of string )
4. Summing all the numbers
5. Doing average
That's it.
The only problem is that, as I said, I dont know how to separate the strings / intergers from other intergers / string in the list.
I did that code: ( dont mind the variable names here, or the 2 definitions, and the long code. I can fix it later on, its on a new pywindow, just for trying it out ).
words = ["hello", "my", "dear", "properly"]

def list_of_letter_ascii_values(lst):
    word = ", ".join(words)
    listed = []
    for i in word:
        listed.append(ord(i))
    return listed

x = list_of_letter_ascii_values(words)
print(x)

def average_ascii_values(lst):
    total = 0
    for i in x:
        total += i
    average = total / len(x)
    return total, average

y, z = average_ascii_values(x)
print(y, z)
The problem with this code, it returns the average of all the int in the lists, If I knew how to separate, it was good ( the reason I used join before, but it also didnt help )
Reply


Messages In This Thread
Sorting by average ascii value - by ben1122 - Sep-07-2021, 06:33 PM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 07:26 PM
RE: Sorting by average ascii value - by ben1122 - Sep-07-2021, 07:42 PM
RE: Sorting by average ascii value - by ben1122 - Sep-07-2021, 08:05 PM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 08:19 PM
RE: Sorting by average ascii value - by ben1122 - Sep-08-2021, 11:46 AM
RE: Sorting by average ascii value - by deanhystad - Sep-07-2021, 08:42 PM
RE: Sorting by average ascii value - by deanhystad - Sep-08-2021, 04:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 6,060 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020