Переместился на https://github.com/jinahya/bit-io
. Посмотрите на http://jinahya.googlecode.com/ svn / trunk / com.googlecode.jinahya / bit-io / src / main / java / com / googlecode / jinahya / io /
com.googlecode.jinahya
bit-io
1.0-alpha-13
Это небольшая удобная библиотека для чтения / запись произвольной длины бит с помощью Java.
final InputStream stream;
final BitInput input = new BitInput(new BitInput.StreamInput(stream));
final int b = input.readBoolean(); // reads a 1-bit boolean value
final int i = input.readUnsignedInt(3); // reads a 3-bit unsigned int
final long l = input.readLong(47); // reads a 47-bit signed long
input.align(1); // 8-bit byte align; padding
final WritableByteChannel channel;
final BitOutput output = new BitOutput(new BitOutput.ChannelOutput(channel));
output.writeBoolean(true); // writes a 1-bit boolean value
output.writeInt(17, 0x00); // writes a 17-bit signed int
output.writeUnsignedLong(54, 0x00L); // writes a 54-bit unsigned long
output.align(4); // 32-bit byte align; discarding
Я бы предложил использовать random.shuffle
, затем list.pop
, чтобы избежать дублирования ключей и значений, чтобы всегда иметь три элемента в dict (пропущенный пользовательский ввод, уже показано в других ответах):
import random
colors=['black','blue','green','red','yellow']
nums=[1,2,3,4,5]
res = {}
for _ in range(3):
random.shuffle(colors)
random.shuffle(nums)
num, color = nums.pop(), colors.pop()
res[num] = color
print(res)
#=> {4: 'green', 3: 'yellow', 1: 'black'}
print(res[1])
#=> black
print(colors, nums) # not used
#=> ['blue', 'red'] [2, 5]
Поместите пары в словарь:
pairs = {}
for a in range(1,4):
l=random.choice(lst1)
l1=random.choice(lst)
pairs[l] = l1
n = int(input("Enter a number:"))
if n in pairs:
print(pairs[n])
else:
print"Not found")