On 14/08/18 22:38, Cameron Simpson wrote:
> If you're trying to partition words into values starting with "x" and values
> not starting with "x", you're better off making a separate collection for the
> "not starting with x" values. And that has me wondering what the list "b" in
> your code
On 14/08/18 23:16, Peter Otten wrote:
> For a simple solution you do need a and b: leave words unchanged, append
> words starting with "x" to a and words not starting with "x" to b.
>
> Someone familiar with Python might do it with a sort key instead:
Or, for one definition of simple, a list
Deepti,
What you’re seeing happens because you are making changes (words.remove(z)) to
the list while you are iterating over it (for z in words). If your goal is to
print the original words, removed words and original words without removed
words, you could do something like this using sets:
Thanks all. This is very helpful. I am new to Python :)
Sent from my iPhone
> On 15 Aug 2018, at 8:16 am, Peter Otten <__pete...@web.de> wrote:
>
> Alan Gauld via Tutor wrote:
>
>>> On 14/08/18 09:11, Deepti K wrote:
>>> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>>>
Alan Gauld via Tutor wrote:
> On 14/08/18 09:11, Deepti K wrote:
>> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
>> function, it picks up only 'xzz' and not 'xaa'
>
> Correct because
>
>> def front_x(words):
>> # +++your code here+++
>> a = []
>> b = []
>>
On 14Aug2018 18:11, Deepti K wrote:
when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
function, it picks up only 'xzz' and not 'xaa'
def front_x(words):
# +++your code here+++
a = []
b = []
for z in words:
if z.startswith('x'):
words.remove(z)
b.append(z)
On 14/08/18 09:11, Deepti K wrote:
> when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
> function, it picks up only 'xzz' and not 'xaa'
Correct because
> def front_x(words):
> # +++your code here+++
> a = []
> b = []
> for z in words:
> if z.startswith('x'):
when I pass ['bbb', 'ccc', 'axx', 'xzz', 'xaa'] as words to the below
function, it picks up only 'xzz' and not 'xaa'
def front_x(words):
# +++your code here+++
a = []
b = []
for z in words:
if z.startswith('x'):
words.remove(z)
b.append(z)
print 'z is', z
print
On 2018-08-14, Alan Gauld via Tutor wrote:
> - Use a list for objects(*) where you might need to
> change the value of one of the objects
A list is standard practice when you need an ordered collection
of objects of the same type, e.g., a bunch of numbers, a bunch of
chickens, etc.. It is most
On 14/08/18 07:56, Rafael Knuth wrote:
> List comprehension is really cool. One thing I like about list
> comprehension is that you can get a dictionary, tuples or lists as a
> result by just changing the type of braces.
>
> # dictionary
> colors = ["red", "blue", "white", "yellow"]
> colors_len
Use List comprehension:
animals = ["Dog", "Tiger", "SuperLion", "Cow", "Panda"]
animals_lol = [[animal, len(animal)] for animal in animals]
print(animals_lol)
[['Dog', 3], ['Tiger', 5], ['SuperLion', 9], ['Cow', 3], ['Panda', 5]]
If you want to read more about list comprehension,
> I wrote this code below
> I was wondering if there is a shorter, more elegant way to accomplish this
> task.
> Thanks!
thank you so much everyone!
List comprehension is really cool. One thing I like about list
comprehension is that you can get a dictionary, tuples or lists as a
result by just
Just a quick 'Thank you' for this advice the other day Alan and
Abdur-Rahmaan.
Greatly appreciated as we work together with our students here.
Thank you.
Matthew Polack | Teacher
[image: Emailbanner3.png]
Trinity Drive | PO Box 822
Horsham Victoria 3402
p. 03 5382 2529 m. 0402456854
13 matches
Mail list logo