Search a list and split - python

First of all sorry for the very basic question but I haven't been able to find an example with the end results I need. In the following example I have a list with numbers and sometimes the numbers are joined like index 2 and 5 in the following list.
list = ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10']
I would like to search the list and split when it finds a comma so the end result is the following:
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
What would be the easiest way to accomplish this?

>>> [x for y in ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10'] for x in y.split(', ')]
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

An alternative method is:
>>> my_list = ['1', '2', '3, 4', '5', '6', '7, 8', '9', '10']
>>> sum((item.split(', ') for item in my_list), [])
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
or
>>> ', '.join(my_list).split(', ')
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

Related

How to remove items from list and make items join

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]

Python Bubble Sort, 10 being considered < 5, 7 etc [duplicate]

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(""))

How to add a list to another list

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

How to add each number to list in python?

I have following variable:
number = "456367"
I need to add it to list by one number, like this:
list = [['4', '5', '6', '3', '6', '7']]
How can I achieve this?
Since a string is iterable, pass it to the list constructor:
>>> list("456367")
['4', '5', '6', '3', '6', '7']
Which is why you would not want to name the result list because you would stomp on the name of that convenient function ;-)
If you really want [['4', '5', '6', '3', '6', '7']] vs just a list of characters:
>>> s="456367"
>>> map(list, zip(*s))
[['4', '5', '6', '3', '6', '7']]
Or,
>>> [list(s)]
[['4', '5', '6', '3', '6', '7']]
But I am assuming with the first answer that the extra brackets were a typo.
>>> number = "456367"
>>> [char for char in number]
['4', '5', '6', '3', '6', '7']

Printing a variable and appending it with a line from a file in a loop

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)

Categories