Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to change values in a nested list depending on elements of two other different lists and function is difficult to read
    primarykey
    data
    text
    <p>objective of the function is to change the elements of this</p> <pre><code>image=[[(15,103,225), (0,3,19)], [(22,200,1), (8,8,8)], [(0,0,0), (5,123,19)]] </code></pre> <p>into this with encode(nested, message)</p> <pre><code>&gt;&gt;&gt;encode(image,'hello) [[(14, 103, 255), (0, 3, 18)], [(22, 200, 0), (9, 9, 8)], [(0, 1, 0), (5, 122, 19)]] </code></pre> <p>each character in the message is converted into it's corresponding ascii thing and then each ascii thing is changed into 8-digit, base-two numbers. got that covered.</p> <p>now, to change the list like the above, what i have is:</p> <pre><code>def char_to_bits(char): """char_to_bits(char) -&gt; string Convert the input ASCII character to an 8 bit string of 1s and 0s. &gt;&gt;&gt; char_to_bits('A') '01000001' """ result = '' char_num = ord(char) for index in range(8): result = get_bit(char_num, index) + result return result def get_bit(int, position): """get_bit(int, position) -&gt; bit Return the bit (as a character, '1' or '0') from a given position in a given integer (interpreted in base 2). The least significant bit is at position 0. The second-least significant bit is at position 1, and so forth. &gt;&gt;&gt; for pos in range(8): ... print(b.get_bit(167, pos)) ... 1 1 1 0 0 1 0 1 """ if int &amp; (1 &lt;&lt; position): return '1' else: return '0' def message_to_bits(message): if len(message)==0: return '' for m in message: return "".join("".join(str(bits.char_to_bits(m)))for m in message) def set_bit_on(int, position): """set_bit_on(int, position) -&gt; int Set the bit at a given position in a given integer to 1, regardless of its previous value, and return the new integer as the result. The least significant bit is at position 0. The second-least significant bit is at position 1, and so forth. &gt;&gt;&gt; set_bit_on(0, 0) 1 &gt;&gt;&gt; set_bit_on(0, 1) 2 &gt;&gt;&gt; set_bit_on(167, 3) 175 """ return int | (1 &lt;&lt; position) def set_bit_off(int, position): """set_bit_off(int, position) -&gt; int Set the bit at a given position in a given integer to 0, regardless of its previous value, and return the new integer as the result. The least significant bit is at position 0. The second-least significant bit is at position 1, and so forth. &gt;&gt;&gt; set_bit_off(0, 0) 0 &gt;&gt;&gt; set_bit_off(1, 0) 0 &gt;&gt;&gt; set_bit_off(167, 0) 166 &gt;&gt;&gt; set_bit_off(175, 3) 167 """ return int &amp; ~(1 &lt;&lt; position) def set_bit(int, bit, position): """set_bit(int, bit, position) -&gt; int Set the bit at a given position to the given bit (as a char, either '1' or '0') regardless of its previous value, and return the new integer as the result. The least significant bit is at position 0. The second-least significant bit is at position 1, and so forth. &gt;&gt;&gt; set_bit(0, '1', 0) 1 &gt;&gt;&gt; set_bit(0, '1', 1) 2 &gt;&gt;&gt; set_bit(0, '1', 2) 4 &gt;&gt;&gt; set_bit(0, '1', 3) 8 &gt;&gt;&gt; set_bit(175, '0', 3) 167 &gt;&gt;&gt; set_bit(175, '1', 3) 175 """ if bit == '1': return set_bit_on(int, position) else: return set_bit_off(int, position)​ from collections import Iterable def flatten(nested): for item in nested: if isinstance(item, Iterable) and not isinstance(item, basestring): for x in flatten(item): yield x else: yield item def encode(nested, message): position= 0 fnested= list(flatten(nested)) bitlist= list("".join("".join(str(bits.char_to_bits(m)))for m in message)) for int in fnested: for bit in bitlist: return "".join("".join(("".join(str(bits.set_bit(int,bit,position)))for int in fnested)) for bit in bitlist) </code></pre> <p>the encode returns a string of length 1200 which is an epic failure and it looks very non-user-friendly.</p> <p>please help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload