Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modifying a list in an expression
i have some incoming data. each unit of data can be converted to a list. that list can be converted back to that data. the conversions are expressions. i can nest these two expression to convert the data to a list and then back. i have a modification to the data that is defined in terms of it being in the list form. the modification is copying element index B to element index A of the list like. i can do this in 3 lines:

    temp = convert_data_to_list(data)
    temp[A] = temp[B]
    data = convert_list_to_data(temp)
is there an easy way to use slicing (or something else) to make this all be a single expression?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Something like this (as you can see it's readability is not great)?

convert_list_to_data([convert_data_to_list(data)[B] if convert_data_to_list(data).index(x) == A else x for x in convert_data_to_list(data)])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
that's also going to be slow. the code i gave is actually going to be in a loop affecting many units of data (each unit can be converted to a list). most units are strings and conversion to a list was a workaround because they are immutable. i guess squeezing this to a big expression in one line is just not going to happen.

and, yeah, it is harder to read. so i guess it's back to separate steps.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  looking fo an expression that yields a dictionary without an item Skaperen 5 170 Apr-09-2019, 02:05 AM
Last Post: Skaperen
  Get the variables in a sympy expression SJ001 8 215 Feb-12-2019, 07:25 AM
Last Post: SJ001
  iterate through a list with comparison of expression Alexandro 6 208 Jan-31-2019, 05:16 PM
Last Post: Scorpio
  assignment: not an operator nor expression, but x=y=z=3 works fine? jefdaels 1 142 Jan-29-2019, 02:19 PM
Last Post: perfringo
  Product expression. jarrod0987 1 194 Dec-13-2018, 11:32 AM
Last Post: buran
  How to assign a found regex expression to a variable Pedroski55 2 347 Nov-24-2018, 07:14 AM
Last Post: Pedroski55
  Random Expression Creator SheeppOSU 2 291 Nov-01-2018, 11:08 PM
Last Post: wavic
  Regular Expression rzbddm 4 346 Oct-30-2018, 04:25 PM
Last Post: stranac
  Is there a way to append to a list WITHOUT modifying the parameter passed? arnavb 11 666 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  File Watcher and File modifying chatbot sreevik 1 323 Sep-17-2018, 09:38 PM
Last Post: micseydel

Forum Jump:

Users browsing this thread: 1 Guest(s)