division of text in lines - python

hello and thank you for any help I need help
I have this code
he reads the 5-10 lines but I need to remake it
f=open("input.txt", encoding='utf-8')
lines=f.readlines()
for x in range(5,10):
print(lines[x])
I am looking for this output
first input second is output image
input
1-100 lines
im search this number search in list
xx = lambda n:n and list(range(n,100,9))
print(xx(num))
output lines is text
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
[2, 12, 22, 32, 42, 52, 62, 72, 82, 92]
[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]
[5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
[6, 16, 26, 36, 46, 56, 66, 76, 86, 96]
[7, 17, 27, 37, 47, 57, 67, 77, 87, 97]
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98]
[9, 19, 29, 39, 49, 59, 69, 79, 89, 99]
[10, 20, 30, 40, 50, 60, 70, 80, 90,100]
just make a square
from 1 to 10 down from 11 to 20 down etc ...
there is text on the lines so that, for example, all 10 texts are combined into one
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
this is just an example I have a long text of 33 thousand lines

Add 10 to the list index to get the contents of the line 10 rows later.
with open("input.txt", encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
lines = [line.rstrip() for line in lines]
for i in range(10):
output_line = ",".join(lines[i+n] for n in range(0, 100, 10))
print(output_line)

Related

How do you split an array into specific intervals in Num.py for Python?

The question follows a such:
x = np.arange(100)
Write Python code to split the following array at these intervals: 10, 25, 45, 75, 95
I have used the split function and unable to get at these specific intervals, can anyone enlighten me on another method or am i doing it wrongly?
Here's both the manual way and the numpy way with split.
# Manual method
x = np.arange(100)
split_indices = [10, 25, 45, 75, 95]
split_arrays = []
for i, j in zip([0]+split_indices[:-1], split_indices):
split_arrays.append(x[i:j])
print(split_arrays)
# Numpy method
split_arrays_np = np.split(x, split_indices)
print(split_arrays_np)
And the result is (for both)
[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]),
array([25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]),
array([45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74]),
array([75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94])
]

for loop range outside a sliding window

