Python help with sequences and elements?

In summary: Sure. That's with 'sequence' a list. Try it with the tuple ('one','two','three').You're right, Dick. With parentheses, the sequence is immutable.
  • #1
acurate
17
1

Homework Statement


My professor wants us to program on Python, where we have a certain sequence, for example:
sequence = ("one", "two", "three", "four")
I need to replace one of the sequence elements (example: "one") with another element (example: instead of the word "one", I need to put "zero").

2. The attempt at a solution
I really have no idea how to do it, but I guess I have to start with.

sequence = ("one", "two", "three", "four")

Do I need to use something like:

sequence [1 : : 2]

or something else?

Would really appreciate if you guys would help.
I would appreciate even a hint or advice on which path to take to be able to write this.
 
Technology news on Phys.org
  • #2
Do you need to replace it everytime it shows up? Might be able to handle this with a simple if statement. Is the sequence a while loop?
 
  • #3
I think I figured it out actually! It works when I do it:

sequence = ("one", "two", "three", "four")
sequence2 = ("zero",)+sequence[1::]
print (sequence2)
 
  • #4
Cool beans man.
 
  • #5
I'm not a huge python guy myself, but I was thinking something like
for(k=0, sequence(k::k)!="one", k++){
print(sequence(k::k)
else print("zero")
}
This looks suspiciously like java though...
 
  • #6
lol, I'm not eveen going to change that. :D
 
  • #7
BiGyElLoWhAt said:
lol, I'm not eveen going to change that. :D
Hahah, thank you for trying to help, though. Really appreciate it, man!
 
  • #8
acurate said:

Homework Statement


My professor wants us to program on Python, where we have a certain sequence, for example:
sequence = ("one", "two", "three", "four")
I need to replace one of the sequence elements (example: "one") with another element (example: instead of the word "one", I need to put "zero").

2. The attempt at a solution
I really have no idea how to do it, but I guess I have to start with.

sequence = ("one", "two", "three", "four")Do I need to use something like:

sequence [1 : : 2]

or something else?
Something else.
Given the definition you have above, the statement below will change the string at index 1 to the string "four".
Code:
sequence[1] = "four"
Note that indexes start at 0.

Edit: My lack of expertise in Python is showing. Defining a sequence as shown in the Attempt section above makes it a tuple, which is immutable. Defining a sequence as I did, with brackets, makes it a list, and mutable.
acurate said:
Would really appreciate if you guys would help.
I would appreciate even a hint or advice on which path to take to be able to write this.
 
Last edited:
  • #9
Mark44 said:
Something else.
Given the definition you have above, the statement below will change the string at index 1 to the string "four".
Code:
sequence[1] = "four"
Note that indexes start at 0.

Well, no it won't. As written 'sequence' is a tuple. Tuples are immutable - you can't change them. To do something like that you'd need to change 'sequence' to a list - which is mutable.
 
  • #10
Dick said:
Well, no it won't. As written 'sequence' is a tuple. Tuples are immutable - you can't change them. To do something like that you'd need to change 'sequence' to a list - which is mutable.
@Dick, it worked for me. This is an exact cut-and-paste from my python session.
Python:
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> sequence = ["one", "two", "three"]
>>> sequence
['one', 'two', 'three']
>>> sequence[1] = "four"
>>> sequence
['one', 'four', 'three']
 
  • #11
Mark44 said:
@Dick, it worked for me. This is an exact cut-and-paste from my python session.
Python:
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> sequence = ["one", "two", "three"]
>>> sequence
['one', 'two', 'three']
>>> sequence[1] = "four"
>>> sequence
['one', 'four', 'three']

Sure. That's with 'sequence' a list. Try it with the tuple ('one','two','three').
 
  • #12
You're right, Dick. With parentheses, the sequence is immutable.
 

Related to Python help with sequences and elements?

1. What is the difference between a list and a tuple in Python?

A list is a mutable data structure in Python, meaning that its elements can be modified. A tuple, on the other hand, is immutable, meaning that its elements cannot be changed once it is created. Additionally, a list is denoted by square brackets [ ] while a tuple is denoted by parentheses ( ).

2. Can you explain the concept of indexing in Python lists?

Indexing is the process of accessing a specific element in a list. In Python, indexing starts at 0, meaning that the first element in a list has an index of 0. For example, if a list has 5 elements, their indices would be 0, 1, 2, 3, and 4. Negative indices can also be used to access elements from the end of the list, with -1 being the index of the last element.

3. How can I add or remove elements from a list in Python?

To add elements to a list, you can use the append() method to add an element to the end of the list, or the insert() method to add an element at a specific index. To remove elements, you can use the remove() method to remove a specific element, or the pop() method to remove an element at a specific index.

4. What is the difference between a for loop and a list comprehension in Python?

A for loop is a control flow statement that allows you to iterate through a sequence of elements. In contrast, a list comprehension is a concise way of creating a new list by iterating through an existing list and applying a condition. List comprehensions are often more efficient and readable than for loops.

5. How can I reverse the order of elements in a list in Python?

You can use the reverse() method to reverse the order of elements in a list in-place, meaning that the original list will be modified. Alternatively, you can use the slice notation [::-1] to create a new list with the elements in reversed order without modifying the original list.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
7
Views
638
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
372
  • Programming and Computer Science
Replies
1
Views
747
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top