One-line Python Code that returns a sequence of integers

In summary, the code calculates the cumulative sum of a list of integers by using the numpy.cumsum function. This function takes a list of integers as input and returns the sum of the integers in the list.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! 😊

I want to write a one-line Python generator or iterator expression that returns the sequence of integers generated by repeatedly adding the ascii values of each letter in the word “Close” to itself. The first 10 integers in this sequence are: 67, 175, 286, 401, 502, 569, 677, 788, 903, 1004. If the code should not be in one line it would be as follows, or not ?
Code:
sum = 0 
for letter in list("Close") : 
    sum += ord(letter) 
    print(sum)
So that we write that in oe line the for-loop will be
Code:
sum += [ord(letter) for letter in list("Close")]
right? But how do we add at this line the print? :unsure:
 
Technology news on Phys.org
  • #2
mathmari said:
Code:
sum = 0
for letter in list("Close") :
    sum += ord(letter)
    print(sum)
Hey mathmari!

Shouldn't the loop be repeated? (Wondering)

mathmari said:
So that we write that in oe line the for-loop will be
Code:
sum += [ord(letter) for letter in list("Close")]
right? But how do we add at this line the print?
This doesn't parse since we can't add a list to a number. :oops:

I googled how to calculate a cumulative sum in python and found numpy.cumsum.
That is, we can do import numpy; numpy.cumsum([1,2,3]).

We can repeat a list by multiplying it with a number. For instance 5 * [ord(letter) for letter in list("Close")].

We can print a resulting list by calling print before it. For instance print [1,2,3].
🤔
 
  • #3
Klaas van Aarsen said:
I googled how to calculate a cumulative sum in python and found numpy.cumsum.
That is, we can do import numpy; numpy.cumsum([1,2,3]).

We can repeat a list by multiplying it with a number. For instance 5 * [ord(letter) for letter in list("Close")].

We can print a resulting list by calling print before it. For instance print [1,2,3].
🤔

Ahh ! So we should write print(numpy.cumsum([ord(letter) for letter in list("Close") ])) , right? :unsure:
 
  • #4
mathmari said:
Ahh ! So we should write print(numpy.cumsum([ord(letter) for letter in list("Close") ])) , right?
Yep. (Nod)
And if we add a multiplier before the list, we get:
Python:
>>> print(numpy.cumsum(3 * [ord(letter) for letter in list("Close")]))
[  67  175  286  401  502  569  677  788  903 1004 1071 1179 1290 1405 1506]
It matches the first 10 integers in the opening post. 🤔
 
  • #5
Klaas van Aarsen said:
Yep. (Nod)
And if we add a multiplier before the list, we get:
Python:
>>> print(numpy.cumsum(3 * [ord(letter) for letter in list("Close")]))
[  67  175  286  401  502  569  677  788  903 1004 1071 1179 1290 1405 1506]
It matches the first 10 integers in the opening post. 🤔

Ah ok! I got it! Thank you very much! :geek:
 

1. What is a one-line Python code that returns a sequence of integers?

The following code uses list comprehension to generate a sequence of integers from 0 to 9:

```[integer for integer in range(10)]```

2. How does this code work?

The code uses the built-in function range() to create a range of numbers from 0 to 9. The list comprehension then iterates through this range and adds each integer to a list.

3. Can this code be customized to generate a different sequence of integers?

Yes, the code can be modified to generate any sequence of integers by changing the parameters of the range() function. For example, range(5, 15) would generate a sequence from 5 to 14.

4. Can this code be used for non-integer values?

Yes, this code can be used to generate a sequence of non-integer values by using a float data type in the list comprehension. For example, [float for float in range(1, 10)] would generate a sequence from 1.0 to 9.0.

5. Is there a limit to the length of the sequence that can be generated with this code?

No, there is no limit to the length of the sequence that can be generated with this code. However, for very large sequences, it is recommended to use xrange() instead of range() for better performance.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
11
Views
778
  • Programming and Computer Science
Replies
8
Views
827
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
6K
Back
Top