Converting each entry of each list from string to integer - python

I have lots of lists which contain integers as strings. e.g. scored_h0=['2','3','4']. I'm trying to convert each entry of each list from a string to an integer with the code below, but for some reason it's not working. There is no error, it's just when I print (scored_h0) (for example) after running the code below, the entries haven't been converted.
power_list = [scored_h0, scored_h1, conceded_h0, conceded_h1, scored_a0, scored_a1, conceded_a0, conceded_a1]
for list1 in power_list:
list1 = list(map(int, list1))

you are assigning a new value to the variable list1, but not mutating the actual array. If you want to mutate all these arrays you'll have to change each value in each of them, like so:
for list1 in power_list:
for i in range(len(list1)):
list1[i] = int(list1[i])

Are you looking for something like this?
scored_h0 = ['1', '2', '3']
scored_h1 = ['4', '5', '6']
power_list = [scored_h0, scored_h1]
for i in range(len(power_list)):
for j in range(len(power_list[i])):
power_list[i][j] = int(power_list[i][j])
print(scored_h0)
print(scored_h1)
It prints:
[1, 2, 3]
[4, 5, 6]

The following method do the job
def to_int(s):
for l in s:
for id, val in enumerate(l):
l[id] = int(val)
So you call it as to_int(power_list)

Related

How to flatten a list of lists with a known separator in a single operation

