Python help with sequences and elements?

Click For Summary

Discussion Overview

The discussion revolves around programming in Python, specifically focusing on how to replace elements in a sequence. Participants explore the differences between tuples and lists, their mutability, and provide various coding approaches to achieve the desired outcome.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a problem statement involving a sequence and seeks help on replacing an element.
  • Another participant suggests using an if statement to handle replacements, questioning whether the sequence needs to be modified every time an element appears.
  • A different participant shares a solution that involves creating a new sequence with the desired changes, demonstrating a working code snippet.
  • One participant proposes a code structure that resembles Java, indicating uncertainty about Python syntax.
  • Several participants clarify that the original sequence is a tuple, which is immutable, and discuss the need to use a list for mutability.
  • Another participant confirms that they successfully modified a list, contrasting it with the behavior of tuples.
  • There is a reiteration that tuples cannot be changed, emphasizing the distinction between tuples and lists.

Areas of Agreement / Disagreement

Participants generally agree on the immutability of tuples versus the mutability of lists. However, there are differing opinions on the best approach to replace elements in a sequence, and some uncertainty remains regarding the correct syntax and methods in Python.

Contextual Notes

Participants note the importance of understanding the differences between tuples and lists, particularly regarding mutability, which affects how elements can be replaced. There is also a lack of consensus on the most effective coding approach to solve the problem presented.

acurate
Messages
17
Reaction score
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
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?
 
I think I figured it out actually! It works when I do it:

sequence = ("one", "two", "three", "four")
sequence2 = ("zero",)+sequence[1::]
print (sequence2)
 
Cool beans man.
 
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...
 
lol, I'm not eveen going to change that. :D
 
BiGyElLoWhAt said:
lol, I'm not eveen going to change that. :D
Hahah, thank you for trying to help, though. Really appreciate it, man!
 
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:
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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 43 ·
2
Replies
43
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K