I have a range in a list(100 strings). I want to iterate a number of times (say 4 times) over this range by selecting 30 strings at a given interval and store them in a list. But I also want to store the selections outside the 30 strings and store them in a different list using pyspark.
Assuime the integers in the lists below are strings, Example code:
t=[]
start=[10,20,50,70]
for i in start:
windoww=i+30
for j in range(i,windoww):
t.append(j)
The above code gets me a list t ranging 10 to 99 with the start string specified in start.
I also want a list that is outside t. So for the first iteration if the content of t is
t= [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
I want another list say u that has
u=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Is this possible in pyspark?
I tried creating a list from [0 to 100] called v and did:
u=[stri for stri in v not in t]
However, I get an error that
'bool' object is not iterable
Try below function
def Diff(li1, li2):
return (list(list(set(li1)-set(li2)) + list(set(li2)-set(li1))))
li1 = [10, 15, 20]
li2 = [20, 15, 35]
li3 = Diff(li1, li2)
print(li3)
Output = [10,35]

How do I input randomly generated integer values in a 2-d array in Python [duplicate]

This question already has answers here:
Simple way of creating a 2D array with random numbers (Python)
(7 answers)
Closed 2 years ago.
I have written my code like this:
import random
a = []
for i in range(10):
for j in range(10):
a.append(random.randint(0,100))
x = np.shape(a)
print(a)
This is my output:
[[], 79, 46, 29, 48, 88, 43, 57, 53, 70, 55, 89, 19, 49, 11, 79, 41, 76, 82, 90, 91, 21, 86, 67, 80, 93, 13, 38, 51, 27, 43, 50, 79, 87, 23, 27, 1, 64, 43, 81, 67, 48, 35, 9, 50, 48, 70, 73, 94, 58, 75, 60, 43, 73, 88, 51, 12, 74, 88, 72, 83, 100, 7, 10, 50, 13, 64, 74, 37, 76, 44, 37, 46, 42, 20, 20, 100, 81, 11, 83, 27, 76, 29, 15, 3, 18, 81, 5, 34, 85, 99, 88, 53, 75, 53, 12, 19, 62, 1, 51, 44]
It is a 10 x 10 array. The first value is an array and 99 rest of the values. Can anyone help me solve this issue?
Try this:
import random
[[random.randint(0,100) for i in range(10)] for j in range(10)]

How to print a long string of numbers neatly in python 3.6?

>>> text = [ str(i) for i in range(1, 100)]
>>> print( " {}".format( ", ".join( str(i) for i in text ) ) )
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Instead of printing a long text string, I would like a neater print out of text with 80 characters width for each line, each line is indented with 2 character space and each element to be equally spaced like so:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Is there a ready python object that I can use to do this? If not, what is the pythonic way of achieving this in python 3.6?
As Saelyth pointed out, use textwrap to format this, but before that, you need to format each number right justified and take up 2 spaces:
import textwrap
text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
print('\n'.join(textwrap.wrap(one_line, width=80)))
Output:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
Update
If you want 2-space indent, the first way is to print each line with the indentation:
import textwrap
text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
for line in textwrap.wrap(one_line, width=78):
print(' {}'.format(line))
The second way is to use textwrap.indent:
import textwrap
text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
block = '\n'.join(textwrap.wrap(one_line, width=78))
print(textwrap.indent(block, ' '))
Note that because of the 2-space indentation, I reduced the width from 80 down to 78.
Update 2
Thanks to Tomerikoo for the suggestion to use textwrap.fill, the code is now simpler:
import textwrap
text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
print(textwrap.fill(one_line,
width=80,
initial_indent=' ',
subsequent_indent=' '))
Update 3
In this updated, I added advice from Tim to reduce the steps further.
import textwrap
one_line = ', '.join('{:>2}'.format(e) for e in range(1, 100))
# one_line == ' 1, 2, 3, ...'
print(textwrap.fill(one_line,
width=80,
initial_indent=' ',
subsequent_indent=' '))
From the textwrap library:
>>> text = [str(i) for i in range(1, 100)]
>>> print(textwrap.fill(" {}".format(", ".join(str(i) for i in text)), width=80))
The description of textwrap.fill:
textwrap.fill(text, width=70, **kwargs)
Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. fill() is shorthand for
"\n".join(wrap(text, ...))
Basically this function wraps the text to the given width. If you are willing to accept the default width of 70, you can ommit the width keyword argument entirely.
Many of the other answers are missing the power of the Python formatting mini-language also, as you are using Python 3.6 f-strings!
# Format range into a spaced-out string
formatted_range = ", ".join(f"{x:2d}" for x in range(0, 100))
# Split into lines and print
print(textwrap.fill(formatted_range, width=80))
f{x:2d} will format an integer to take up 2 characters (padded with spaces). More info here
Again textwrap is used to split across lines, I have used textwrap.fill which is a shorthand for "\n".join(wrap(text, ...))
import textwrap
maximum = 100
text = str([str(i).zfill(len(str(maximum))) for i in range(1, maximum)])[1:-1]
text = textwrap.wrap(text, width=20)
for i in text:
print(i)
This would do the trick, the textwrap module makes "lists" of your desired width in case you want to work with those lines. So you just need to feed it some text and get your result.
Also added zfill so all numbers are of the same lenght as the maximum number.
If you just need the text, other answers are better.
Using only format. We first pack all the elements to one long line while fixing their length to 2. Then just joining the lines with '\n' using cs_in_line jumps with added 2-spaces:
text = [str(i) for i in range(1, 100)]
one_line = ", ".join("{:>2}".format(num) for num in text)
print("\n".join(" "+one_line[i:i+cs_in_line] for i in range(0, len(one_line), cs_in_line)))
And this gives:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

Python, delete subinterval from an interval

In python, I have an interval
a = 1-100
And I have a list with intervals
b = [11-20,41-50,91-110]
Now I would like my output to be like this in python:
a-b = 1-10,21-40,51-90
You have this :
a=range(1,100)
b = [range(11,20),range(41,50),range(91,110)] #list of ranges
So you can try :
d=[]
i=a.start
for sub_range in b: #for each sub range
#get the start and stop
start=sub_range.start
end=sub_range.stop
#append range to list
d.append(range(i,start-1))
i=end+1
print(d)
[range(1, 10), range(21, 40), range(51, 90)]
You can try this
a = range(1,101)
set(a)
b = [11,12,13,14,15,16,17,18,19,20,41,42,43,44,45,46,47,48,49,50,91,92,93,94,95,96,97,98,99,100]
set(a)-set(b)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}
You can use more_itertoools to divide the list
import more_itertools as mit
iterable = list(d)
[list(group) for group in mit.consecutive_groups(iterable)]
Result
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40], [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]]

Categories