I have a function called "organizer" which takes 2 values a list and an index(number), to reorganize this list, after that it return a new list.
I want to execute this function 10 times to create 10 lists and add this lists to a single list of lists for later use, the program acomplish this, however it repeat the last list 10 times, it does not add the other lists.
I put the function "organizer" in here,because Im unable to find where the problem relies so more information may be needed.
I have been using the print function along the function to see where it fails, and it creates the lists as I desire, the problem is that after creating them it just copy the last created list as many times as the loop goes on. It takes the final list produced and copy it several times
Here is the code:
number_list = ["12","11","10","9","8","7","6","5","4","3","2","1"]
indexes = [0,5,6,8,10,4,5,2,1,9]
def organizer(list,index): # Takes a list and reorganize it by a number which is an index.
number1 = index - 1
count_to_add = -1
list_to_add = []
for item in range(number1):
count_to_add += 1
a = list[count_to_add]
list_to_add.append(a)
number2 = index - 1
for item1 in range(number2):
del list[0]
list.extend(list_to_add)
return list
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
count = -1
for item in list_indexes:
count += 1
result = organizer(number_list, list_indexes[count])
final_list.append(result)
return final_list
final_output = lists_creator(indexes)
print(final_output)
Here is the result:(the same list 10 times)
[['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']]
Note: If I change the line 28 in list_creator function
final_list.append(result)
for
final_list.extend(result)
the result is:
['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']
Which is the result that I desire but is not a group of lists.
you are using the same list every time, so the result consists of actually one list,
in your list_creator method, you have to give to organizer method a copy of your original list number_list, otherwise you just mutate over and over again the same one list:
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
count = -1
for item in list_indexes:
count += 1
result = organizer(list(number_list), list_indexes[count])
final_list.append(result)
return final_list
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
list = []
for i in range(len(list_indexes)):
result = organizer(number_list, list_indexes[i])
list.extend(result)
n = 0
m = 12
for n_times in range(len(list_indexes)):
final_list.append(list[n:m])
n = m
m += 12
return final_list
Related
I have a list which has numbers, but because its appended to a list using for loop it has '\n' and I don't know how to remove it.
the list looks like this
['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
I want to remove ' ' and '\n' so it would look like this
[374559227,437159430,372410275,784516257,280661123,935683871,675547486]
Join to a string and split the newlines:
l = [
'3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5',
'9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n',
'7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6',
'1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n',
'6', '7', '5', '5', '4', '7', '4', '8', '6'
]
print([int(x) for x in ''.join(l).split('\n')])
>>> [374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]
You can use itertools.groupby:
>>> from itertools import groupby
>>> lst = ['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
>>> [int(''.join(digits)) for is_number, digits in groupby(lst, lambda x: x != '\n') if is_number]
[374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]
You can use reduce function
from functools import reduce
lst = ['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
lst_result = [int(n) for n in reduce(lambda x, y: f"{x}{y}", lst).split('\n')]
Output:
[374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I have created a bubble sort program in which the user is able to enter the values which will be stored in the array which will be sorted by the bubble sort algorithm, however the output is not what I expect if i enter the values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 will be sorted to just after 1 changing the sorted list to 1, 10, 2, 3, 4, 5, 6, 7, 8, 9 which makes me believe that 10 is being read as 1 instead of 10 but I have no idea why.
def initlist():
unsortedlist=[]
for counter in range (0,10):
print("Please enter value", (counter+1))
temp = input("")
unsortedlist.append(temp)
return unsortedlist
def BubbleSort(unsortedlist):
passes = 0
print ("Fixed Loop")
print ("Initial State:", unsortedlist)
passes = 0
swaps = 0
for outer in range(len(unsortedlist) -1, 0, -1):
passes += 1
for inner in range (outer):
if unsortedlist[inner] > unsortedlist[inner + 1]:
swaps +=1
temp = unsortedlist[inner]
unsortedlist[inner] = unsortedlist[inner+1]
unsortedlist[inner + 1] = temp
print("Pass", passes, ":", unsortedlist)
input("Press Any Key To Continue")
Output
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 9 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
temp = input("") reads input as strings. you are doing a lexicographical sorting here.
If you want to sort integers, then change to temp = int(input(""))
I need to complete an assignment which involves cleansing a list of lists in Python. If a sub-list contains an item that is non-numeric or numeric but greater than 20, I need to remove the sub-list and add it to a separate list.
My current code correctly removes some sub-lists but not others. I think it is because of two consecutive sub-lists with errors but I haven't been able to fix this. My code:
datalist = [['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['1', '30', '2', '5', '7'], ['14', '1', '2', '9', '3'], ['6', '9', '16', '0', ''], ['14', '11', 'forteen', '8', '20'], ['12', '11', '8', '15', '7'], ['18', '9', '9', '22', '4'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0'], ['einundzwanzig', '14', '1', '2', '4']]
invalidList = []
def validate(myList): #non-numeric values or values greater than 20 must be removed from myList and added to invalidList
for lst in myList: # check each list
for item in lst: # check element in each list
try:
val = int(item)
if val >20:
raise ValueError
except ValueError:
invalidList.append(lst)
myList.remove(lst)
return myList
The problematic sublist is:
['14', '11', 'forteen', '8', '20']
Actual output:
>>> print(validate(datalist)) # this should be the cleansed list
[['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['14', '1', '2', '9', '3'], ['14', '11', 'forteen', '8', '20'], ['12', '11', '8', '15', '7'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0']]
>>> print(invalidList)
[['1', '30', '2', '5', '7'], ['6', '9', '16', '0', ''], ['18', '9', '9', '22', '4'], ['einundzwanzig', '14', '1', '2', '4']]
Expected output:
>>> print(validate(datalist)) # this should be the cleansed list
[['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['14', '1', '2', '9', '3'], ['12', '11', '8', '15', '7'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0']]
>>> print(invalidList)
[['1', '30', '2', '5', '7'], ['6', '9', '16', '0', ''], ['14', '11', 'forteen', '8', '20'],['18', '9', '9', '22', '4'], ['einundzwanzig', '14', '1', '2', '4']]
Thanks in advance :)
The problem is that you change your list during the loop which leads to unexpected results. I'd advice against removing the element - just "mark it" for deletion and remove it just before returning.
This is an example way of doing that, without modifying much of your code:
datalist = [['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['1', '30', '2', '5', '7'], ['14', '1', '2', '9', '3'], ['6', '9', '16', '0', ''], ['14', '11', 'forteen', '8', '20'], ['12', '11', '8', '15', '7'], ['18', '9', '9', '22', '4'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0'], ['einundzwanzig', '14', '1', '2', '4']]
invalidList = []
def validate(myList): #non-numeric values or values greater than 20 must be removed from myList and added to invalidList
for lst in myList: # check each list
for item in lst:# check element in each list
try:
val = int(item)
if val >20:
raise ValueError
except ValueError:
invalidList.append(lst[:]) # copy the invalid list - otherwise the next line would break it because they share the list object
lst.clear() # this will change the invalid list
return [elem for elem in myList if elem] # empty list evaluate to False
Returned values:
>>> validate(datalist)
[['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['14', '1', '2', '9', '3'], ['12', '11', '8', '15', '7'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0']]
>>> invalidList
[['1', '30', '2', '5', '7'], ['6', '9', '16', '0', ''], ['14', '11', 'forteen', '8', '20'], ['18', '9', '9', '22', '4'], ['einundzwanzig', '14', '1', '2', '4']]
Why is it happening?
When you remove an item from the middle of the list, all further elements shift left.
This means, after removing an element, next element jumps into the removed one's place... but the loop goes on, onto the next place.
When your list contains two invalid elements in a row, the second one is always skipped because it jumps into that place, as noted below:
[['16', '10', '8', '3', '7'], #ok
['8', '9', '19', '20', '4'], #ok
['6', '8', '16', '5', '0'], #ok
['1', '30', '2', '5', '7'], #removed
['14', '1', '2', '9', '3'], #skipped! but ok
['6', '9', '16', '0', ''], #removed
['14', '11', 'forteen', '8', '20'], #skipped! but should've been removed
['12', '11', '8', '15', '7'], #ok
['18', '9', '9', '22', '4'], #removed
['1', '3', '14', '18', '20'], #skipped! but ok
['5', '3', '19', '20', '0'], #ok
['einundzwanzig', '14', '1', '2', '4']] #removed
This is one approach using any().
Ex:
datalist = [['16', '10', '8', '3', '7'], ['8', '9', '19', '20', '4'], ['6', '8', '16', '5', '0'], ['1', '30', '2', '5', '7'], ['14', '1', '2', '9', '3'], ['6', '9', '16', '0', ''], ['14', '11', 'forteen', '8', '20'], ['12', '11', '8', '15', '7'], ['18', '9', '9', '22', '4'], ['1', '3', '14', '18', '20'], ['5', '3', '19', '20', '0'], ['einundzwanzig', '14', '1', '2', '4']]
def validate(myList):
invalidList = []
validList = []
for i in myList:
if any(j=='' or j.isalpha() or int(j) > 20 for j in i):
invalidList.append(i)
else:
validList.append(i)
return validList, invalidList
print(validate(datalist))
Output:
([['16', '10', '8', '3', '7'],
['8', '9', '19', '20', '4'],
['6', '8', '16', '5', '0'],
['14', '1', '2', '9', '3'],
['12', '11', '8', '15', '7'],
['1', '3', '14', '18', '20'],
['5', '3', '19', '20', '0']],
[['1', '30', '2', '5', '7'],
['6', '9', '16', '0', ''],
['14', '11', 'forteen', '8', '20'],
['18', '9', '9', '22', '4'],
['einundzwanzig', '14', '1', '2', '4']])
You can achieve what you want with a one-liner as follows:
validlist = [sublist for sublist in datalist if all(i.isdigit() for i in sublist) and max([int(i) for i in sublist])<=20]
Output:
[['16', '10', '8', '3', '7'],
['8', '9', '19', '20', '4'],
['6', '8', '16', '5', '0'],
['14', '1', '2', '9', '3'],
['12', '11', '8', '15', '7'],
['1', '3', '14', '18', '20'],
['5', '3', '19', '20', '0']]
I've researched this already but I can't find an exact answer. I've found answers for appending when there's 2 lists involved but this is different so here goes.
I'm creating a really basic fuzzer but I'm having issues appending the directory names to the end of the address. Here's what I have so far.
The expected output is as follows;
www.website.com/1
www.website.com/2
www.website.com/3
www.website.com/4
etc. But I'm getting something completely different. Here's the first piece of code I tested.
>>> host = "www.website.com/"
>>> path = [line.strip() for line in open("C:/Users/Public/Documents/tester.txt", 'r')]
>>> print str(host) + str(path)
which returns the following
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
The second attempt was this;
>>> host = "www.website.com/"
>>> path = [line.strip() for line in open("C:/Users/Public/Documents/tester.txt", 'r')]
>>> for line in path:
print str(host) + str(path)
Which returned this;
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
I can see exactly what's happening and why it's happening but I can't figure out how to arrive at the expected output. I'll also need to filter out the special characters which I didn't think 'print' would pick up. Maybe there's different rules for print when it's reading something as a list.
I've thought of stupid complex methods such as counting the lines in the file and then throwing it into a while loop using that count but I'm sure there's something I can use or something I've done wrong. My Python knowledge isn't fantastic.
Can anybody help with this?
You were nearly there , when using the for loop , concatenate the elements from the list (which in your case is in variable 'line') , not the complete list again .
Code -
for line in path:
print str(host) + str(line)
This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 7 years ago.
I have this string:
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
and I want to return/output:
[['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']]
how do i get to this list of lists from that huge string?
ast.literal_eval is a good for exactly that.
>>> num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
>>> import ast
>>> list(ast.literal_eval(num))
[['1', '9', '7', '6'], ['2', '0', '8', '3', '7'], ['3', '8', '5', '7', '9', '10', '4']]
You can use AST:
import ast
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
num = list(ast.literal_eval(num))