String and dots filling [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Write a program (without using loops) which given a string, of size less
than 5, and a positive integer less than 100, prints both of them with
enough dots in between so that the whole string has 20 characters in them.
I know that if I am not using loops I have to use print(..., sep="") supresses the blank between the objects ,printed.
Can somebody tell me How do I restrict new string length to 20 characters?

So you need to know the length of the string and the number of digits in your integer, subtract those from 20, and you know how many dots to place in-between. This shouldn't be too hard (check out the len() and str() functions).
Did you know that you can get a string of n dots by doing '.' * n?

Related

Find the average of string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I have an string in python (with float and integer numbers)
string = '44,44,44,33'
I want to calculate the mean of that.
Note: I have one large dataframe, and in one column of the dataframe, I have such a string. I want to calculate the mean for all rows. I hope I there is an efficient way for that.
Thanks
Spilt string, covert to integers, and calculate mean:
import statistics
result = statistics.mean(map(int, string.split(",")))
l = string.split(',')
average = sum(int(k) for k in l)/len(l)

Creating a List of a Range is slow, are there alternatives? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
so I have to create a List of a big range and am trying to accomplish this with list(range(number1, number2))and the numbers number1 and number2 can have a big difference, for example 235 and 4323246376. Now, this takes a long time to process. Can this process be sped up?
If you're only looking to use the list as a filter to count items in another list, you should not be using a range at all.
For example:
count = numpy.sum(theList >= 235) - numpy.sum(theList >= 4323246376)
What use case are you going to achieve with this?
In case you are going to use this list once, don't typecast the range object into a list.
When you typecast the range object into a list, It stores all the numbers in a list which costs you memory.
You can just iterate over the range object.

Averaging the digits of pi in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
As a preface, I must admit that this is my first real attempt to code a simple program from scratch, and I've just begun my foray into learning python.
As a simple project, I wanted to write a program to find the average value of the digits of π. In other words, we all know that π=3.141529....., but I set out to determine the value of (3+1+4+1+5+....)/(# of digits summed).
This was the result:
#retrieve the value of pi
n=math.pi
#request the number of digits of pi to average over
mag=input("how many terms?")
mag=int(mag)-1
#generate a string populated with the whole number values of the digits of py
k=int(round(n*10**(mag),0))
digits=[int(d) for d in str(k)]
print(k)
#sum the specified values of the digits of pi
print(sum(digits))
#recall the length of the string
print(len(digits))
#calculate average
print((sum(digits)/len(digits)))
The code runs well enough, but I am curious about what tweaks I could make to improve the program or simplify it.
Most specifically, I would like to know if there there is a simpler or more direct way to cast the individual digits of pi into a list or string of integers for manipulation. For instance, is there a specific operator one could use to call individual digits of a given number, like digit(0) returns 3 with respect to pi.
Any advice is greatly appreciated!

Splitting strings in 80%/20% parts [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list of strings and I only want to keep the first 80% of text of each string. So, if a string has for example 100 words, I only want to keep the first 80 words. The split function is not suitable for this problem.
What function can I use, while iterating over the list, to achieve this?
Why isn't it?
sentence = "long string lots of words..."
parts = sentence.split()
newsentence = ' '.join(parts[:len(parts)*4//5])

Special counting [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to fill an array in python with ones in a specific way. An example with 4 digits is the easiest way to explain:
0000, 0001, 0010, 0100, 1000, 1001, 1010, 1100, 1101, 1110, 1111
I need a list with 8 digits, so just writing them down is not an option. I really have no idea how to do that..
Edit: To clarify the problem. I need for the first entry all digits to be 0. Then all possibilities with just one 1, then all possibilities with two 1's and so forth. The last entry would be all 1.
An easy way is to sort all the binary representations by the number of 1s they have. Since python sort is stable, this also keeps the original order between numbers with the same number of 1s.
sorted(('{:08b}'.format(i) for i in xrange(256)), key=lambda x: x.count('1'))
Its not in the same order in your example, I assume you don't need the specific order:
for i in range(256):
print "{:08b}".format(i)
Also I assume you need values in strings, since you specified stuff like 000001 and that expression is meaningless in numbers (equals 1).

Categories