Suppose I have a list with a known separator ;. How would I flatten that list if it ever contains a string separated by that separator? For example:
>>> [item for item in [1,2,'2;3']]
[1, 2, '2;3']
I can do this verbosely with a second for loop:
vals = []
separator = ';'
for _val in [1,2,'2;3']:
_val = str(_val)
if separator not in _val:
vals.append(_val)
else:
vals.extend(_val.split(separator))
Is there a way to do this in a single operation, such as with a list comprehension or chain ?
Since you want the final values to all be stings, you can call split on all of them after converting to string and useitertools.chain.from_iterable:
import itertools
c = itertools.chain.from_iterable(str(item).split(";") for item in [1,2,'2;3'])
print(list(c))
result:
['1', '2', '2', '3']
You can use list comprehension (delete int type cast if you don't need items to be int):
test = [1, 2, "3;4", 5]
res = [int(item) for items in test for item in str(items).split(";")]
print(res)
Result:
[1, 2, 3, 4, 5]
I would do this with a generator if you want all values to be int:
def values(items):
for x in items:
if isinstance(x, str) and separator in x:
yield from map(int, x.split(separator))
yield x
separator = ';'
vals = list(values([ 1, 2, '2;3' ]))
This could also be achieved without imports using a nested list comprehension:
sep = ';'
a = [item for sublist in [str(i).split(sep) for i in lst] for item in sublist]
outputs:
['1', '2', '2', '3']

Matching and Combining Multiple 2D lists in Python

I am trying to combine (2) 2D lists based on a common value in both lists.
The values in the list are unique so there is nothing to take in to account for a list entry having any of the same values.
The example is:
list1 = [['hdisk37', '00f7e0b88577106a']]
list2 = [['1', '00f7e0b8cee02cd6'], ['2', '00f7e0b88577106a']]
With the desired result of:
list3 = [['hdisk37', '00f7e0b88577106a','2']]
The common value is at list1[0][1] and list2[1][1].
The pythonic way to get the needed result using set objects:
list1 = [['hdisk37', '00f7e0b88577106a']]
list2 = [['1', '00f7e0b8cee02cd6'], ['2', '00f7e0b88577106a']]
set1 = set(list1[0])
list3 = [list(set1 | s) for s in map(set, list2) if set1 & s]
print(list3)
The output:
[['00f7e0b88577106a', '2', 'hdisk37']]
set1 & s is intersection of two sets(returns a new set with elements common to the first set and all others)
set1 | s is union of a specified sets
Try this:
result = []
for inner_list1 in list1:
for inner_list2 in list2:
set1 = set(inner_list1)
set2 = set(inner_list1)
if set1.intersection(set2):
result.append(list(set1.union(set2)))
For each inner list in both lists, check if the intersection between them is not empty. In case it isn't, they are both merged and added to the final result.
This method returns all the possible "second value" matches as a dict, from the second value to the resulting list. It also takes an arbitrary number of these lists of lists (not just two).
import collections
a = [['hdisk37', '00f7e0b88577106a']]
b = [['1', '00f7e0b8cee02cd6'], ['2', '00f7e0b88577106a']]
def combine(*lols): # list of lists
ret = collections.defaultdict(set)
for lol in lols:
for l in lol:
ret[l[1]].add(l[1])
ret[l[1]].add(l[0])
return {k:list(v) for k,v in ret.items()}
print combine(a,b)
Output:
$ python test.py
{'00f7e0b8cee02cd6': ['00f7e0b8cee02cd6', '1'], '00f7e0b88577106a': ['hdisk37', '2', '00f7e0b88577106a']}
To get your exact output requested, you'd do:
combine(list1, list2).get('00f7e0b88577106a')
If you wanna try something different you could do a
merger = lambda x,y : set(x)|set(y) if set(x)&set(y) else x
results = []
for item in list1:
result = reduce(merger,[item]+list2)
if isinstance(result,set):
results.append(result)
print results

I want my program to sort the value 'score' highest to lowest [duplicate]

I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of "numbers" that are actually in string form, so I first convert them to ints, then attempt a sort.
list1=["1","10","3","22","23","4","2","200"]
for item in list1:
item=int(item)
list1.sort()
print list1
Gives me:
['1', '10', '2', '200', '22', '23', '3', '4']
What I want is
['1','2','3','4','10','22','23','200']
I've looked around for some of the algorithms associated with sorting numeric sets, but the ones I found all involve sorting alphanumeric sets.
I know this is probably a no brainer problem but google and my textbook don't offer anything more or less useful than the .sort() function.
You haven't actually converted your strings to ints. Or rather, you did, but then you didn't do anything with the results. What you want is:
list1 = ["1","10","3","22","23","4","2","200"]
list1 = [int(x) for x in list1]
list1.sort()
If for some reason you need to keep strings instead of ints (usually a bad idea, but maybe you need to preserve leading zeros or something), you can use a key function. sort takes a named parameter, key, which is a function that is called on each element before it is compared. The key function's return values are compared instead of comparing the list elements directly:
list1 = ["1","10","3","22","23","4","2","200"]
# call int(x) on each element before comparing it
list1.sort(key=int)
# or if you want to do it all in the same line
list1 = sorted([int(x) for x in list1])
I approached the same problem yesterday and found a module called natsort, which solves your problem. Use:
from natsort import natsorted # pip install natsort
# Example list of strings
a = ['1', '10', '2', '3', '11']
[In] sorted(a)
[Out] ['1', '10', '11', '2', '3']
[In] natsorted(a)
[Out] ['1', '2', '3', '10', '11']
# Your array may contain strings
[In] natsorted(['string11', 'string3', 'string1', 'string10', 'string100'])
[Out] ['string1', 'string3', 'string10', 'string11', 'string100']
It also works for dictionaries as an equivalent of sorted.
You could pass a function to the key parameter to the .sort method. With this, the system will sort by key(x) instead of x.
list1.sort(key=int)
BTW, to convert the list to integers permanently, use the map function
list1 = list(map(int, list1)) # you don't need to call list() in Python 2.x
or list comprehension
list1 = [int(x) for x in list1]
In case you want to use sorted() function: sorted(list1, key=int)
It returns a new sorted list.
You can also use:
import re
def sort_human(l):
convert = lambda text: float(text) if text.isdigit() else text
alphanum = lambda key: [convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key)]
l.sort(key=alphanum)
return l
This is very similar to other stuff that you can find on the internet but also works for alphanumericals like [abc0.1, abc0.2, ...].
Python's sort isn't weird. It's just that this code:
for item in list1:
item=int(item)
isn't doing what you think it is - item is not replaced back into the list, it is simply thrown away.
Anyway, the correct solution is to use key=int as others have shown you.
Seamus Campbell's answer doesn't work on Python 2.x.
list1 = sorted(list1, key=lambda e: int(e)) using lambda function works well.
Try this, it’ll sort the list in-place in descending order (there’s no need to specify a key in this case):
Process
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC
output:
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
The most recent solution is right. You are reading solutions as a string, in which case the order is 1, then 100, then 104 followed by 2 then 21, then 2001001010, 3 and so forth.
You have to CAST your input as an int instead:
sorted strings:
stringList = (1, 10, 2, 21, 3)
sorted ints:
intList = (1, 2, 3, 10, 21)
To cast, just put the stringList inside int ( blahblah ).
Again:
stringList = (1, 10, 2, 21, 3)
newList = int (stringList)
print newList
=> returns (1, 2, 3, 10, 21)
real problem is that sort sorts things alphanumerically. So if you have a list
['1', '2', '10', '19'] and run sort you get ['1', '10'. '19', '2']. ie 10 comes before 2 because it looks at the first character and sorts starting from that.
It seems most methods in python return things in that order. For example if you have a directory named abc with the files labelled as 1.jpg, 2.jpg etc say up to 15.jpg and you do
file_list=os.listdir(abc) the file_list is not ordered as you expect but rather as
file_list=['1.jpg', '11.jpg'---'15.jpg', '2.jpg]. If the order in which files are processed is
important (presumably that's why you named them numerically) the order is not what you think it will be. You can avoid this by using "zeros" padding. For example if you have a list
alist=['01', '03', '05', '10', '02','04', '06] and you run sort on it you get the order you
wanted. alist=['01', '02' etc] because the first character is 0 which comes before 1. The amount of zeros padding you need is determined by the largest value in the list.For example if the largest is say between 100 and 1000 you need to pad single digits as 001, 002 ---010,011--100, 101 etc.
If you want to use strings of the numbers better take another list as shown in my code it will work fine.
list1=["1","10","3","22","23","4","2","200"]
k=[]
for item in list1:
k.append(int(item))
k.sort()
print(k)
# [1, 2, 3, 4, 10, 22, 23, 200]
Simple way to sort a numerical list
numlists = ["5","50","7","51","87","97","53"]
results = list(map(int, numlists))
results.sort(reverse=False)
print(results)
may be not the best python, but for string lists like
['1','1.0','2.0','2', '1.1', '1.10', '1.11', '1.2','7','3','5']with the expected target
['1', '1.0', '1.1', '1.2', '1.10', '1.11', '2', '2.0', '3', '5', '7'] helped me...
unsortedList = ['1','1.0','2.0','2', '1.1', '1.10', '1.11', '1.2','7','3','5']
sortedList = []
sortDict = {}
sortVal = []
#set zero correct (integer): examp: 1.000 will be 1 and breaks the order
zero = "000"
for i in sorted(unsortedList):
x = i.split(".")
if x[0] in sortDict:
if len(x) > 1:
sortVal.append(x[1])
else:
sortVal.append(zero)
sortDict[x[0]] = sorted(sortVal, key = int)
else:
sortVal = []
if len(x) > 1:
sortVal.append(x[1])
else:
sortVal.append(zero)
sortDict[x[0]] = sortVal
for key in sortDict:
for val in sortDict[key]:
if val == zero:
sortedList.append(str(key))
else:
sortedList.append(str(key) + "." + str(val))
print(sortedList)
scores = ['91','89','87','86','85']
scores.sort()
print (scores)
This worked for me using python version 3, though it didn't in version 2.

Convert all strings in a list to int

How do I convert all strings in a list to integers?
['1', '2', '3'] ⟶ [1, 2, 3]
Given:
xs = ['1', '2', '3']
Use map then list to obtain a list of integers:
list(map(int, xs))
In Python 2, list was unnecessary since map returned a list:
map(int, xs)
Use a list comprehension on the list xs:
[int(x) for x in xs]
e.g.
>>> xs = ["1", "2", "3"]
>>> [int(x) for x in xs]
[1, 2, 3]
There are several methods to convert string numbers in a list to integers.
In Python 2.x you can use the map function:
>>> results = ['1', '2', '3']
>>> results = map(int, results)
>>> results
[1, 2, 3]
Here, It returns the list of elements after applying the function.
In Python 3.x you can use the same map
>>> results = ['1', '2', '3']
>>> results = list(map(int, results))
>>> results
[1, 2, 3]
Unlike python 2.x, Here map function will return map object i.e. iterator which will yield the result(values) one by one that's the reason further we need to add a function named as list which will be applied to all the iterable items.
Refer to the image below for the return value of the map function and it's type in the case of python 3.x
The third method which is common for both python 2.x and python 3.x i.e List Comprehensions
>>> results = ['1', '2', '3']
>>> results = [int(i) for i in results]
>>> results
[1, 2, 3]
You can easily convert string list items into int items using loop shorthand in python
Say you have a string result = ['1','2','3']
Just do,
result = [int(item) for item in result]
print(result)
It'll give you output like
[1,2,3]
If your list contains pure integer strings, the accepted answer is the way to go. It will crash if you give it things that are not integers.
So: if you have data that may contain ints, possibly floats or other things as well - you can leverage your own function with errorhandling:
def maybeMakeNumber(s):
"""Returns a string 's' into a integer if possible, a float if needed or
returns it as is."""
# handle None, "", 0
if not s:
return s
try:
f = float(s)
i = int(f)
return i if f == i else f
except ValueError:
return s
data = ["unkind", "data", "42", 98, "47.11", "of mixed", "types"]
converted = list(map(maybeMakeNumber, data))
print(converted)
Output:
['unkind', 'data', 42, 98, 47.11, 'of mixed', 'types']
To also handle iterables inside iterables you can use this helper:
from collections.abc import Iterable, Mapping
def convertEr(iterab):
"""Tries to convert an iterable to list of floats, ints or the original thing
from the iterable. Converts any iterable (tuple,set, ...) to itself in output.
Does not work for Mappings - you would need to check abc.Mapping and handle
things like {1:42, "1":84} when converting them - so they come out as is."""
if isinstance(iterab, str):
return maybeMakeNumber(iterab)
if isinstance(iterab, Mapping):
return iterab
if isinstance(iterab, Iterable):
return iterab.__class__(convertEr(p) for p in iterab)
data = ["unkind", {1: 3,"1":42}, "data", "42", 98, "47.11", "of mixed",
("0", "8", {"15", "things"}, "3.141"), "types"]
converted = convertEr(data)
print(converted)
Output:
['unkind', {1: 3, '1': 42}, 'data', 42, 98, 47.11, 'of mixed',
(0, 8, {'things', 15}, 3.141), 'types'] # sets are unordered, hence diffrent order
A little bit more expanded than list comprehension but likewise useful:
def str_list_to_int_list(str_list):
n = 0
while n < len(str_list):
str_list[n] = int(str_list[n])
n += 1
return(str_list)
e.g.
>>> results = ["1", "2", "3"]
>>> str_list_to_int_list(results)
[1, 2, 3]
Also:
def str_list_to_int_list(str_list):
int_list = [int(n) for n in str_list]
return int_list
Here is a simple solution with explanation for your query.
a=['1','2','3','4','5'] #The integer represented as a string in this list
b=[] #Fresh list
for i in a: #Declaring variable (i) as an item in the list (a).
b.append(int(i)) #Look below for explanation
print(b)
Here, append() is used to add items ( i.e integer version of string (i) in this program ) to the end of the list (b).
Note: int() is a function that helps to convert an integer in the form of string, back to its integer form.
Output console:
[1, 2, 3, 4, 5]
So, we can convert the string items in the list to an integer only if the given string is entirely composed of numbers or else an error will be generated.
You can do it simply in one line when taking input.
[int(i) for i in input().split("")]
Split it where you want.
If you want to convert a list not list simply put your list name in the place of input().split("").
I also want to add Python | Converting all strings in list to integers
Method #1 : Naive Method
# Python3 code to demonstrate
# converting list of strings to int
# using naive method
# initializing list
test_list = ['1', '4', '3', '6', '7']
# Printing original list
print ("Original list is : " + str(test_list))
# using naive method to
# perform conversion
for i in range(0, len(test_list)):
test_list[i] = int(test_list[i])
# Printing modified list
print ("Modified list is : " + str(test_list))
Output:
Original list is : ['1', '4', '3', '6', '7']
Modified list is : [1, 4, 3, 6, 7]
Method #2 : Using list comprehension
# Python3 code to demonstrate
# converting list of strings to int
# using list comprehension
# initializing list
test_list = ['1', '4', '3', '6', '7']
# Printing original list
print ("Original list is : " + str(test_list))
# using list comprehension to
# perform conversion
test_list = [int(i) for i in test_list]
# Printing modified list
print ("Modified list is : " + str(test_list))
Output:
Original list is : ['1', '4', '3', '6', '7']
Modified list is : [1, 4, 3, 6, 7]
Method #3 : Using map()
# Python3 code to demonstrate
# converting list of strings to int
# using map()
# initializing list
test_list = ['1', '4', '3', '6', '7']
# Printing original list
print ("Original list is : " + str(test_list))
# using map() to
# perform conversion
test_list = list(map(int, test_list))
# Printing modified list
print ("Modified list is : " + str(test_list))
Output:
Original list is : ['1', '4', '3', '6', '7']
Modified list is : [1, 4, 3, 6, 7]
The answers below, even the most popular ones, do not work for all situations. I have such a solution for super resistant thrust str.
I had such a thing:
AA = ['0', '0.5', '0.5', '0.1', '0.1', '0.1', '0.1']
AA = pd.DataFrame(AA, dtype=np.float64)
AA = AA.values.flatten()
AA = list(AA.flatten())
AA
[0.0, 0.5, 0.5, 0.1, 0.1, 0.1, 0.1]
You can laugh, but it works.
Coerce invalid input
int() raises an error if an invalid value is fed. If you want to set these invalid values to NaN and convert the valid values in the list (similar to how pandas' to_numeric behaves), you can do so using the following list comprehension:
[int(x) if x.replace('-','', 1).replace('+','',1).isdecimal() else float('nan') for x in lst]
It is essentially checking if a value is decimal or not (either negative or positive). Scientific notation (e.g. 1e3) can also be a valid integer, in that case, we can add another condition to the comprehension (albeit less legible):
[int(x) if x.replace('-','', 1).replace('+','',1).isdecimal() else int(e[0])*10**int(e[1]) if (e:=x.split('e',1))[1:] and e[1].isdecimal() else float('nan') for x in lst]
For lst = ['+1', '2', '-3', 'string', '4e3'], the above comprehension returns [1, 2, -3, nan, 4000]. With minor adjustments, we can make it handle floats too but that's a separate topic.
map() is faster than list comprehension
map() is about 64% faster than the list comprehension. As can be seen from the following performance plot, map() outperforms list comprehension regardless of list size.
The code used to produce the plot:
from random import choices, randint
from string import digits
from perfplot import plot
plot(
setup=lambda n: [''.join(choices(digits, k=randint(1,10))) for _ in range(n)],
kernels=[lambda lst: [int(x) for x in lst], lambda lst: list(map(int, lst))],
labels= ["[int(x) for x in lst]", "list(map(int, lst))"],
n_range=[2**k for k in range(4, 22)],
xlabel='Number of items',
title='Converting strings to integers',
equality_check=lambda x,y: x==y);

How to sort a list of strings numerically?

I know that this sounds trivial but I did not realize that the sort() function of Python was weird. I have a list of "numbers" that are actually in string form, so I first convert them to ints, then attempt a sort.
list1=["1","10","3","22","23","4","2","200"]
for item in list1:
item=int(item)
list1.sort()
print list1
Gives me:
['1', '10', '2', '200', '22', '23', '3', '4']
What I want is
['1','2','3','4','10','22','23','200']
I've looked around for some of the algorithms associated with sorting numeric sets, but the ones I found all involve sorting alphanumeric sets.
I know this is probably a no brainer problem but google and my textbook don't offer anything more or less useful than the .sort() function.
You haven't actually converted your strings to ints. Or rather, you did, but then you didn't do anything with the results. What you want is:
list1 = ["1","10","3","22","23","4","2","200"]
list1 = [int(x) for x in list1]
list1.sort()
If for some reason you need to keep strings instead of ints (usually a bad idea, but maybe you need to preserve leading zeros or something), you can use a key function. sort takes a named parameter, key, which is a function that is called on each element before it is compared. The key function's return values are compared instead of comparing the list elements directly:
list1 = ["1","10","3","22","23","4","2","200"]
# call int(x) on each element before comparing it
list1.sort(key=int)
# or if you want to do it all in the same line
list1 = sorted([int(x) for x in list1])
I approached the same problem yesterday and found a module called natsort, which solves your problem. Use:
from natsort import natsorted # pip install natsort
# Example list of strings
a = ['1', '10', '2', '3', '11']
[In] sorted(a)
[Out] ['1', '10', '11', '2', '3']
[In] natsorted(a)
[Out] ['1', '2', '3', '10', '11']
# Your array may contain strings
[In] natsorted(['string11', 'string3', 'string1', 'string10', 'string100'])
[Out] ['string1', 'string3', 'string10', 'string11', 'string100']
It also works for dictionaries as an equivalent of sorted.
You could pass a function to the key parameter to the .sort method. With this, the system will sort by key(x) instead of x.
list1.sort(key=int)
BTW, to convert the list to integers permanently, use the map function
list1 = list(map(int, list1)) # you don't need to call list() in Python 2.x
or list comprehension
list1 = [int(x) for x in list1]
In case you want to use sorted() function: sorted(list1, key=int)
It returns a new sorted list.
You can also use:
import re
def sort_human(l):
convert = lambda text: float(text) if text.isdigit() else text
alphanum = lambda key: [convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key)]
l.sort(key=alphanum)
return l
This is very similar to other stuff that you can find on the internet but also works for alphanumericals like [abc0.1, abc0.2, ...].
Python's sort isn't weird. It's just that this code:
for item in list1:
item=int(item)
isn't doing what you think it is - item is not replaced back into the list, it is simply thrown away.
Anyway, the correct solution is to use key=int as others have shown you.
Seamus Campbell's answer doesn't work on Python 2.x.
list1 = sorted(list1, key=lambda e: int(e)) using lambda function works well.
Try this, it’ll sort the list in-place in descending order (there’s no need to specify a key in this case):
Process
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC
output:
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
The most recent solution is right. You are reading solutions as a string, in which case the order is 1, then 100, then 104 followed by 2 then 21, then 2001001010, 3 and so forth.
You have to CAST your input as an int instead:
sorted strings:
stringList = (1, 10, 2, 21, 3)
sorted ints:
intList = (1, 2, 3, 10, 21)
To cast, just put the stringList inside int ( blahblah ).
Again:
stringList = (1, 10, 2, 21, 3)
newList = int (stringList)
print newList
=> returns (1, 2, 3, 10, 21)
real problem is that sort sorts things alphanumerically. So if you have a list
['1', '2', '10', '19'] and run sort you get ['1', '10'. '19', '2']. ie 10 comes before 2 because it looks at the first character and sorts starting from that.
It seems most methods in python return things in that order. For example if you have a directory named abc with the files labelled as 1.jpg, 2.jpg etc say up to 15.jpg and you do
file_list=os.listdir(abc) the file_list is not ordered as you expect but rather as
file_list=['1.jpg', '11.jpg'---'15.jpg', '2.jpg]. If the order in which files are processed is
important (presumably that's why you named them numerically) the order is not what you think it will be. You can avoid this by using "zeros" padding. For example if you have a list
alist=['01', '03', '05', '10', '02','04', '06] and you run sort on it you get the order you
wanted. alist=['01', '02' etc] because the first character is 0 which comes before 1. The amount of zeros padding you need is determined by the largest value in the list.For example if the largest is say between 100 and 1000 you need to pad single digits as 001, 002 ---010,011--100, 101 etc.
If you want to use strings of the numbers better take another list as shown in my code it will work fine.
list1=["1","10","3","22","23","4","2","200"]
k=[]
for item in list1:
k.append(int(item))
k.sort()
print(k)
# [1, 2, 3, 4, 10, 22, 23, 200]
Simple way to sort a numerical list
numlists = ["5","50","7","51","87","97","53"]
results = list(map(int, numlists))
results.sort(reverse=False)
print(results)
may be not the best python, but for string lists like
['1','1.0','2.0','2', '1.1', '1.10', '1.11', '1.2','7','3','5']with the expected target
['1', '1.0', '1.1', '1.2', '1.10', '1.11', '2', '2.0', '3', '5', '7'] helped me...
unsortedList = ['1','1.0','2.0','2', '1.1', '1.10', '1.11', '1.2','7','3','5']
sortedList = []
sortDict = {}
sortVal = []
#set zero correct (integer): examp: 1.000 will be 1 and breaks the order
zero = "000"
for i in sorted(unsortedList):
x = i.split(".")
if x[0] in sortDict:
if len(x) > 1:
sortVal.append(x[1])
else:
sortVal.append(zero)
sortDict[x[0]] = sorted(sortVal, key = int)
else:
sortVal = []
if len(x) > 1:
sortVal.append(x[1])
else:
sortVal.append(zero)
sortDict[x[0]] = sortVal
for key in sortDict:
for val in sortDict[key]:
if val == zero:
sortedList.append(str(key))
else:
sortedList.append(str(key) + "." + str(val))
print(sortedList)
scores = ['91','89','87','86','85']
scores.sort()
print (scores)
This worked for me using python version 3, though it didn't in version 2.

Categories

Resources